Labs › runtimes

Build Your Own Garbage Collector

Build a garbage collector from stop-the-world mark-sweep to generational GC to tri-color incremental marking — then map each algorithm directly to G1GC, ZGC, and Shenandoah.

rust 1.83 extreme ~14h build · ~4h read ~1000 LoC (v3)
v0: Stop-the-World Mark-Sweepv1: Generational GCv2: Tri-Color Incremental Marking
☕ Java/Spring integration included jvm perspective

Why this lab exists

Every managed runtime you use daily — the JVM, the .NET CLR, V8, CPython with its cyclic GC — has a garbage collector at its heart. The GC is why Java developers rarely think about free(), why Python handles circular references without leaking, why Go programs don’t need manual memory management despite being systems code.

Yet “how does a GC work” is one of those questions where the honest answer is “it depends on which GC” — and the variation between algorithms is enormous. A stop-the-world mark-sweep GC and ZGC are both garbage collectors, but their pause characteristics differ by three orders of magnitude.

This lab builds three GC algorithms from scratch in Rust, each a meaningful step beyond the last. By the end, you will be able to read the JVM GC tuning flags (-XX:+UseG1GC, -XX:+UseZGC) and understand precisely which algorithmic choices those flags select — and why each trade-off exists.

What we’re building

A garbage collector library in Rust, built across three stages.

We build:

  • v0: stop-the-world mark-sweep GC with DFS mark phase and free-list sweep
  • v1: two-generation GC (nursery + tenured) with write barrier and remembered set
  • v2: tri-color incremental marking with the Dijkstra write barrier, bounded-pause step(n)
  • A Java integration showing G1GC, ZGC, and Shenandoah mapped to our implementations

We explicitly skip (and why):

  • Moving/compacting GC: requires updating every pointer — needs precise stack map info we don’t have
  • Concurrent evacuation: Shenandoah-style — Brooks forwarding pointers are a full lab on their own
  • Precise stack scanning: Rust’s ownership model already prevents dangling refs; our “roots” are manually registered handles, not real stack scanning
  • Reference counting: covered in WhatTheToyMisses — a separate algorithm family

Mental model

A GC answers one question: which objects are reachable from the program?

Everything reachable from the program’s current state (local variables, global variables, registers) is a root. Everything transitively reachable from a root must be kept alive. Everything else is garbage.

The simplest way to answer “what’s reachable?” is to walk the object graph starting from roots — a DFS or BFS. Any object the walk touches is alive. Any object not touched is dead.

Roots → [A] → [B] → [D]
         ↓
        [C]          [E]  ← unreachable: garbage

Mark traversal visits A, B, C, D.
Sweep frees E.

The three stages in this lab differ not in what they collect but in when and how incrementally they do it:

  • v0 marks and sweeps the entire heap atomically (stop-the-world)
  • v1 marks only the young generation most of the time (minor GC)
  • v2 interleaves mark work with the running program (incremental)

Each step trades implementation complexity for lower maximum pause time.

Prerequisites

  • Memory management basics: what heap allocation is, why free() matters, what a dangling pointer is
  • Rust ownership model: understanding why Rust prevents dangling pointers (which is why we need an explicit GC for managed-language semantics)
  • Graph traversal: DFS from a starting node — this is the entire mark phase

Stage-by-stage build

Stage 0 Stop-the-World Mark-Sweep ~250 LoC ›
  • Implement GcHandle (usize index), GcValue {data, refs}, GcObject {value, marked}
  • Implement Heap {objects: Vec<Option<GcObject>>, free_list, roots}
  • alloc(): pop from free_list or push to objects vec
  • collect(): mark phase (DFS from roots) + sweep phase (free unmarked, clear marks)
  • 8 tests: alloc, reference following, mark-sweep frees unreachable, root protection, circular ref collected, free list reuse, stats, remove_root

The entire GC can be described in two loops:

