A garbage collector finds objects still reachable from roots such as the stack and globals, and reclaims everything else. Most modern collectors are generational, built on the observation that most objects die young, so they collect a small nursery often and the old generation rarely. It becomes a problem when pause times eat your latency budget, when allocation outruns collection, or when you leak by holding references you forgot about.
Why interviewers ask this
Managed runtimes hide memory until they do not, and the interviewer wants to know you can handle that day. They are checking that you understand reachability rather than reference counting folklore, that you know generational collection is why short lived allocation is cheap, and that you can name the symptoms of GC pressure: rising pause times, heap occupancy climbing after every collection, throughput falling under load.
How to structure your answer
- Explain reachability from roots, not reference counts.
- Describe generational collection and why the assumption holds.
- Name the failure modes: pauses, allocation rate, leaks.
- Say how you would diagnose it with real tooling.
Example answer
The mental model is reachability. The collector starts at the roots, the stack, registers and globals, walks everything reachable, and anything it never reached is garbage. Most collectors are generational because the vast majority of objects die almost immediately, so you scan a small young space often and touch the old space rarely. That is why allocating a short lived object is close to free, and why a long lived cache is the expensive thing. It turns into a problem in three ways. Pauses, if a stop the world collection lands inside your p99 budget. Allocation rate, where you produce garbage faster than the collector reclaims it and throughput collapses. And leaks, which in a managed runtime always mean something still holds a reference, usually a static map or a listener nobody removed. When I have chased this I start with GC logs and watch heap occupancy after each collection. If that line trends up over hours it is a leak, and then it is a heap dump to find the holder.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What is the difference between a leak and just high memory use?
- How would you reduce allocation in a hot path?
- When would you tune the heap size instead of the code?
Related software engineer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions