31

I don't have much idea on Java.

I was going through few links and found blog says "Java Primitives stored on stack", which I feel it depends on instance variable or local variable.

After going through several links my conclusion is,


Class variables – primitives – are stored on heap as a part of Object which it contains.

Class variables – object(User Defined) – are stored on heap as a part of Object which it contains. This is true for both reference and actual object.

Method Variables – Primitives – are stored on stack as a part of that stack frame.

Method Variables – object(User Defined) – are stored on heap but a reference to that area on heap is stored on stack as a part of that stack frame. References can also be get stored on heap if Object contains another object in it.

Static methods (in fact all methods) as well as static variables are stored in heap.

Please correct me if my understanding is wrong. Thanks.

2
  • 2
    See.stackoverflow.com/questions/8204595/…. I think you have everything other than methods right. Commented Oct 16, 2013 at 11:45
  • @user1436026: Link is very useful. Commented Oct 16, 2013 at 11:48

3 Answers 3

16

There are some optimizations in the JVM that may even use the Stack for Objects, this reduces the garbage collection effort.

Classes are stored on a special part of the heap, but that depends on the JVM you use. (Permgen f.e. in Hotspot <= 24).

In general you should not have to think about where the data is stored, but more about the semantics like visibility and how long something lives. Your explanation in the questions looks good so far.

Sign up to request clarification or add additional context in comments.

4 Comments

i guess place of storage is also important when it comes to thread safety
generally allocation of RAM ie Stack to JVM is less, if Stack is used for Objects then will that not be a problem, consider scenario if String str = new String("//Big Size String"). storing this big string will make some operation to StackOverFlowException?
@Jayesh: you told it yourself that "Method Variables – object(User Defined) – are stored on heap but a reference to that area on heap is stored on stack" so you replied to yourself that it wont be problem as whole big string shall be on heap, and only reference to it shall be on stack.
Also one good reason why method members shall be on stack is that if they would not than the method could not be recursively called.
6

"Method Variables – object(User Defined) – are stored on heap but ..."

Wrong. First, method variables are called local variables.

Let's consider

public static void main(String[] args) {
    List<Integer> model = new ArrayList<Integer>();

Variable model is placed in the stack frame, not on heap. The referenced object generated with new ArrayList<Integer>() is placed in the heap but it is not a local variable.

The 3 things:

  • variable model
  • generated object
  • reference to that object, stored in a variable

are quite different, do not mess them up.

5 Comments

I think I have told the same thing which you are telling. Also, there is only 2 thing one is reference and one is actual space created for object. So, your 3 points is actually 2 I think, "variable model" is same as "reference to that object, stored in a variable".
@Jayesh Nope. "variable model" is (as any variable) a range of memory. "reference to that object" is a value, just like primitive value 123 or 3.14. References may be stored in different variables, or in no one. Variables may contain different values,
@Jayesh same variable may contain different values in different moments of time. Do you think that a handbag is the same as things in it?
You are right but as the discussion was going on memory part, I think generated object(in heap) is where you handbag is and values inside it will be at same place where handbag is created, so that they can store it, so "generated object" and "reference to that object, stored in a variable" will be at same place always, am I correct?
@Jayesh No, not always. Reference can be in a stack frame, as in my code example. And anyway, "at the same place" does not mean "the same". If you store my phone number in your phone's memory, will your phone and my phone number be the same?
2
  • Object instances are stored in the Heap.

  • The object reference is stored on the stack.

  • Static variables are stored in the method area.

Example

abc obj=new abc();

The abc object is saved in the heap and the object reference to it is stored on the stack.

static int i=10;

The i variable is stored in the method area.

4 Comments

what do you mean by method area here? is it where method binary will get placed?
This is the area where bytecodes reside. The program counter points to some byte in the method area. It always keep tracks of the current instruction which is being executed (interpreted). After execution of an instruction, the JVM sets the PC to next instruction. Method area is shared among all the threads of a process. Hence if more then one threads are accessing any specific method or any instructions, synchorization is needed. Synchronization in JVM is acheived through Monitors.
Method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.
String literals are stored on heap too, in the string pool. Regardless of whether or not they were created as a local variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.