What they're testing
Whether you've ever read a GC log. It's the foundation for every OutOfMemoryError and pause-time question.
The short answer~30 seconds
The heap holds objects, split into a young generation (eden plus two survivor spaces) and an old generation. Each thread has its own stack for frames and primitives. Metaspace holds class metadata and lives in native memory since Java 8 — replacing PermGen. Generational GC rests on the observation that most objects die young: collect eden often and cheaply, promote whatever survives enough passes into the old generation, which is collected less often and more expensively.
The heap splits into a young generation (eden plus two survivor spaces) and an old generation. The layout rests on one observation: most objects die young.
The long answer
The two survivor spaces exist for compaction: a minor GC copies live objects out of eden and the in-use survivor into the other survivor, then wipes both source regions at once. Because the operation is a copy rather than a mark-and-sweep, the cost scales with how much SURVIVES, not with how much garbage there is. Which is why allocating many short-lived objects is nearly free on the JVM — deeply counter-intuitive coming from C++.
G1 (the default since Java 9) shifts the model slightly: it divides the heap into thousands of equal regions, each acting as eden, survivor or old at any moment. That lets it collect the regions with the MOST GARBAGE first — "garbage first" — and target a pause time you specify (-XX:MaxGCPauseMillis). For very large heaps, ZGC or Shenandoah fit better, holding sub-millisecond pauses regardless of heap size in exchange for extra CPU and memory.
The practical thing to remember when debugging: OutOfMemoryError: Java heap space means the heap is exhausted, usually a leak or an undersized heap; Metaspace means too many classes loaded, the classic sign of repeated redeploys into one JVM or dynamic class generation; unable to create native thread has nothing to do with the heap and means native memory or an OS process limit. Three errors, three different investigations.
What they'll ask next
?How do a stack overflow and a heap exhaustion differ?
StackOverflowError is a call chain too deep — usually unbounded recursion — bounded by -Xss per thread. OutOfMemoryError means no room to allocate in the heap. The first is fixed by changing the algorithm, the second by finding what's retaining memory.
?Which collector for a latency-sensitive web service?
G1 is a sensible default up to tens of gigabytes. If p99 is dominated by pauses, ZGC is worth trying, since its pauses are nearly independent of heap size. But before switching collectors, measure whether pauses really are the cause — very often the culprit is I/O waiting, not GC.
These lose points
- Referring to PermGen as current. It was removed in Java 8 and replaced by Metaspace.
- Saying "call
System.gc()to free memory". It's only a hint, and on real systems it usually causes an unnecessary long pause.