How does the .NET garbage collector work, including generations and the LOH?
Assesses fundamental understanding of C# 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.
.NET uses a generational, compacting, tracing collector. New objects go to generation 0. Survivors are promoted to gen 1 then gen 2. Gen 0 collections are frequent and cheap because most objects die young; gen 2 collections are expensive and mark the whole heap.
Large objects of 85KB or more, such as big arrays, go to the Large Object Heap. It is not compacted by default to avoid moving costs, so it can fragment. ArrayPool<T> avoids repeated large allocations.
var buffer = ArrayPool<byte>.Shared.Rent(1_000_000);
try {
// use buffer
} finally {
ArrayPool<byte>.Shared.Return(buffer);
}
The GC is non-deterministic, so it does not replace IDisposable. Reduce pressure with pooling, Span<T> and structs, and measure with dotnet-counters or GC ETW events before optimising.
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.