The JVM's heap (memory area) stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.
You can even control the heap size, by issuing command :-
This option specifies that 32 MB of memory should be allocated initially for the program to run in, and that up to 128 MB may be allocated if necessary.
Hope it helps !
[ March 29, 2006: Message edited by: Mishra Anshu ]
"Ignorance is bliss"
Joined: Mar 23, 2006
Thanks for your answer. But I didn't get what I want exactly. I want to know what is heap(memory area). Is this the memory that JVM owns ??? or could be any part of RAM ?? . I want answer in low detail.
Those were both neat sources. One part of your question that didn't seem to be explicitly answered is: Yes, the stack and heap are both areas that are managed by the JVM.
Command line parameters (or default values) tell the JVM a min and max size for various types of memory. At startup the JVM asks the operating system for the min. If that fills up, the JVM can ask the OS for more up to the max. When the garbage collector cleans up, it frees some of the memory managed by the JVM but the JVM doesn't (usually) give that back to the OS. So if you watch the memory used by the JVM with an OS tool you may see it grow from min to max but never shrink. I think some of the later JVMs may release memory to the OS as an exception to all this.
More important than stack & heap is to understand the life cycle of objects and variables. Variables created within a method, including its parameters, only last until the method exits. Then they go away. If those variables point to objects and there are no other pointers, the objects become eligible for garbage collection. If those variables are pointers to objects and there are other references to those objects the objects may live much longer.
There are even shorter life spans. If you declare a variable within curly braces, say inside an "if" or "while" or "catch" the variables go out of scope at the close brace.
The stack is the mechanism for these local variables. It's not the only possible mechanism, but it's a perfect fit, very fast and efficient and is built into most CPUs just for this purpose. I think the JVM spec specifies a "stack" so even if it's just an abstraction we can talk about the stack and those local variables together.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
If you really want to learn what a stack and heap are, read up on them in terms of their data structure properties, not as generic places in memory controlled by the JVM.
You can write your own heap and stack(as well as linked lists, other trees, queues, ect) and highly recommend you do so after you have a decent grasp of the language. [ April 06, 2006: Message edited by: Rusty Shackleford ]
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra