wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is a Heap Memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a Heap Memory?" Watch "What is a Heap Memory?" New topic
Author

What is a Heap Memory?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Hello,
What is HEAP MEMORY? How it is different than RAM Memory and Hard Disk Space? Which Memory get affected by this? What I mean which memory a program uses when it runs?
Any Comments?
regards,
Arun
Tom Hennigan
Ranch Hand

Joined: Feb 25, 2001
Posts: 71
Forget hard drive - think only of system memory or, RAM.
The compiler for any language must use some runtime data structure for storage of stuff like references to objects in Java or method calls in C++. C++ uses a "stack" for such storage, Java uses a "heap" both of which are dynamically managed at runtime.
The key difference, here, is that different pieces of storage can be allocated from and returned to the heap in no particular order. Whenever something is allocated on the heap, its lifetime is independent of the scope in which it was allocated. Stacks release memory references onces the method or variable completes (returns) or otherwise goes out of scope.
Java uses heap-based storage for variables accessed through references; hence, objects can continue to live even after they go out of scope. Heaps and stacks grow dynamically on demand so most compilers will place them at opposite ends of the memory address space.
This is why you might hear that the heap and the stack grow into the hole between them.


Tom Hennigan<P>Sun Certified Java 2 Platform Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A little animation showing how the heap works:
Artima.com:Heap of Fish: A Simulation of a Garbage-Collected Heap


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
site. The artima.com
thanks Valentin.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yeah they are doing a great job over there
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
Actually, C++ also uses the heap to store objects and primitives.
int* p = new int(1); // goes to heap
int i = 1; // goes to stack
X* q = new X; // goes to heap
X r; // goes to stack
Note that the pointer p itself is stored on the stack while the int it is referencing is on the heap. If the value of p is lost, that little bit of int in the heap is inaccessible, and no friendly garbage collecting thread is going to pick it up, so it becomes a memory leak. Same with q.
Also, I think Java primitives are stored on the stack, am I right?
-anthony
[ March 29, 2002: Message edited by: Anthony Villanueva ]
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Oh GOD!! So much of JGurus are answering for this. Thanks a lot. But if you forgive me perhaps I am a beginner who does not know about basics and should first fix up his basics.
Can I ask the basic defination of following, before it goes beyond my understanding:
1. What is HEAP MEMORY? how can you define it.
2. What is STACK MEMORY? how can you define it.
3. What is the differance between two?
I hope you won't mind answering this.
regards,
arun
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I think I'll let the JVM Spec speak for me:
JVMS 3.5.2 Java Virtual Machine Stacks
JVMS 3.5.3 Heap
In short, the JVM stack offers some kind of temporary area for local variables, computations, method activation frames, etc. If you want, the stack is the construction area where everything gets done during execution. Each thread in a JVM has a private stack.
The heap, however, is shared among all JVM threads and is the runtime data area or long-term storage room containing all instantiated objects and arrays. So, everytime you create an new object, it is going to be stored on the heap. The heap further contains what we call the method area. The latter contains all code for all methods, constructors and initializers.
The garbage collection only act on the heap but does neither touch the method area nor the different stacks.
You should really play a little with the following link in order to understand what the heap is:
Artima.com:Heap of Fish: A Simulation of a Garbage-Collected Heap
Moreover, Cindy wrote a nice article in the March newsletter containing a short description of what the heap is:
Cindy's Segment - a Whimsical View of the World
[ March 30, 2002: Message edited by: Valentin Crettaz ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is a Heap Memory?