Mark and sweep is a technique in GarbageCollection to free all unreferenced objects.
It works with three passes.
-- MarkGrosberg
There is a simple variation of MarkAndSweep that needs to make only one linear sweep. It works simply by reversing the meaning of the mark bit from sweep to sweep. I am also under the impression that in practice marking tends to dominate sweeping, but this of course depends on what percentage of the heap is actually live at GC-time. -- CurtisBartley
I've seen a variant that uses two bits - one bit for even sweeps and the other bit for the odd sweep. The even sweep clears the odd bit, and vice versa. It still takes a second pass to clean out garbage. -- DaveSmith
You can have one linear sweep and only one mark bit. The final sweep unsets the mark bits as it passes through all the objects. See http://www.brpreiss.com/books/opus5/html/page424.html.
-- KarlOkeeffe
If one wishes to provide a way for objects to release their resources when they're about to be removed (what Java calls finalizing), an additional pass must be made; you can't just call the finalizer as you sweep, since if one dead object has a reference to another dead object, and uses it in its finalizer, the process would fail if the referenced object had already been deleted. The finalizing pass is after marking but before sweeping, and simply calls the finalizers of all dead objects. -- MikaelBrockman
I came to this page to look for what MarkAndSweep meant. When I read the descriptions however, it is reminiscent of Clock algorithm for page file replacement used in many operating systems. -- AnandHariharan?
I think it's worth noting that all MarkAndSweep algorithms have an execution time proportional to the size of the entire object space rather than the number of live objects. This (in addition to the need to pause system operation while executing) makes them less attractive than the various semi-space scavenging algorithms that copy live objects from "from space" to "to space". No such pauses are needed, and execution time is proportional to the number of live objects rather than available space. A price paid for this advantage is a factor of two increase in the available memory required.
In the naïve mark-and-sweep method, each object in memory has a flag (typically a single bit) reserved for garbage collection use only. This flag is always cleared, except during the collection cycle. The first stage of collection sweeps the entire 'root set', marking each accessible object as being 'in-use'. All objects transitively accessible from the root set are marked, as well. Finally, each object in memory is again examined; those with the in-use flag still cleared are NotReachable? by any program or data, and their memory is freed. (For objects which are marked in-use, the in-use flag is cleared again, preparing for the next cycle.)