20

Is an instance variable of an object in Java stored on the stack or method area of the JVM?

Also, do we have different instance variable for multiple threads?

If it is stored in method area how is instance variable different from static variable storage?

dimo414
  • 47,227
  • 18
  • 148
  • 244
saurabh goyal
  • 1,754
  • 8
  • 35
  • 45

3 Answers3

26

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. (variable references either primitive or object references are also stored in the stack). Heap is a memory place where the objects and its instance variable are stored.

So to sum it up:

  • Class objects, including method code and static fields: heap.
  • Objects, including instance fields: heap.
  • Local variables and calls to methods: stack

Also, do we have different instance variable for multiple threads?

Every thread will have a program counter (PC) and a java stack. PC will use the java stack to store the intermediate values, dynamic linking, return values for methods and dispatch exceptions. This is used in the place of registers.

Also for more about thread, you really should read this topic Where is Thread Object created? Stack or Heap?.

If it is stored in method area how is instance variable different from static variable storage?

As you can see above static fields are stored in heap. On the other hand, local variables are stored in stack.

//EDIT

According to the comments of Bruno Reis and Peter Lawrey, you should also read about Escape analysis

  1. Wikipedia
  2. Virtual Machine Performance Enhancements,Escape Analysis
Community
  • 1
  • 1
Matej Špilár
  • 2,617
  • 3
  • 15
  • 28
  • This is not strictly correct. Modern JVMs are much more complex and have optimization algorithms that may determine that storing a given object on a given situation on the *stack* may lead to better performance (i.e., less stress on the GC). For example, google JVM Escape Analysis. In any case, unless you are writing something really low level, you shouldn't care whether something is stored on heap or stack. – Bruno Reis May 08 '14 at 19:26
  • 1
    I am not saying that this is strictly correct. This is just a very simple a view how are variables stored in Java. – Matej Špilár May 08 '14 at 19:30
  • 1
    @BrunoReis true, but escape analysis rarely results in avoiding use of the heap. – Peter Lawrey May 08 '14 at 19:35
  • 1
    @MatejSpili, your assumption is incorrect: I didn't down vote your answer. The answer is fine, not wrong, good enough for most developers, just not 100% strictly correct. (I didn't up vote either -- to me, this is perfectly neutral!) (and looks like your comment is gone by now!) – Bruno Reis May 08 '14 at 19:45
  • 1
    @BrunoReis I this case I am really sorry :) i have also edited the answer with links to the escape analysis and so on ... – Matej Špilár May 08 '14 at 19:50
6

To be precise,

  • Instance variables will be stored on the heap.
  • local variables on the stack(in case of variable not a primitive[reference variable] reference variables live on stack
    and the object on heap ). Only method invocation and partial results will be stored in stack not the method itself.
  • Static variables and Methods(including static and Non Static) on the Method Area.

Reference: Head First Java

Jayanth
  • 746
  • 6
  • 17
5

Most of the JVM implementation divides memory into following parts:

  1. Method Area
  2. Stack
  3. Heap
  4. pc registers
  5. Native method stacks.

Lets talk about Method Area, Stack and Heap only.

For e.g Take a class

class Lava {
  int i = 5;
  static int j = 10;
  void flow() { //some implementation}
 }

When an instance of this object is created from a class X

Lava l = new Lava();

First, Class type of Lava, i.e. Lava.class is stored in your Method area, with details like methods, fields and other referencing type. Also static variables like j in our example is stored in Method area itself.

Second the instance of Object Lava is stored in Heap Area as well as its instance variable i.e i.

Third, Its reference, i.e l in our example is stored in Stack area, which point to instance that is created in Heap.

aditya lath
  • 410
  • 7
  • 6