Advertisement
Beginner C#, Memory & Garbage Collection

What are .NET's garbage collection 'generations,' and what's the core assumption behind this design?

Short answer

The GC divides objects into Generation 0 (newest, short-lived objects), Generation 1 (a buffer generation), and Generation 2 (long-lived objects) — based on the 'generational hypothesis' that MOST objects die young, so the GC checks Gen 0 frequently and cheaply, only…

Save C# mock interview
Advertisement
AModel answer
The GC divides objects into Generation 0 (newest, short-lived objects), Generation 1 (a buffer generation), and Generation 2 (long-lived objects) — based on the 'generational hypothesis' that MOST objects die young, so the GC checks Gen 0 frequently and cheaply, only occasionally promoting survivors to older generations, which are scanned far less often.
// Conceptual: objects are checked increasingly less often as they survive collections
var temp = new byte[100];    // likely Gen 0, collected quickly if short-lived
static readonly Cache cache = new Cache(); // promoted to Gen 2 over time, rarely re-scanned
Real-world example
Understanding why creating many short-lived temporary objects in a loop is generally cheap, since Gen 0 collections are fast and frequent.
Likely follow-ups
  • What triggers a full Generation 2 collection, and why is it considered much more expensive than a Gen 0 collection?
Related topics