// Mark phase: DFS from each root
for root in roots:
    worklist.push(root)
    while not empty:
        obj = worklist.pop()
        if not obj.marked:
            obj.marked = true
            worklist.extend(obj.refs)

// Sweep phase: free unmarked objects
for (i, obj) in objects.iter().enumerate():
    if obj.is_some() and not obj.marked:
        objects[i] = None      // free
        free_list.push(i)      // slot available for reuse
    elif obj.is_some():
        obj.marked = false     // clear for next cycle

The key insight is why circular references don’t leak with mark-sweep. Consider two objects that reference each other but are reachable from no root:

[A] → [B]
[B] → [A]
      (no root points to A or B)

Reference counting would never free these: refcount(A) = 1 (B holds it), refcount(B) = 1 (A holds it). Both counters are nonzero, so neither is freed. CPython has a separate cycle detector (in gc module) specifically because its reference counting cannot handle cycles.

Mark-sweep has no such problem. The mark phase starts from roots. If no root reaches A or B, neither is ever marked. Both are freed in the sweep phase. Cycles are irrelevant to reachability from roots.

The free list: when sweep frees slot i, it pushes i to free_list. The next alloc() pops from free_list first before growing the objects vec. This gives O(1) amortized allocation: the same slots are reused without ever calling the allocator.

pub fn alloc(&mut self, value: GcValue) -> GcHandle {
    let obj = GcObject { value, marked: false };
    if let Some(idx) = self.free_list.pop() {
        self.objects[idx] = Some(obj);  // reuse freed slot
        idx
    } else {
        let idx = self.objects.len();
        self.objects.push(Some(obj));   // grow the heap
        idx
    }
}

JVM correlation: The JVM’s Serial GC (-XX:+UseSerialGC) is our v0: stop-the-world, mark all live objects, sweep dead ones, compact (which we skip). Serial GC is still used for small heaps (embedded devices, batch jobs) where predictable throughput matters more than pause time.

Stage 1 Generational GC ~400 LoC ›
  • Split heap into nursery (GenHeap) and tenured (GenHeap)
  • minor_gc(): collect nursery only — mark from nursery roots + remembered set
  • major_gc(): collect nursery then tenured
  • write_barrier_tenured_to_nursery(): update ref + add to remembered_set
  • Promote nursery objects that survive PROMOTION_THRESHOLD (2) minor GCs
  • 6 tests: nursery-only, tenured untouched in minor, write barrier, promotion, major GC, stats

The generational hypothesis is an empirical observation validated across decades of workloads: most objects die young. A string built to format a log line, a request object created to handle an HTTP request, a temporary buffer used during JSON parsing — all are allocated and immediately become garbage.

This observation motivates splitting the heap:

Nursery (generation 0):  new allocations land here
                         collected frequently (minor GC)
                         most objects die here

Tenured (generation 1):  objects that survived N minor GCs
                         collected rarely (major GC)
                         contains long-lived state

Minor GC collects only the nursery. Because most nursery objects are already dead by the time GC runs, the live set is tiny, and the entire minor GC completes quickly — often in under 1ms for small heaps.

The write barrier problem: minor GC only traces the nursery. But a tenured object can hold a reference to a nursery object. If we ignore the tenured generation during minor GC, we might free a nursery object that is still referenced from tenured space.

Solution: the write barrier. Whenever a tenured object’s refs field is updated to include a nursery handle, we record the tenured object in the remembered_set. During minor GC, the remembered set becomes additional roots:

pub fn write_barrier_tenured_to_nursery(
    &mut self,
    tenured_handle: GcHandle,
    nursery_handle: GcHandle,
) {
    // Update the ref.
    if let Some(obj) = self.tenured.get_mut(tenured_handle) {
        obj.value.refs.push(nursery_handle);
    }
    // Record in remembered set so minor GC doesn't miss this nursery object.
    if !self.remembered_set.contains(&tenured_handle) {
        self.remembered_set.push(tenured_handle);
    }
}

