When a Java program runs, the JVM divides memory into several logical areas. Each area has a specific role in storing class data, objects, variables, and method calls. Understanding these areas is essential for performance tuning, debugging, and memory management.
Stores class-level data such as class metadata, static variables, and method bytecode. It is shared among all threads.
Stores objects and instance variables. This is the largest memory area and is managed by the Garbage Collector.
Stores method calls, local variables, and partial results. Each thread has its own stack.
Keeps track of the currently executing instruction for each thread.
Stores native (non-Java) method information used by JNI.
// Demonstrates where variables and objects are stored
class MemoryDemo {
static int staticVar = 10;
int instanceVar = 20;
public static void main(String[] args) {
int localVar = 30;
MemoryDemo obj = new MemoryDemo();
obj.show();
}
void show() {
int methodVar = 40;
System.out.println(methodVar);
}
}
staticVar → Method AreainstanceVar & object obj → HeaplocalVar and methodVar → Stack