Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 1 OF 4 · 2 MIN READ

JVM Memory: Heap, Stack and Metaspace

The JVM splits memory into the heap for objects, a stack per thread for method frames, and metaspace for class metadata. Interviewers ask where variables live, what causes StackOverflowError and OutOfMemoryError, and how to size memory in containers.

What you will learn

  • Name the runtime data areas
  • Explain what lives on the stack and on the heap
  • Know the common OutOfMemoryError messages
  • Size the heap with -Xmx and MaxRAMPercentage

Key concepts

Heap

Shared by all threads; every object and array lives here. Split into young (eden + survivor spaces) and old generations for most collectors. Size with -Xms and -Xmx.

Stack

One per thread. Each method call pushes a frame with local variables (primitives and references) and partial results. Deep or infinite recursion throws StackOverflowError. Size with -Xss.

Metaspace

Class metadata, stored in native memory since Java 8 (replacing PermGen). Leaking class loaders, for example on repeated hot redeploys, fills it.

Other areas

The program counter per thread, native method stacks, the code cache for JIT-compiled code, and direct buffers outside the heap.

Step by step

Where does each thing live?

class OrderHandler {
    static int handled;                // static field: stored with the class (on the heap since Java 8)
    private final Cache cache;         // instance field: inside the OrderHandler object, on the heap

    void handle(int qty) {             // qty: stack (primitive)
        Order o = new Order(qty);      // o: reference on the stack; the Order object: heap
        String s = "PAID";             // literal in the string pool (heap)
        handled++;
    }
}

Containers

Modern JVMs read container memory limits. Prefer -XX:MaxRAMPercentage=75 over a fixed -Xmx so the heap scales with the container, and leave room for metaspace, thread stacks and direct memory, or the container is OOM-killed.

Examples

OutOfMemoryError messages and their meaning

java.lang.OutOfMemoryError: Java heap space              → too many live objects (leak or too small heap)
java.lang.OutOfMemoryError: GC overhead limit exceeded   → almost all time spent in GC, little reclaimed
java.lang.OutOfMemoryError: Metaspace                    → too many classes / class loader leak
java.lang.OutOfMemoryError: unable to create native thread → too many threads

# capture a heap dump automatically
java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dumps -jar app.jar

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Write recursive code that throws StackOverflowError and change -Xss to see the effect.
  • Explain why a pod with a 1 GB limit and -Xmx1g gets killed.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Is Java pass-by-value or pass-by-reference?

Always pass-by-value. For objects the value passed is a copy of the reference, so a method can change the object's fields but cannot make the caller's variable point to a different object.

What is escape analysis?

A JIT optimisation: if an object never escapes the method, the JVM can avoid allocating it on the heap (scalar replacement) and remove locks on it, which reduces GC pressure.

Summary

Objects live on the shared heap, method frames on per-thread stacks, class metadata in metaspace. Size memory with container-aware flags and know what each OutOfMemoryError message means.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus