What are the different memory areas allocated by the JVM?
The JVM divides memory into several runtime data areas, each with a specific purpose and lifecycle.
**Heap**: The largest memory area. All object instances and arrays are allocated here. It is shared across all threads and is managed by the garbage collector. The heap is further divided into Young Generation (Eden + two Survivor spaces) and Old Generation (Tenured), with G1GC and ZGC using region-based layouts instead. `OutOfMemoryError` is thrown when the heap is exhausted.
**Stack (JVM Stack)**: Each thread has its own stack. It stores stack frames — one per method invocation. A frame holds: local variables, the operand stack, and a reference to the runtime constant pool of the method's class. When a method returns, its frame is popped. `StackOverflowError` is thrown when recursion exhausts the stack depth.
**Method Area** (Metaspace in Java 8+): Stores class-level data: the runtime constant pool, field and method data, method bytecode, and static variables. Prior to Java 8 this was PermGen (fixed size, a common OOM source). Java 8 replaced it with Metaspace, which uses native memory and grows dynamically (bounded by `MaxMetaspaceSize`). Each class loaded by a classloader stores its metadata here.
**PC Register** (Program Counter): One per thread. Holds the address of the current JVM instruction being executed. For native methods, the PC is undefined.
**Native Method Stack**: Supports native (non-Java) methods written in C/C++ via JNI. One per thread. Behaves like the JVM stack but for native frames.
**Runtime Constant Pool**: Technically part of the Method Area. Contains symbolic references from the class file's constant pool, resolved at runtime to actual memory references.
For GC tuning, Heap sizing (`-Xms`, `-Xmx`) and Metaspace sizing (`-XX:MaxMetaspaceSize`) are the primary levers.
Correctly describes Heap, Stack, and Method Area/Metaspace with their purposes and per-thread vs shared characteristics.
Also covers PC Register, Native Method Stack, the PermGen-to-Metaspace migration in Java 8, heap generations, and relevant JVM flags.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment