Interview questions
Explain the JVM's memory areas.
How I'd answer:
"Shared by all threads: the heap, where every object and array lives and which the garbage collector manages, divided into young — Eden and survivor spaces — and old
generations; and metaspace, in native memory since Java 8, holding class metadata like method bytecode and the constant pool. Per thread: the JVM stack of frames, each
with local variables — primitives and references — an operand stack and a return address, which overflows as StackOverflowError; the PC register pointing at the
current bytecode instruction; and a native method stack. There's also the code cache for JIT-compiled machine code. Heap exhaustion gives
OutOfMemoryError: Java heap space, metaspace exhaustion OutOfMemoryError: Metaspace."
Combines: Ch 3, 6.
How does garbage collection work in Java?
How I'd answer: "The JVM traces reachability: starting from GC roots — local variables in active stack frames, static fields, active threads, JNI references — it marks every object it can reach, and everything else is garbage. That handles reference cycles, unlike pure reference counting. Because most objects die young, the heap is generational: new objects go to Eden, and a minor GC copies the few survivors into a survivor space, ageing them, promoting long-lived ones to the old generation. Young collections are cheap because their cost depends on live objects — I've seen G1 go from 153 MB to 1 MB in under a millisecond. Old-generation collection uses marking with compaction or evacuation. Collectors differ in how much work they do concurrently versus in stop-the-world pauses: G1, the default, works region by region within a pause target; ZGC does almost everything concurrently for sub-millisecond pauses."
Combines: Ch 5–8.
G1 vs ZGC vs Parallel — how do you choose?
How I'd answer: "Parallel GC is stop-the-world with many threads, maximising throughput — good for batch jobs where pauses don't matter. G1 is the default: it divides the heap into regions, marks concurrently, and collects the regions with the most garbage first to meet a pause-time goal, 200 ms by default — good for most services. ZGC does marking and compaction concurrently using load barriers, giving sub-millisecond pauses even with very large heaps, at the cost of some CPU and needing headroom — if the heap is too small you get allocation stalls. I'd start with G1, look at GC logs, and move latency-sensitive services with visible pause problems to generational ZGC on Java 21. And before changing collectors I'd try reducing allocation."
Combines: Ch 7, 8.
Your Java service in Kubernetes keeps getting OOMKilled, but you never see an OutOfMemoryError. Why?
How I'd answer:
"OOMKilled with exit code 137 means the kernel killed the whole process for exceeding the container's memory limit. That's different from a Java heap OOM, which the JVM
reports itself. The JVM's total footprint is the heap plus metaspace, thread stacks — about a megabyte each — the code cache, GC structures and direct buffers. If -Xmx
is set close to the container limit, or there are thousands of threads, the total exceeds the limit. I'd size the heap as a percentage with -XX:MaxRAMPercentage
around 70–75 so there's room for non-heap memory, check thread counts and native memory — -XX:NativeMemoryTracking helps — and make sure the JVM version is
container-aware. If the heap itself were leaking, I'd expect a Java OutOfMemoryError and would take a heap dump."
Combines: Ch 3, 10; Operating Systems Ch 5.
How would you find a memory leak in a Java application?
How I'd answer:
"First confirm it's a leak and not just a big working set: in the GC logs or jstat, the heap occupancy after full or mixed collections keeps climbing over time. Then
take a heap dump — ideally automatically via -XX:+HeapDumpOnOutOfMemoryError, or with jcmd GC.heap_dump — and open it in Eclipse MAT. The dominator tree and leak
suspects report show which objects retain the most memory; then I look at their path to GC roots to see what's keeping them reachable. Common culprits are static maps
used as unbounded caches, listeners or callbacks never deregistered, ThreadLocals not cleared in pooled threads, and queues without consumers. The fix is to remove the
unintended reference — for caches, a bounded cache with eviction. Java Flight Recorder helps too, showing allocation hot spots with low overhead."
Combines: Ch 5, 10.