How does the Java memory model and garbage collection work?
Assesses fundamental understanding of Java conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
Memory is divided into heap (shared objects) and per-thread stacks (frames, locals). The heap has a young generation (Eden plus two survivor spaces) and an old generation.
GC roots (stack references, statics, JNI references) mark reachable objects; everything else is collectible. Minor GC copies surviving young objects between survivor spaces and promotes long-lived ones to old gen. Major/full GC handles old gen and can pause the application.
Collectors: Serial, Parallel, G1 (default in recent JDKs, region-based with pause targets), ZGC and Shenandoah for very low pause times. Tuning knobs include heap size (-Xmx), pause goals (-XX:MaxGCPauseMillis) and region size. Diagnose with jstat, GC logs, JFR and heap dumps.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.