http://www.brpreiss.com/books/opus5/html/page424.html
void mark (Object p)Notice that this recursive mark algorithm does nothing when it encounters an object that has already been marked. Consequently, the algorithm is guaranteed to terminate. And it terminates only when all accessible objects have been marked.if (!p.marked)
p.marked = true; for each Object q referenced by p mark (q);
In its second phase, the mark-and-sweep algorithm scans through all the objects in the heap, in order to locate all the unmarked objects. The storage allocated to the unmarked objects is reclaimed during the scan. At the same time, the marked field on every live object is set back to false in preparation for the next invocation of the mark-and-sweep garbage collection algorithm:
void sweep ()for each Object p in the heap
if (p.marked) p.marked = false else heap.release (p);