Mark then sweep is a collection. Today you wire them into one Collect call and run it on a graph with a shared node and both kinds of cycle, asserting the exact survivors and reclaimed ids - your first real garbage collection.
Run mark then sweep as a single Collect and assert the exact reclaimed and surviving sets.
Collect is the whole point of the chapter in two lines: mark to blacken the
reachable set, sweep to reclaim the white and reset the survivors. Run it and the
promises from the tracing chapter become actions - the reachable cycle a to b to a
survives because a root reaches it, while the isolated cycle x to y to x is reclaimed
in full despite each object having an incoming reference. That is a working tracing
garbage collector.
Two properties are worth pinning because they guard against subtle bugs. First,
survivors are white after Collect, so a second Collect on an unchanged graph
correctly reclaims nothing rather than misfiring on stale marks. Second, Collect is
idempotent on a stable graph: collecting twice equals collecting once. This is the
chapter’s demoable milestone - a collection you can point at real cyclic data and
trust. The remaining lessons connect it to allocation, so collection happens
automatically when the heap fills instead of only when you ask.
func (h *Heap) Collect() []Ref {h.Mark()return h.Sweep()}// reachable cycle {0,1,2} survives; unreachable cycle {3,4} reclaimed