GARBAGE COLLECTION
Mark the living, sweep the dead
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.
// 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// Dijkstra insertion barrier: no black → white edge can appear
void store(Obj* src, Obj** field, Obj* dst) {
if (marking && is_white(dst)) // on every store; some runtimes
shade_grey(dst); // also require is_black(src)
*field = dst;
}
// mark: worklist = roots (grey)
// pop grey g; colour g black; shade each white child grey
// sweep: free every object still white 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.
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
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.
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.
“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.