Explain the .NET Garbage Collector — what are the generations and what triggers a Gen 2 collection?
cs-sen-001
Your answer
Answer as you would in a real interview — explain your thinking, not just the conclusion.
Model answer
The .NET GC uses a generational model based on the observation that most objects die young. New allocations go to Gen 0, which collects frequently and cheaply. Survivors promote to Gen 1. Gen 2 holds long-lived objects and the Large Object Heap (LOH, objects >= 85 KB). A Gen 2 collection is a full GC — it scans the entire managed heap and pauses all threads (stop-the-world in the default workstation GC). It triggers when Gen 2's budget is exhausted or the OS signals memory pressure. To avoid frequent full GCs: pool large objects with ArrayPool/MemoryPool, avoid excessive allocations in hot paths, use struct-based value types, and prefer server GC mode in ASP.NET Core which allocates one heap per logical CPU.
Follow-up
What is the difference between workstation GC and server GC modes? How does background GC change the pause story?