29

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.

Jayesh
  • 6,047
  • 13
  • 49
  • 81
  • 2
    See.http://stackoverflow.com/questions/8204595/java-c-method-representation-in-memory. I think you have everything other than methods right. – DJG Oct 16 '13 at 11:45
  • @user1436026: Link is very useful. – Jayesh Oct 16 '13 at 11:48

3 Answers3

15

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.

Thomas
  • 11,272
  • 2
  • 24
  • 40
  • 2
    i guess place of storage is also important when it comes to thread safety – upog Oct 16 '13 at 11:35
  • 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 Oct 16 '13 at 11:44
  • @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. – Vit Bernatik Apr 29 '15 at 18:53
  • 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. – Vit Bernatik Apr 29 '15 at 18:57
5

"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.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • 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 Oct 16 '13 at 12:38
  • 1
    @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, – Alexei Kaigorodov Oct 16 '13 at 12:44
  • @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? – Alexei Kaigorodov Oct 16 '13 at 12:52
  • 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 Oct 16 '13 at 12:56
  • @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? – Alexei Kaigorodov Oct 16 '13 at 13:35
2

Object are stored in the Heap.

The object reference stored in the stack.

Static variable stored in the method area.

Example

abc obj=new abc();

abc object save in the heap and the object reference is stored in the stack.

  static int i=10;

i variable stored in the method area.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
Dev
  • 3,410
  • 4
  • 17
  • 16
  • what do you mean by method area here? is it where method binary will get placed? – Jayesh Oct 16 '13 at 12:03
  • 1
    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. – Dev Oct 16 '13 at 12:06
  • 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. – Dev Oct 16 '13 at 13:11
  • String literals are stored on heap too, in the string pool. Regardless of whether or not they were created as a local variable. – wild_nothing Apr 01 '19 at 16:18