JVM Internals + Memory
What are the main JVM memory areas?
The JVM organizes runtime memory into areas like the heap, stack, method area, PC register, and native method stack. The key interview idea is knowing what is shared across threads, what is private to each thread, and where objects, method calls, and class metadata live.
The Short Answer
The JVM splits runtime memory into several areas. The most important ones to understand for interviews are the heap, JVM stack, method area, PC register, and native method stack.
The big mental model is simple: some memory is shared by all threads, while some memory is private to each thread.
The Mental Model
One JVM Process
Shared Across Threads
Private Per Thread
Why This Matters
JVM memory areas explain common production and interview topics:
- Why recursive code can cause StackOverflowError
- Why too many objects can cause OutOfMemoryError
- Why local variables are naturally thread-private
- Why shared objects need concurrency control
- Why garbage collection mostly focuses on heap memory
Heap: Where Objects Live
The heap is the runtime area where Java objects and arrays are allocated. It is shared across all JVM threads.
User user = new User("Asha");
// The reference variable "user" may be local to a method,
// but the User object itself lives on the heap.Reference vs Object
Thread Stack
Heap
This is where many beginners get confused. The local variable may be on a stack frame, but the actual object created with new is on the heap.
JVM Stack: Where Method Calls Live
Each thread has its own JVM stack. Every method call creates a stack frame. That frame holds things like local variables, intermediate results, and information needed to return from the method.
public int addTax(int price) {
int tax = 5;
return price + tax;
}One Thread's Stack
Method Area: Where Class Metadata Lives
The method area stores class-level information: class metadata, method information, field information, bytecode for methods and constructors, and runtime constant pool data.
Method Area Stores
A useful way to think about it: heap stores objects, while the method area stores information about classes.
PC Register: Where Is This Thread Executing?
Each thread has a program counter, often called the PC register. It tracks the current JVM instruction being executed by that thread.
You usually do not directly think about the PC register in day-to-day Java coding, but it is part of the JVM runtime model and helps explain how each thread can be executing different code independently.
Native Method Stack
The native method stack is used when Java code calls native methods, such as code written in C or C++ through JNI.
For most backend interview discussions, it is enough to know that this exists for native execution and is separate from the ordinary Java method-call stack.
Common Interview Mistake
Common Interview Follow-Ups
Is the heap shared across threads?
Yes. Objects and arrays allocated on the heap can be accessed by multiple threads if references are shared.
Is the stack shared across threads?
No. Each thread has its own JVM stack. That is why method-local variables are naturally thread-private.
What causes StackOverflowError?
Usually deep or infinite recursion that requires more stack frames than the thread stack can support.
What causes OutOfMemoryError?
Often heap exhaustion, but it can also involve other memory areas depending on what cannot be allocated.
Where do static fields live?
The class metadata is associated with the method area, but the actual storage details depend on JVM implementation. For interviews, explain that static data is class-level, not instance-level.