The .NET collector is generational. New objects go into generation 0, which is collected often and cheaply; survivors are promoted to generations 1 and 2, which are collected less often and cost more. Objects of roughly 85,000 bytes or more go on a separate large object heap. Reduce pressure in hot paths by reusing buffers with ArrayPool, slicing with Span, avoiding boxing and LINQ in tight loops, and measuring first.
Why interviewers ask this
Garbage collection pauses are a common source of tail latency in .NET services, and the interviewer wants to see that you understand why allocation rate matters more than total memory. They listen for the generational model, the large object heap, and server versus workstation modes. Above all they want measurement before optimisation, since most allocation work in normal application code is wasted effort outside genuinely hot paths.
How to structure your answer
- Explain the generational model and why it works.
- Mention the large object heap and server mode.
- Describe how you measure allocation pressure.
- List the techniques you use in hot paths.
Example answer
It is generational, built on the observation that most objects die young. New allocations go into generation 0, which is small and collected often, and because most of it is garbage by then, those collections are quick. Anything that survives is promoted to generation 1 and then 2, which are collected less often and cost more, since there is more to walk. Objects of roughly eighty five thousand bytes or more go straight onto the large object heap, which is collected with generation 2 and not compacted by default, so large temporary arrays are expensive. The number I care about is allocation rate, because that drives how often collections happen. I measure with dotnet-counters for the rate and generation 2 counts, then a profiler to find which code allocates. The fixes are targeted: renting buffers from ArrayPool, using Span to slice strings and arrays without copying, and avoiding boxing and LINQ in tight loops. On one parsing service that cut generation 2 collections to a fraction of what they had been.
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 server and workstation garbage collection?
- When would using a struct to avoid allocations backfire?
- How would you find which code is allocating the most?
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