Minor GC then marks from two root sets: explicit nursery roots, and all nursery objects referenced by any entry in the remembered set.

Promotion: objects that survive PROMOTION_THRESHOLD minor GCs are copied to the tenured heap and their nursery slot is freed. The assumption: if an object has survived this many collections, it’s probably long-lived.

JVM correlation: G1GC (-XX:+UseG1GC) is our v1, but region-based. G1 divides the heap into equal-sized regions (1–32 MB). Each region is tagged as Eden, Survivor, or Old. Minor GC collects Eden + Survivor regions. Mixed GC collects Eden + Survivor + a few Old regions. The card table replaces our Vec-based remembered set: the heap is divided into 512-byte cards; a write barrier marks the card dirty. At minor GC time, only dirty cards are scanned.

Stage 2 Tri-Color Incremental Marking ~350 LoC ›
  • Add Color enum: White (unvisited), Grey (in worklist), Black (fully scanned)
  • begin(): shade all roots grey, add to worklist, set gc_in_progress = true
  • step(n): pop n objects from worklist, shade their refs grey, color them black
  • finish(): sweep white objects, return freed count
  • write_ref(): Dijkstra barrier — if source is black and target is white, shade target grey
  • 5 tests: incremental convergence, write barrier protection, bounded step, invariant across mutations, concurrent mutator

Stop-the-world GC pause time scales with heap size. A 1 GB heap with 500 MB of live objects requires 500 MB of mark work before any mutator thread can resume. For latency-sensitive applications — web servers, real-time systems — pauses measured in hundreds of milliseconds are unacceptable.

Incremental GC breaks the mark phase into small steps. Between steps, the mutator runs. The goal is to bound the pause time independently of heap size.

The tri-color invariant makes this safe:

White:  not yet visited — candidate for collection
Grey:   visited but refs not yet scanned — in the worklist
Black:  fully scanned — all refs have been made grey

Strong invariant: no black object may reference a white object.

If this holds when the worklist empties, all remaining white objects
are unreachable and can be safely freed.

The critical problem: while the GC pauses between steps, the mutator continues to create and update references. A mutator could store a white object into a black object’s refs field, violating the invariant. The black object won’t be scanned again, so the white object won’t be marked — and will be incorrectly freed.

Dijkstra write barrier: whenever the mutator writes a reference, check: if the source is black and the target is white, shade the target grey immediately.

pub fn write_ref(&mut self, from: GcHandle, to: GcHandle) {
    // Update the reference.
    if let Some(obj) = self.objects.get_mut(from).and_then(|o| o.as_mut()) {
        obj.value.refs.push(to);
    }
    if !self.gc_in_progress { return; }

    self.barrier_checks += 1;

    let from_black = /* check color of from */;
    let to_white   = /* check color of to   */;

    if from_black && to_white {
        // Shade to grey — maintains the invariant.
        shade_grey(to);
        self.barrier_fires += 1;
    }
}

The step function: bounded work per pause.

pub fn step(&mut self, n: usize) -> usize {
    for _ in 0..n {
        let Some(handle) = self.worklist.pop() else { break };
        let refs = /* get refs of handle */;
        for r in refs {
            self.shade_grey(r); // grey unvisited refs
        }
        // Mark handle black — fully processed.
        if let Some(obj) = self.objects.get_mut(handle)... {
            obj.color = Color::Black;
        }
    }
    self.worklist.len() // remaining grey objects
}

Processing 100 objects per step takes roughly 100µs. The mutator gets 100µs of pause, regardless of whether the heap is 1 MB or 1 GB.

New allocations during GC: when an object is allocated while a GC cycle is in progress, it must not be swept as white garbage in the current cycle — it was just created. We allocate it grey and add it to the worklist.

