wood burning stoves*
The moose likes Beginning Java and the fly likes What is stored on Stack and Heap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is stored on Stack and Heap" Watch "What is stored on Stack and Heap" New topic
Author

What is stored on Stack and Heap

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi all,

Can anyone tell me what is stored on a stack and what is stored on a heap in a Java program?

Thanks.


Be Humble... Be Nice.
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hello,
go below link
http://forums.sun.com/thread.jspa?messageID=2406140


Best Regards,<br />Vipin<br />MCA, SCJP5, SCWCD in progress
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hello Arjun,

AS I Think
Primitives are stored on the stack, objects in the heap space.

int j = 20; will be stored on the stack, because the variable itself holds the value and it's much faster, but if you wrap it:
Integer p = new Integer(20); then it's stored in the heap space because it's now an object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
That doesn't sound right; I thought that all fields live in the heap and local and intermediate variables are pushed onto the stack. That is slightly imprecise for Java 6 however.

Anybody else?
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Hi Arjun...

Well Heap and stack are the two main memories that JVM is concerned as far as memory allocation by the OS(operating system) is concerned.

As said above, stack and heap are the memories allocated by the OS to the JVM that runs in the system.

Stack is a memory place where the methods and the local variables are stored.

Heap is a memory place where the objects and its instance variable are stored.

Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack.

i have written this to the best of knowledge. Any corrections are welcome.

Thank You.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18498
    
  40

Stack is a memory place where the methods and the local variables are stored.


By "methods", I am assumming that you mean the callstack -- not the actual code for the methods.

Heap is a memory place where the objects and its instance variable are stored.


Yes.

Also it is to be remembered that the variable references (either primitive or object references) are stored in the stack.


Only for local variables. For instance and class variables, they are part of the object, hence, on the heap.

Finally, Java 6 may throw a monkey wrench into this. Due to "escape analysis" optimization, sometimes Java 6 will allocate objects on the stack.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vipin jain
Ranch Hand

Joined: Aug 24, 2008
Posts: 122
hello,

