| OCR Text |
Show 54 and compacted at the same time. At the end, ToSpace contains all accessible objects and the roles of ToSpace and FromSpace are reversed. To describe a copying garbage collection algorithm, objects can be imagined as having one of the four colors: white, blank, grey and black. White are objects in FromSpace that have not been copied by the collector. Blank are objects in FromSpace that have been already copied by the collector. A blank object contains a forwarding pointer to its copy with a real value in ToSpace. Grey are objects in ToSpace that may reference objects in FromSpace and ToSpace. Black are objects in ToSpace that reference only objects in ToSpace. At the beginning, all objects are white and ToSpace is empty. Objects are copied by the process called scavenging. A scavenge performs a breadth first search traversal of accessible objects using objects in ToSpace as a queue. Scavenging achieves copying, compaction and pointer forwarding in one pass through objects. Scavenging starts by copying root objects. These are objects accessible from static data structures, internal registers, or stack. Root white objects are copied to allocated space in ToSpace. After a white object is copied, the original copy of the object in FromSpace becomes a blank object with a forwarding pointer and the new copy in ToSpace is a grey object. After all root objects are copied, the collector starts traversing grey objects in ToSpace. During the traversal of a grey object, its pointers are checked. Pointers to blank objects are altered to point to objects in ToSpace. If a pointer points to a white object, then the object is copied to a new grey object in ToSpace and the forwarding pointer is placed in the old copy which is now a blank object. This process of traversal turns a grey object into a black object. After all grey objects are traversed, there are only black objects left and FromSpace and ToSpace can be reversed. This process of scavenging can be implemented efficiently, provided that the mutator checks each pointer reference for a forwarding pointer. |