Skip to content
Latchkey

What Is Mark and Sweep?

Mark and sweep is a garbage collection technique that runs in two phases: it first traverses from root references to mark every object still reachable, then sweeps through memory reclaiming all objects left unmarked. Because it follows references, it can collect cyclic structures that reference counting cannot. The trade-off is a collection pass that periodically scans the heap.

Why it matters

Mark and sweep reclaims memory automatically and handles reference cycles, freeing programmers from manual deallocation. Its pauses and scanning cost motivated refinements like generational collection.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →