This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Heap" Watch "Heap" New topic
Author

Heap

Srinivas Redd
Greenhorn

Joined: Mar 23, 2006
Posts: 17
Hi ,

I hear a word "heap" , like JVM store 2 bytes in heap etc... . What is this heap. Is this a part of RAM that JVM owns ???

Thanks in advance ..
Srinivas.


Srinvasa Reddy Thamma,<br />Chennai,India.
Mishra Anshu
Ranch Hand

Joined: Sep 16, 2003
Posts: 224
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 :-

java -Xms32m -Xmx128m MyClassName

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"
Srinivas Redd
Greenhorn

Joined: Mar 23, 2006
Posts: 17
Hi Mishra,

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.

Thanks in advance.
fred Joly
Ranch Hand

Joined: Jan 19, 2006
Posts: 55
Take a look at this, maybe it can help :
http://www.coderanch.com/t/388024/java/java/heap-stack
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also see Where storage lives from Eckel's Thinking in Java.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
 
 
subject: Heap