Java Interview Questions

JVM Memory Model: Heap, Stack, Metaspace Explained

Walk through the JVM memory model. Where does each piece of data live and what OOMs can you hit?

Why Interviewers Ask This

Production Java bugs are often OOMs or leaks. Interviewers expect you to map symptom → area (heap OOM vs metaspace OOM vs stack overflow).

How to Answer

  1. 1

    Name the areas: heap (shared, GC-managed), stack (per-thread, frames), metaspace (class metadata, replaces PermGen in Java 8+), PC register, native stack, direct memory.

  2. 2

    Heap is split into young (Eden + two Survivor) and old. Most objects die young — generational hypothesis.

  3. 3

    Stack holds local variables and frames. StackOverflowError = too-deep recursion. Each thread has its own stack, default ~1MB on Linux x64.

  4. 4

    Metaspace lives in native memory, not heap. "Metaspace OOM" usually means classloader leak (hot reload frameworks, dynamic proxies).

  5. 5

    Direct memory is outside heap. NIO ByteBuffers, Netty allocate here. Can OOM even when heap has room.

Sample Answer

I think about JVM memory by failure mode. Heap OOM means too many live objects or a leak — take a heap dump, analyze with MAT. Metaspace OOM means classloader leak — usually hot-reload or dynamically generated classes. StackOverflowError means recursion too deep, often accidental infinite recursion. DirectBuffer OOM is native memory, heap has room but the OS refuses more. Each thread gets its own stack (~1MB default), so 10K threads eat 10GB before any heap is touched — that's why reactive frameworks push you off thread-per-request.

Pro Tips

  • MaxMetaspaceSize is unlimited by default — set it to fail fast on classloader leaks.

  • Thread stack size is -Xss. Reduce if you need many threads; raise if deep recursion.

  • HireMe AI (即答侠) tracks which JVM OOM type each answer addresses so you never confuse heap and metaspace.

Frequently Asked Questions

Why was PermGen replaced by metaspace?

PermGen had a fixed max size that caused frequent OOMs on app redeploys. Metaspace auto-grows in native memory, and is easier for the JVM to size dynamically.

Is the String pool on heap or metaspace?

On heap since Java 7. Strings interned via String.intern() live in the heap string pool, collected by GC.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free