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.

JVMMemoryPerformanceSenior Java

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

Heap
Method Area
Runtime Constant Pool

Private Per Thread

Thread 1 Stack
Thread 2 Stack
Thread 1 PC
Thread 2 PC
Native Stack
Native Stack
If you remember only one thing: objects usually live on the heap, method calls live on each thread's stack, and class metadata lives in the method area.

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.

java
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

user reference

Heap

User object

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.

java
public int addTax(int price) {
    int tax = 5;
    return price + tax;
}

One Thread's Stack

frame: addTax(price, tax)
frame: calculateTotal()
frame: main()
This is why local primitive variables inside a method are naturally isolated per thread. Each thread has its own 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

Class metadata
Method data
Field data
Runtime constant pool
Method bytecode
Constructor bytecode

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

Do not say “all variables live on the stack.” That is too vague and often wrong. A local reference variable may be in a stack frame, but the object it points to usually lives on the heap.

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.

Final Takeaway

Heap is shared object memory. Stack is per-thread method-call memory. Method area stores class-level runtime structures. PC registers and native method stacks support per-thread execution.