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
What Is Generational Garbage Collection?Generational garbage collection groups objects by age and collects short-lived young objects more often than…
What Is Reference Counting?Reference counting tracks how many references point to an object and frees it automatically when that count d…
What Is a Weak Reference?A weak reference points to an object without keeping it alive, so the object can still be collected and the r…