Skip to content

Generations 0, 1, 2

Context: To improve efficiency, the GC divides the managed heap into three generations: 0, 1, and 2. Newly allocated objects start in generation 0. When a garbage collection occurs, objects that survive are promoted to the next generation. Because most objects die young (e.g., temporary variables), collecting only generation 0 is fast and frequent. Objects that survive multiple collections (e.g., static data, caches) end up in generation 2, which is collected rarely.

GenerationTypical lifetimeCollection frequency
0Very short (local variables)Very frequent
1Buffer (objects that survived Gen0)Less frequent
2Long‑lived (static objects, singletons)Rare
using System;
class Program
{
static void Main()
{
var obj = new object();
Console.WriteLine($"Initial generation: {GC.GetGeneration(obj)}"); // 0
GC.Collect(); // Gen0 collection
Console.WriteLine($"After first collection: {GC.GetGeneration(obj)}"); // 1
GC.Collect(); // Gen1 collection (promotes survivors)
Console.WriteLine($"After second collection: {GC.GetGeneration(obj)}"); // 2
GC.Collect();
Console.WriteLine($"Still in Gen2: {GC.GetGeneration(obj)}"); // 2
}
}
Terminal window
dotnet run
Initial generation: 0
After first collection: 1
After second collection: 2
Still in Gen2: 2
  • Avoid calling GC.Collect() manually – it disrupts GC heuristics.
  • Gen2 objects are rarely collected, so avoid unnecessary long‑lived allocations.

Caching service – Cache entries should be stored with weak references or be carefully designed to avoid promoting too many objects to Gen2, which would increase memory footprint.
See .NET docs on generations.