Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Heap

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Srinivas Redd
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this, maybe it can help :
https://coderanch.com/t/388024/java/java/heap-stack
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see Where storage lives from Eckel's Thinking in Java.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic