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?
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.
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 .
"JavaRanch, where the deer and the Certified play" - David O'Meara