Confirm it is a leak and not just a high steady state by watching heap size and generation 2 size over time with dotnet-counters. Then capture memory snapshots or a dump at two points and compare which object types are growing and what keeps them alive. Common culprits are unbounded caches, static collections, event handlers that are never unsubscribed, and scoped objects captured by long lived singletons.
Why interviewers ask this
Memory leaks in managed code surprise people who assume the garbage collector makes them impossible. The interviewer wants a methodical approach: confirm the trend, capture evidence, compare snapshots and follow the reference chain to the root. They also want familiarity with the usual causes and the tools, such as dotnet-counters, gcdump and dump analysis. A plausible story about finding a real leak lands far better than a list of tools.
How to structure your answer
- Confirm the growth pattern and rule out normal behaviour.
- Capture snapshots or dumps at two points in time.
- Compare growing types and follow references to the root.
- Fix the cause and add a guard, such as cache limits.
Example answer
First I check it really is a leak. I watch the managed heap and generation 2 size with dotnet-counters over a few hours. If generation 2 keeps rising across full collections, something is holding references. If the managed heap is flat while the process grows, it is probably native memory, a different hunt. For a managed leak I take a gcdump at two points and compare them to see which object types grew. Then I follow the retention path from those objects to whatever root is holding them. The usual culprits are a cache with no size limit or expiry, a static dictionary someone uses as a registry, event handlers attached to a long lived object and never removed, or a scoped object captured by a singleton. On one service the growing type was a request context object, and the path led to a static event that every request subscribed to and never unsubscribed from. One line fixed it. Afterwards we capped every in memory cache and alerted on generation 2 trending upwards.
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
- How would you tell a managed leak from a native memory leak?
- How do you take a memory dump safely from a production container?
- What limits would you put on an in memory cache?
Related .net developer 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