Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

As I understand , in Java, Stack memory holds primitives and method invocations and heap memory is used to store objects .

Suppose I have a class

class A {
       int a ;
       String b;
       //getters and setters
  1. where will the primitive 'a' in class A be stored ?

  2. Why does heap memory exist at all ? Why cant we store everything in stack ?

  3. When the object gets garbage collected , is the stack associated with the objected destroyed ?

share|improve this question
stackoverflow.com/questions/1056444/is-it-on-the-stack-or-heap seems to answer your question. –  S.Lott Apr 5 '11 at 11:48
@S.Lott, except that this one is about Java, not C. –  Péter Török Apr 5 '11 at 11:51
@Péter Török: Agreed. While the code sample is Java, there's no tag to indicate that it's only Java. And the general principle ought to apply as well to Java as C. Further, there's a lot of answers to this question on Stack Overflow. –  S.Lott Apr 5 '11 at 11:54
Should be on Stackoverflow? –  Steve Haigh Apr 5 '11 at 11:56
Did anybody else read "Stack and Happy memory"? –  EricSchaefer Apr 5 '11 at 12:00

6 Answers 6

up vote 33 down vote accepted

The basic difference between stack and heap is the life cycle of the values.

Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded.
Heap values however exist on the heap. They are created at some point in time, and destructed at another (either by GC or manually, depending on the language/runtime).

Now Java only stores primitives on the stack. This keeps the stack small and helps keeping individual stack frames small, thus allowing more nested calls.
Objects are created on the heap, and only references (which in turn are primitives) are passed around on the stack.

So if you create an object, it is put on the heap, with all the variables that belong to it, so that it can persist after the function call returns.

share|improve this answer
"and only references (which in turn are primitives) " Why do say that references are primitives ? Can you clarify please? –  Geek Jul 31 '12 at 8:33
@Geek: Because the common definition of primitive data types applies: "a data type provided by a programming language as a basic building block". You might also notice that references are listed among the canonical examples further down in the article. –  back2dos Jul 31 '12 at 8:52
@Geek: In terms of data, you may view any of the primitive data types - including references - as numbers. Even chars are numbers and can be used interchangeably as so. References are also just numbers referring to a memory address, either 32 or 64 bits long (although they cannot be used as such - unless you're messing around with sun.misc.Unsafe). –  Sune Rasmussen Apr 2 '13 at 9:35
  1. In the heap, as part of the object, which is referenced by a pointer in the stack. ie. a and b will be stored adjacent to each other.
  2. Because if all memory were stack memory, it wouldn't be efficient any more. It's good to have a small, fast-access area where we start and have that reference items in the much larger area of memory which remains. However, this is overkill when an object is simply a single primitive which would take up about the same amount of space on the stack as the pointer to it would.
  3. Yes.
share|improve this answer
I would add to your point #2 that if you stored objects on the stack (imagine a dictionary object with hundreds of thousands of entries) then in order to pass it to or return it from a function you'd need to copy the object every time. By using a pointer or reference to an object in the heap, we only pass the (small) reference. –  Scott Whitlock Apr 5 '11 at 12:25
I thought 3 would be 'no' because if the object is being garbage-collected, then there is no reference in the stack pointing to it. –  Luciano Apr 5 '11 at 13:19
@Luciano - I see your point. I read question 3 differently. The "at the same time" or "by that time" is implicit. :: shrug :: –  pdr Apr 5 '11 at 16:53

In case of Java, the only variables that go on stack are the local variables being used by the current method invocation. Of course only in case of primitive types their value is going to be actually stored on stack.

share|improve this answer

The stack is for transient data objects (like variables in your methods). The heap is for things that have to persist awhile longer.

share|improve this answer
  1. On the heap unless Java allocates the class instance on the stack as an optimization after proving via escape analysis that this will not affect semantics. This is an implementation detail, though, so for all practical purposes except micro-optimization the answer is "on the heap".

  2. Stack memory must be allocated and deallocated in last in first out order. Heap memory can be allocated and deallocated in any order.

  3. When the object is garbage collected, there are no more references pointing to it from the stack. If there were, they'd keep the object alive. Stack primitives aren't garbage collected at all because they're automatically destroyed when the function returns.

share|improve this answer

Stack memory usually stores local variables, method calls, one over the other ..

Stack is always busy in arranging the data and popping it out after the method is executed.

stack memory stores the primitive datatype like int, double, etc.,

stack only stores the address (in other words points to the data in heap), say when you create a new object from your class A from a new program,

A X(object ) = new A;

creating 'new' object uses both stack and heap ,, whereas stack will store the address of the object in the heap (.. just an address..) the real data will be stored on the heap.

when we create another object from the class A, A Y = new A;

and when we assign, Y=X;

what will happen is, Y will just store the address of the variables used in the heap, so,

passing message to the objects X and Y makes no difference .

when you pass message to your object, x.move();

this method move(), is allocated in stack and it will just refer the heap memory with its address.. after the execution of move(), stack pops it out(that is why i said it is very active and busy)

Garbage collection is the process that java does to reclaim the memory when it is no longer used ....

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.