Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java variable placed on stack or heap

Tags:

java

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.

like image 756
Jayesh Avatar asked Oct 16 '13 11:10

Jayesh


People also ask

Are variables stored in stack or heap?

Stack and a Heap ? Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.

Are variables stored in the heap Java?

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.

What variables are stored in stack and heap in Java?

Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

Are Java objects stored in heap or stack?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.


3 Answers

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.

like image 70
Thomas Avatar answered Oct 17 '22 19:10

Thomas


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

like image 26
Alexei Kaigorodov Avatar answered Oct 17 '22 19:10

Alexei Kaigorodov


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.

like image 2
Dev Avatar answered Oct 17 '22 19:10

Dev