How does garbage collection work on the JVM, and how would you decide which collector to use for a service?

technical-conceptual · Mid level · software-engineering

What the interviewer is really asking

Assesses whether the candidate understands generational GC mechanics and can map collector choice to a workload's latency-versus-throughput needs, rather than naming collectors with no reasoning.

What to say

What to avoid

Example answers

Strong: On a low-latency trading-adjacent API with a 32GB heap, G1's pauses were spiking our p99. I moved to Generational ZGC, and GC pauses dropped from tens of milliseconds to under a millisecond, which pulled our tail latency back inside SLA. For a nightly batch job on the same platform I left it on Parallel GC because throughput mattered and pauses didn't.

Weak: The JVM has a garbage collector that cleans up unused objects automatically, so memory management isn't really something you worry about in Java. I'd just use whatever the default is.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.