Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are instance variables of an Object stored in the JVM?

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?

like image 796
saurabh goyal Avatar asked May 08 '14 19:05

saurabh goyal


People also ask

Where are objects stored in JVM?

Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.

Where does the instance variable found?

Instance variables in Java are non-static variables which are defined in a class outside any method, constructor or a block. Each instantiated object of the class has a separate copy or instance of that variable. An instance variable belongs to a class.

How variables are stored in JVM?

Class variables(Static variables) are stored as part of the Class object associated with that class. This Class object can only be created by JVM and is stored in permanent generation . Also some have answered that it is stored in non heap area which is called Method Area.

Where local and instance variables are stored?

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.


2 Answers

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
like image 168
Matej Špilár Avatar answered Sep 24 '22 05:09

Matej Špilár


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

like image 39
Jayanth Avatar answered Sep 23 '22 05:09

Jayanth