cjl
cjl

Reputation: 11

Where does the JVM store local primitive variables on a method

One Question: where "1"、"2" stored when the program runs. I only know "new Object()" will be stored in [Heap],does "1" and "2" both stored in [Java Virtual Machine Stacks] or maybe in [Method Area] thanks a lot! Here the code:

class MyClass {
    public void fun() {
        int a = 1;//where the "1" stored
        Integer b = 2;//where the "2" stored
        Object c = new Object();//i just know "new Object()" stored in heap
    }
}

Example

Upvotes: 0

Views: 1426

Answers (2)

JohannesB
JohannesB

Reputation: 2308

On a conceptual level you should refer to the specifications of the JVM in practice you may also see some objects on the stack instead of the heap as an optimization known as escape analysis but that depends on the JVM implementation, e.g. Hotspot or OpenJ9, etc.

Also you should be aware that a Cache for Autoboxing of primitives ints to wrapper objects like Integer complicates things further for 256 values per type (-128 to 127 if I recall correctly) 😉

But try compiling your application to bytecode and then disassemble it with javap to see what happens at the bytecode level

Upvotes: 0

Holger
Holger

Reputation: 298579

Every local variable is stored on the stack. But in case of object types, the variable only contains a reference to the object, which is why these types are also called reference types.

The objects are always stored in the heap memory, as that’s the way heap memory is defined in the first place:

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

Since Integer is a reference type, in your example

Integer b = 2;

the local variable b is stored in the stack memory and contains a reference to a heap allocated Integer object. The Integer object itself contains a field holding the value 2.

Likewise for

Object c = new Object();

the Object instance is stored in the heap memory and the variable c, holding a reference to that object, is stored in the stack memory.

In contract, since int is a primitive type

int a = 1;

declares a variable a which will be stored in the stack memory, containing the value 1 directly.

But beware that this is only a mental model. An actual implementation can do whatever it wants, as long as the behavior is compatible.

The cite above hints at the relevant difference regarding the behavior; it says “… heap that is shared among all Java Virtual Machine threads”, which is in contrast to the local variables living in the stack space of a particular thread. Compare with JLS, §17.4.1:

17.4.1. Shared Variables

Memory that can be shared between threads is called shared memory or heap memory.

All instance fields, static fields, and array elements are stored in heap memory. In this chapter, we use the term variable to refer to both fields and array elements.

Local variables (§14.4), formal method parameters (§8.4.1), and exception handler parameters (§14.20) are never shared between threads and are unaffected by the memory model.

Upvotes: 3

Related Questions