GARBAGE COLLECTION

Mark the living, sweep the dead

L216 MINSYSTEMS & LOW-LEVEL
PREDICT

A concurrent collector marks reachable objects while the program keeps writing pointers, with no hook on those writes. The marker has already scanned A and will not look at it again (A is black); B is not scanned yet. The program stores a pointer to C into A, then deletes B→C, the only other path to C. What happens at sweep?

A collector has two jobs: never free an object the program can still reach, and eventually free every object it cannot. Reference counting frees an object when its count drops to 0, which never happens inside a cycle. Mark-and-sweep starts at the roots, marks everything it can reach, and frees the rest, so cycles go too.

To shorten pauses, a concurrent marker runs while the program (the mutator) keeps writing pointers. Objects are white (unseen), grey (seen, children pending) or black (done). If the mutator stores a white object into a black one, the marker never looks again. A write barrier closes that hole.

▶ SIMULATION
MUTATOR
roots: A
reachable: A, B, C, D
stores during marking: 0
HEAP
A (root) → B, D · white
B → C · white
C → B · white
D → ∅ · white
COLLECTOR
mode: tracing
phase: idle · grey: —
barrier shades: 0
last—
freed—
✓ no reachable object is freed✓ objects unreachable when a collection starts are reclaimed by its end✓ no black object points to a white one
SCHEDULE
01[CONFIG]config: reference counting (free when rc hits 0)
02[MUTATOR]mutator: drop A→D
03[MUTATOR]mutator: drop A→B
04[COLLECTOR]refcount: no count is at 0, nothing more to free
Start of refcount: cycle leaks scenario. 4 steps.
// concurrent marking, no barrier
void store(Obj* src, Obj** field, Obj* dst) {
  *field = dst;            // marker may have finished src already
}

// reference counting
void release(Obj* o) {
  if (--o->rc == 0) free_and_release_children(o);
}                          // a cycle never reaches 0

Watch the HEAP box and the invariant strip. REFCOUNT leaves B and C marked LEAKED; TRACING frees them. CONCURRENT, NO BARRIER turns the tri-colour check red at the store and frees a reachable C at the sweep. FIX: WRITE BARRIER runs the same interleaving and frees only D.

BREAK IT NOT YET

Heap: root → A; A → B, D; B → C; C → B. No write barrier. Interleave the mutator with the marker so that sweep frees an object the root can still reach.

GOAL: sweep frees an object that is still reachable

FIX IT NOT YET

The mutator calls writeBarrier(src, dst, colours) right before every store src.f = dst while the marker runs. colours maps object id → 'white' | 'grey' | 'black'; the marker scans grey objects, alphabetically. Make sure no reachable object is still white when marking ends, without keeping garbage alive. The grader tries every interleaving of the mutator's stores and deletes with the marker's steps.

Mental model:

liveness is reachability from the roots, not a count. Counts miss cycles; tracing does not. Once the marker runs concurrently, the program can rewire the graph behind its back, so every pointer store has to keep one rule: no black object points to a white one. The write barrier is the code that enforces it on each store.

IN PRODUCTION

CPython's cycle collector (refcounting plus a tracing pass for cycles), Swift and Objective-C weak references, Go's concurrent mark with a hybrid write barrier, the JVM's G1 and ZGC, V8's concurrent marking. Symptoms: slow memory growth from leaked cycles, or rare use-after-free crashes after a GC change.

IN INTERVIEWS

“How does a garbage collector decide what to free?” and “How can marking run while the program is mutating the heap?” Say why refcounting leaks cycles, walk tri-colour marking, show the store-then-delete interleaving that loses an object, and explain how an insertion barrier keeps no black-to-white edge.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: CPU PIPELINE →