JVM correlation: ZGC (-XX:+UseZGC) is our v2, with significant extensions. ZGC encodes color information in the pointer itself (bits 42–45 of a 64-bit address). Instead of a worklist, ZGC uses load barriers: every heap dereference checks the pointer’s color bits. If the bits indicate the pointer is “stale” (GC has moved the object or changed its mark state), the load barrier heals the pointer before returning it. This enables fully concurrent marking without stopping the world at all, except for brief safepoints to scan thread stacks.

Run it yourself

Prereqs: Rust 1.83+cargo
  1. 1
    Clone and build
    git clone https://github.com/pushkarkumar-dev/system-design-labs
    cd system-design-labs/labs/garbage-collector
    cargo build
  2. 2
    Run the demo (all three stages)
    cargo run
  3. 3
    Run all tests
    cargo test
  4. 4
    Run benchmarks
    cargo bench
Expected output
=== Garbage Collector Lab — Full Demo ===

--- v0: Stop-the-World Mark-Sweep ---
Before GC: 6 objects
After GC:  4 objects
Freed:     2
root alive: true
c alive:    true
garbage-1:  true

--- v1: Generational GC (nursery + tenured) ---
Initial: nursery=3 tenured=1
After minor GC 1: nursery=1 tenured=1
After minor GC 2 (promotion): nursery=0 tenured=2
Remembered set size: 1
After major GC: nursery=0 tenured=3
Minor collections: 3
Major collections: 1

--- v2: Tri-Color Incremental Marking ---
Before GC: 10 objects
After GC:  4 objects
Freed:     6
Steps:     4 (2 objects per step)
root alive: true
c alive:    true

Write barrier stats:
Barrier checks: 1
Barrier fires:  1 (actual shading events)
Completed GC cycles: 2
Total freed: 6

Benchmarks

Benchmark results on M2 MacBook Pro, 16GB RAM (estimated — run cargo bench for your numbers)
Metric Result Notes
Stop-the-world mark-sweep (1000 live objects) 2.1 ms pause DFS mark + full sweep; pause scales with live set
Minor GC (nursery only, 100 live objects) 0.18 ms pause ~10x faster — traces only nursery, not tenured
Incremental step (100 objects) 0.09 ms pause Bounded by n — heap size doesn't change this number
Alloc throughput (free list reuse) 45,000,000 allocs/sec Vec index write — no system allocator involved

Stress tests

Circular reference collection: allocate 1000 pairs of objects where each pair forms a cycle (A→B, B→A). Add no roots. Run collect(). All 2000 objects should be freed. Reference counting would leak all 2000. Mark-sweep handles cycles correctly because reachability from roots is the only criterion.

Write barrier correctness under load: in v2, run 1000 mutations that set black_obj.refs[i] = white_obj during an active GC cycle. Each mutation should trigger the Dijkstra barrier. After finish(), verify that zero reachable objects were collected. This is the hardest correctness property to test — a missing barrier is silent data corruption.

Incremental convergence: allocate 10,000 objects, root 100 of them, begin a GC cycle, and call step(10) in a loop until worklist.len() == 0. Then call finish(). Verify that exactly 100 objects remain and the rest were freed. Incremental GC must converge to the same answer as stop-the-world.

Promotion storm: allocate 500 objects per minor GC cycle for 10 cycles, rooting all of them. By cycle 2 (our threshold), the first batch should be promoted to tenured. By cycle 10, the tenured heap should hold the equivalent of 8 batches. Run a major GC with no tenured roots — all should be freed.

What the toy misses

