Course contentsShow
← Computer Science
Lesson 2098 of 2,87247. Memory Management in LanguagesFree lesson

Generational Garbage Collection

Segregate objects by age; collect young generation frequently since most objects die young.

Generational Garbage Collection

What you'll learn: How garbage collectors improve efficiency by separating young objects from old ones and collecting them at different rates.


Most objects in a typical program die young—they're created, used briefly, and become unreachable quickly. Think of temporary variables in a loop or intermediate calculation results. Generational garbage collection exploits this "generational hypothesis" to make memory management dramatically more efficient.

The Core Idea

Instead of treating all heap objects equally, a generational collector divides memory into separate regions by age:

  • Young generation (nursery): Newly allocated objects live here
  • Old generation (tenured space): Objects that have survived multiple collections graduate here

The collector runs frequently on the young generation because most objects there will be garbage. Since this region is small, collection is fast. The old generation is collected much less often because most objects there are long-lived and still in use.

How It Works

When you allocate an object, it starts in the young generation. During a minor collection (young-generation sweep), the collector identifies which young objects are still reachable. Unreachable objects are discarded. Objects that survive are either kept in the young generation or promoted to the old generation after surviving a certain number of collections.

Periodically, a major collection scans the entire heap, including the old generation, but this happens far less frequently.

Why It's Effective

By focusing collection effort where most garbage accumulates, generational collectors avoid repeatedly scanning long-lived objects. This dramatically reduces pause times and improves throughput compared to treating all memory uniformly (as in basic mark-and-sweep or copying collection).

Key Takeaway: Generational garbage collection segregates objects by age and collects the young generation frequently because most objects die young, making memory reclamation much more efficient than whole-heap collection.