File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is heap & stack memory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is heap & stack memory?" Watch "What is heap & stack memory?" New topic
Author

What is heap & stack memory?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
Was just wondering if there is some explanation on above and as a programmer how one can take care of same?
Arun
babu krishnan
Greenhorn

Joined: Jun 09, 2002
Posts: 25
hi,
1)Stack:
a)All primitive data types are stored here.
Becoz you know the size of all primitive types.
b)All object rerences(handles for the object) are stored in stack.
2)Heap:
All Objects are stored in Heap. The reference for the object which is in stack points to this object.
From programmers point of view, whenever you declare a handle you reserve a space in stack. And whenever you say new keyword you create object in heap.
In the program when the scope of variable ends, the reference pointing to the object is removed. But the object hangs and GC has to collect it, if it run shorts of memory.
Hope this wud help you
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
Hi,
To augment what babu krishnan said, here is an excerpt from Thinking in Java, by Bruce Eckel, Chapter 2
----------------------------------------------------------------

The stack. This lives in the general RAM (random-access memory) area, but has direct support from the processor via its stack pointer. The stack pointer is moved down to create new memory and moved up to release that memory. This is an extremely fast and efficient way to allocate storage, second only to registers. The Java compiler must know, while it is creating the program, the exact size and lifetime of all the data that is stored on the stack, because it must generate the code to move the stack pointer up and down. This constraint places limits on the flexibility of your programs, so while some Java storage exists on the stack—in particular, object references—Java objects themselves are not placed on the stack. [ Add Comment ]

The heap. This is a general-purpose pool of memory (also in the RAM area) where all Java objects live. The nice thing about the heap is that, unlike the stack, the compiler doesn’t need to know how much storage it needs to allocate from the heap or how long that storage must stay on the heap. Thus, there’s a great deal of flexibility in using storage on the heap. Whenever you need to create an object, you simply write the code to create it using new, and the storage is allocated on the heap when that code is executed. Of course there’s a price you pay for this flexibility: it takes more time to allocate heap storage than it does to allocate stack storage (that is, if you even could create objects on the stack in Java, as you can in C++).
Special case: primitive types
There is a group of types that gets special treatment; you can think of these as “primitive” types that you use quite often in your programming. The reason for the special treatment is that to create an object with new—especially a small, simple variable—isn’t very efficient because new places objects on the heap. For these types Java falls back on the approach taken by C and C++. That is, instead of creating the variable using new, an “automatic” variable is created that is not a reference. The variable holds the value, and it’s placed on the stack so it’s much more efficient.
----------------------------------------------------------------
 
jQuery in Action, 2nd edition
 
subject: What is heap & stack memory?