Learning path

Full curriculum

Full curriculum

Arrows go from each prerequisite to the units that depend on it. Hover or focus a unit to highlight its path.

Unit content

Tracing and generational garbage collection

A tracing garbage collector periodically discovers reachable objects and reclaims the rest.

A basic mark-and-sweep collector works in two phases:

  1. mark: start from the roots and recursively mark every reachable object;
  2. sweep: scan the heap and reclaim unmarked objects.

A copying collector instead copies live objects from one region to another, leaving garbage behind and compacting survivors as a side effect. A mark-compact collector similarly moves live objects after marking, reducing fragmentation but requiring references to moved objects to be updated.

Many runtimes are generational. They exploit the empirical observation that many allocated objects die young. New objects are collected frequently in a young generation; objects that survive may be promoted to older generations collected less often.

Generational collection introduces a problem: an old object may point to a young object. A write barrier records such cross-generation references so a young-generation collection does not need to scan the entire old heap.

Collectors trade pause time, throughput, memory overhead and implementation complexity. The semantic guarantee is simple—unreachable objects may be reclaimed—but the engineering strategies for discovering and moving them vary substantially.