i thing all thought is going different way so can any replay wath is final and Actully answere ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.
    fundu man
    Greenhorn

    Joined: Jan 10, 2007
    Posts: 3
    Thanks everyone for good informations.

    But can anyone say where Interface variables are stored ? heap or stack?
    vipin jain
    Ranch Hand

    Joined: Aug 24, 2008
    Posts: 122
    hello fundu man

    As per JavaRanch Rules you have to Rename your specific name.
    I don't think funda man is any person name.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37884
        
      22
    Originally posted by vipin jain:
    hello fundu man

    As per JavaRanch Rules you have to Rename your specific name.
    I don't think funda man is any person name.


    I think you are right: fundu man please confirm that is your real name or go to update profile and correct it to match the naming policy.
    Ove Lindström
    Ranch Hand

    Joined: Mar 10, 2008
    Posts: 326

    Originally posted by fundu man:
    Thanks everyone for good informations.

    But can anyone say where Interface variables are stored ? heap or stack?


    Heap, since interfaces only exists as implementations in classes and they are on the heap.
    Jason Gosling
    Greenhorn

    Joined: Oct 18, 2009
    Posts: 5
    I concern about this much. Remember in Runtime Environment Data, we have another field named "Method Area" which contains Method information and class variables as well

    In my opinion, the separation between stack and heap can be deemed as following:

    + Stack: local variables (both primitive & reference) and method parameters

    + Heap: object

    ==>When we have a declaration like this

    class A{
    int e = 1;
    public int math (int x, int y){
    A a = new A();
    return (A.e + x + y);
    }
    }

    Then we have:

    Stack: x, y, a
    Heap: instance a (it is A object), a.e = 1

    (Note that a in stack points out to instance a in heap)

    If instance a is no longer used, it is garbage collected
    jeetendra Choudhary
    Ranch Hand

    Joined: Jun 02, 2009
    Posts: 33
    Hi guys,

    I have doubt as per my knowledge interfaces can have constants which are direct values, so it should be in the stack. Please correct if I am wrong here.

    Regards
    Jeetendra


    Preparing for SCJP...!!
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    jeetendra Choudhary wrote:Hi guys,

    I have doubt as per my knowledge interfaces can have constants which are direct values, so it should be in the stack. Please correct if I am wrong here.


    You are wrong here. I don't know what you mean by "direct values," as that's not part of any standard Java lexicon, and whatever it means, I don't know why you would think that means they go on the stack.

    The rule, however, is quite simple: Local variables (including method parameters) go on the stack. Everything else goes on the heap. And note that variables never store objects; they only hold primitives and references. So all objects go on the heap. And all class definitions go on the heap (in a special area called the method area), which includes constants defined in those classes.

    (Note that there was some talk of including or allowing escape analysis in Java 7, so that implementations would be allowed to store objects on the heap if they could verify that the object was never used outside that method invocation. I don't know if that made it into the spec, or, even if it did, if any implementations take advantage of it. So for now, it's safe to say that all objects are always on the heap, and only local variables are on the stack.)
    Dattatraya Tembare
    Greenhorn

    Joined: Feb 01, 2009
    Posts: 10
    Dear All,

    Here one important thing has missed, it will clear all doubts.

    Heap: is a single area where JVM allocates memory for -Objects, including method code , static variables & instance variables.

    Stack: Stack is created for each individual thread, JVM allocates memory for - local variables & arguments (values passed to method variables)

    @ interface - all values in interface are constants i.e final static, so it's stored on Heap only.

    Hope these will clear the picture.

    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Dattatraya Tembare wrote:Dear All,

    Here one important thing has missed, it will clear all doubts.

    Heap: is a single area where JVM allocates memory for -Objects, including method code , static variables & instance variables.


    Already covered. Not missed.

    Stack: Stack is created for each individual thread, JVM allocates memory for - local variables & arguments (values passed to method variables)


    True, and worth knowing, but not really important for the OP's question.

    @ interface - all values in interface are constants i.e final static, so it's stored on Heap only.


    Correct.

    Hope these will clear the picture.


    I'm not sure why you'd think the picture wasn't clear already.

    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Youtube Video


    Tell the difficulties that i am difficult.
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Campbell Ritchie wrote:
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.


    In method code, only if you create any object then it will go in heap else all local variables will go in stack.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    saloni jhanwar wrote:
    Campbell Ritchie wrote:
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.


    In method code, only if you create any object then it will go in heap else all local variables will go in stack.


    What Campbell stated was correct. All the executable code for method bodies goes on the heap. It's only local variables that go on the stack.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    saloni jhanwar wrote:Youtube Video


    That video is somewhere between misleading and just plain wrong.

    It seems to suggest that a method's executable code goes on the stack, which is false. If that's not what it's trying to say, it should be clearer in how it's worded.

    It also talks about stack variables being "flushed", as if there's some active process to remove or clear them. That's not how it works. The stack pointer simply gets reset to its previous value. The old values of the variables are still there on the stack until another stack frame overwrites them.
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Jeff Verdegan wrote:
    saloni jhanwar wrote:
    Campbell Ritchie wrote:
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.


    In method code, only if you create any object then it will go in heap else all local variables will go in stack.


    False, what Campbell stated was correct. All the executable code for method bodies goes on the heap. It's only local variables that go on the stack.


    In a method code, we not only create objects but we also create local variables, so we can't say that whole method code will go on heap,i think so.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    saloni jhanwar wrote:
    Jeff Verdegan wrote:
    saloni jhanwar wrote:
    Campbell Ritchie wrote:
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.


    In method code, only if you create any object then it will go in heap else all local variables will go in stack.


    False, what Campbell stated was correct. All the executable code for method bodies goes on the heap. It's only local variables that go on the stack.


    In a method code, we not only create objects but we also create local variables, so we can't say that whole method code will go on heap,i think so.


    It doesn't matter what we create. There's no reason that the executable code has to live in the same region of memory as the data it refers to.

    The executable code exists exactly once for each method, and it goes in the method area which is part of the heap. Executable code never goes on the stack.
    http://docs.oracle.com/javase/specs/jvms/se5.0/html/VMSpecTOC.doc.html
    http://docs.oracle.com/javase/specs/jvms/se5.0/html/Overview.doc.html#6656

    Note also that the executable code is not copied for each invocation of the method. There's no reason to do so, since the executable code is read-only.
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Jeff Verdegan wrote:
    saloni jhanwar wrote:
    Jeff Verdegan wrote:
    saloni jhanwar wrote:
    Campbell Ritchie wrote:
  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack
  • And as Henry has pointed out some objects can be created on the stack in Java 6.


    In method code, only if you create any object then it will go in heap else all local variables will go in stack.


    False, what Campbell stated was correct. All the executable code for method bodies goes on the heap. It's only local variables that go on the stack.


    In a method code, we not only create objects but we also create local variables, so we can't say that whole method code will go on heap,i think so.


    It doesn't matter what we create. There's no reason that the executable code has to live in the same region of memory as the data it refers to.

    The executable code exists exactly once for each method, and it goes in the method area which is part of the heap. Executable code never goes on the stack.
    http://docs.oracle.com/javase/specs/jvms/se5.0/html/VMSpecTOC.doc.html
    http://docs.oracle.com/javase/specs/jvms/se5.0/html/Overview.doc.html#6656

    Note also that the executable code is not copied for each invocation of the method. There's no reason to do so, since the executable code is read-only.


    Thanks for links.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    You're welcome.
    Pavan Kumar Dittakavi
    Ranch Hand

    Joined: Feb 12, 2011
    Posts: 104

    Hello Cambell Ritchie and Henry Wong,

    I am curious what can make the jvm to store an object in the stack area. I'm really interested in knowing it. Could you guys please explain it...

    Thanks,
    Pavan.

    EDIT: I came to know that this is Escape Analysis,..a compiler optimization technique. I would look into it..thanks .
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: What is stored on Stack and Heap
     
    Similar Threads
    Static method overridden!! need felp!!
    implementation for static and non-static methods stored in memory
    Stack & heap for Static methods
    java compiler issues
    JVM memory management