What the toy misses Honest gaps vs. production
  • Precise vs conservative GC Our GC knows exactly which words are pointers (the refs field). Conservative GC (Boehm GC, used by some C++ runtimes) treats any word that looks like a valid heap address as a pointer. It cannot move objects because it might accidentally update a non-pointer that happens to have a pointer-shaped value.
  • Moving/compacting GC Our sweep leaves freed holes in the heap. Compacting GC moves live objects together to eliminate fragmentation. This requires updating every pointer to each moved object — which in turn requires precise type information (knowing exactly where pointers are in each object). G1GC compacts; our toy does not.
  • Finalization and weak references Java's finalize() and WeakReference require the GC to track and invoke finalizers before collecting objects. The GC must do a second pass after marking: objects with finalizers that would be freed are instead queued for finalization, temporarily resurrecting them. WeakReference clears its referent during sweep but does not prevent collection.
  • Concurrent evacuation (like Shenandoah) Our v2 marks concurrently but stops the world to evacuate (copy) live objects. Shenandoah evacuates concurrently using Brooks forwarding pointers: every object has an extra header word pointing to its current location. The mutator dereferences through this pointer. During concurrent evacuation, the old and new copies coexist briefly.
  • Card table for write barriers Our remembered set is a Vec of GcHandles — O(references) space. G1GC uses a card table: the heap is divided into 512-byte cards, and a write barrier marks the corresponding card byte dirty. At minor GC time, only dirty cards are scanned for cross-generation references — O(dirty cards), not O(all tenured objects).
  • NUMA-aware allocation Multi-socket servers have non-uniform memory access times: accessing memory on a remote NUMA node takes 2-3x longer than local memory. GC allocators partition the heap by NUMA node and prefer allocating objects on the node where the allocating thread is running. Our toy allocates into a single Vec with no NUMA awareness.

Wiring it into a Java/Spring service

☕

Wiring it into a Java/Spring service

jvm perspective Spring Boot 3.3 / Java 17
Maven dependencies
org.springframework.boot:spring-boot-starter-actuator:3.3.0

Part A — Mapping our toy to real JVM GC algorithms

Each stage of our lab corresponds directly to a JVM GC algorithm:

Our implementationJVM flagKey characteristic
v0 mark-sweep-XX:+UseSerialGCStop the world, no generations
v1 generational-XX:+UseG1GCYoung/old regions, card table
v2 tri-color-XX:+UseZGCConcurrent marking, colored pointers
Beyond v2-XX:+UseShenandoahGCConcurrent evacuation

Run the demo with each flag to observe how the GC name changes in the output:

# Serial GC — our v0 equivalent
java -XX:+UseSerialGC -jar target/garbage-collector-spring-0.1.0.jar

# G1GC (default since JDK 9) — our v1 equivalent
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -jar target/garbage-collector-spring-0.1.0.jar

# ZGC — our v2 equivalent, with colored pointer extensions
java -XX:+UseZGC -Xmx512m -jar target/garbage-collector-spring-0.1.0.jar

# Shenandoah — concurrent evacuation, beyond our v2
java -XX:+UseShenandoahGC -jar target/garbage-collector-spring-0.1.0.jar

Part B — G1GC regions vs. our v1 nursery/tenured split

G1GC divides the heap into equal-sized regions (1–32 MB each). Each region is tagged as Eden, Survivor, or Old:

G1 heap: [E][E][S][O][O][E][S][H][O][E]
          ↑       ↑   ↑           ↑
        Eden  Survivor Old      Humongous

Our v1:
  nursery = { E, S regions } — collected by minor_gc()
  tenured = { O regions }    — collected by major_gc()

The critical difference: G1GC’s regions are relocatable. After collecting a region, live objects are copied to a fresh region. This eliminates fragmentation. Our v1 reuses freed slots via free_list — no compaction, so fragmentation accumulates over time.

Part C — ZGC colored pointers vs. our v2 Color enum

Our v2 stores color as a Color enum field (1 byte per object). ZGC encodes color in the pointer itself using unused bits of a 64-bit address:

64-bit ZGC pointer layout:
  [unused 18 bits][Marked1][Marked0][Remapped][Finalizable][object address 42 bits]

Our v2 Color enum:
  White  → pointer bits don't match current mark epoch
  Grey   → in worklist
  Black  → bits match mark epoch, fully scanned

