• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects on Heap

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While going through various java books i come across the sentences like "Objects are alocated memory from heap" or String are part of String Pool..
I want to know what all kind of memories (heap/pool)are there in java and what are their advantages ...e.g. why objects are allocated on heap?
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
here in Java Memory allocation is different from
Non Object Oriented Language as there is nothing like pointers.
When u create a new Object java assigns some memory to this object.
Regarding Heap and Pool, as u must be aware Strings are Immutable Objects i.e. once u create
eg..
String s1= "Anurag";
one string by name Anurag will be created and s1 will point to the memory where Anurag is stored, now if again u try to do eg
String s2= "Anurag"; s2 will not create a new Memory Location for "Anurag" it will point to same old memory location, as it will check if there is a String by name "Anurag" exists or not, if its already there it will point to old String otherwise it will create a new memory location for that String.
due to String immutable property it will check in Memory(heap) used by JVM, abt new Object whether it already exists there or not.By this it saves lot of Space i mean Memory saving.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So - part of memory is reserved for the JVM to use for classfiles and method areas and String pools and such.
Part of memory is reserved for the JVM to do it's current thinking, this includes the Stacks that local variables live in.
Then there is the heap where all of the objects are just dumped in random order. No one would ever find anything on the heap if there were not references to them kept by the creating objects.
Note that there is some lea way in the Java Language Specification that allows JVM vendors to choose to implement some JVM stuff on the heap if they so choose. That should not matter to you as the programmer.
You might enjoy reading this article about Not all Variables are created Equal. It describes the layout of the memory in a "less than technical" approach .
 
Deepti Tewari
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy.
Your article really helped me.
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic