This is my best attempt at explaining Bartlett's Mostly Copying collection. A few points still elude my understanding. Hopefully these will be clarified with time.The algorithm I am describing is the one used by Bartlett to deal with conservative objects within the heap. His algorithm in the case where all objects in the heap are known is straightforward, and much more efficient than the following. Furthermore he has published results regarding refinements such as generations, and using a mark-compact phase ocassionally to reduce fragmentation. Bartlett breaks his heap up into uniformly sized pages. Associated with each page are two boolean variables: promoted, newSpace. Promoted is set to true whenever its page contains an object that cannot be copied because it is referenced by a conservative pointer. NewSpace is set to true whenever the page is in the new Space(commonly referred to as ToSpace). At the start of GC all pages have both of these variables set to false indicating that none of them are promoted and all reside in oldSpace(i.e. fromSpace). Another important detail to be aware of is, that in this approach all conservative objects reside outside the heap, and are thus not garbage collected. The algorithm uses a Cheney style queue to do the copying. We will refer to it throughout as the Queue. Pages on the Queue are in newSpace, and are not promoted. Recall that enqueueing on a Cheney queue involves copying the object. We break up the algorithm into the following phases:
Phase I | Scan the stack and static area for roots. Promote all pages containing roots. Then Enqueue all the objects on the promoted pages on the Queue, leaving forwarding pointers behind. |
Phase II | Perform copying collecting on the Queue, leaving forwarding pointers for the enqueued objects. But do not update the pointers within the objects. In other words all objects in the Queue point into fromSpace even after they have been scanned. Whenever a pointer outside the heap(i.e. a conservative object) is found, promote all pages pointed to by that object. |
Phase III | Rescan the Queue replacing all the pointers into unpromoted, pages in fromSpace with the forwarding pointers. Pointers into promoted pages are not affected. In this phase collect a linked list of all the promoted pages. |
Phase IV | Restore all the objects back onto promoted pages by tracing their forwarding pointers, and copying them back from newSpace. GC is now complete. Any page not promoted or in newSpace is free. The only task remaining to be done is reset the variables for all the pages that have been promoted or are in newSpace. |
Initially | |
Phase I | |
Phase II | |
Phase III | |
Phase IV |