ZGC’s load barrier checks these bits on every heap dereference. If the bits are stale, the barrier heals the pointer in-place. This removes the need for a separate worklist: the “grey” state is implicit in the pointer metadata.

Cost: colored pointers consume 4 bits of the 64-bit address space, limiting ZGC heaps to 4 TB. Our enum wastes a full byte per object but imposes no heap size limit.

Part D — Recording GC events with JMX and JFR

// JMX MXBean approach (available in all JVMs, no flags needed):
List<GarbageCollectorMXBean> beans =
    ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean bean : beans) {
    System.out.printf("GC: %s  collections: %d  time: %dms%n",
        bean.getName(),
        bean.getCollectionCount(),
        bean.getCollectionTime());
}

// JFR approach (JDK 9+, more detail):
// Recording recording = new Recording();
// recording.enable("jdk.GCHeapSummary").withThreshold(Duration.ofMillis(0));
// recording.enable("jdk.GarbageCollection").withThreshold(Duration.ofMillis(0));
// recording.start();
// ... run workload ...
// recording.stop();
// recording.dump(Path.of("gc-events.jfr"));
// // Inspect: jfr print --events jdk.GCHeapSummary gc-events.jfr

Both approaches let you observe what our GcStats struct tracks: collection count and total pause time. JFR adds heap size before/after each collection, which directly maps to our objects_before / objects_after stats.

Run the Java demo

cd labs/garbage-collector/java-integration
mvn spring-boot:run

Expected output:

=== Garbage Collector Lab — JVM Perspective ===

--- Active JVM Garbage Collectors ---
  GC name: G1 Young Generation          collections: 0  time: 0ms
  GC name: G1 Old Generation            collections: 0  time: 0ms

  Mapping to our toy implementations:
  SerialGC      → v0 stop-the-world mark-sweep
  G1GC          → v1 generational (young/old regions, incremental mixed GC)
  ZGC           → v2 tri-color concurrent marking, colored load barriers
  ShenandoahGC  → beyond v2: concurrent evacuation via Brooks pointers

--- G1GC Region Model vs. Our v1 Generational Heap ---
  ...

--- ZGC Colored Pointers vs. Our v2 Tri-Color ---
  ...

--- GC Observability via JMX MXBeans ---
  Allocations: 50,000 x 512 bytes = 24,414 KB
  GC collections triggered: 2
  Total GC pause time: 3ms
  Heap used after: 12,288 KB

What surprised me

Where to go deeper

  • The Garbage Collection Handbook (Jones, Hosking, Moss — 2011): the definitive reference covering every algorithm family with pseudocode and proofs
  • ZGC design document: OpenJDK wiki with the colored pointer design and load barrier protocol — wiki.openjdk.org/display/zgc
  • Shenandoah GC paper (2016): the ACM paper describing concurrent evacuation with Brooks pointers — dl.acm.org/doi/10.1145/2972206.2972210
  • V8 Orinoco GC blog post: how Chrome’s JS engine moved from stop-the-world to incremental+concurrent — v8.dev/blog/trash-talk
  • Compiler companion: Bytecode VM (lab #14) — the VM executes the code that creates objects the GC must track; see “GC” in WhatTheToyMisses of that lab
  • Memory layout: B-Tree KV (lab #2) — arena allocation in B-trees is GC avoidance, not GC: allocate from a slab, free the whole slab at once
  • Distributed angle: how does a GC interact with distributed tracing? A pause of 200ms looks like a network timeout in distributed systems — GC pauses are a common source of false positives in circuit breakers
  • Design question: why does Go use a tri-color concurrent GC with a write barrier but not a generational GC? (Answer: Go has stack-allocated objects and goroutine stacks are scanned precisely; the write barrier cost to maintain a remembered set was measured to outweigh the benefit of generational collection for Go’s typical workloads)