130 likes | 351 Views
Incremental Garbage Collection. Baker’s Incremental Copying Collector. Nels Beckman nbeckman@scs.cmu.edu. Background. Two forms of memory: From Space: Contains the ‘white’ colored objects. To Space: Contains the ‘black’ and ‘grey’ colored objects. When it’s ‘time to garbage collect’.
E N D
Incremental Garbage Collection Baker’s Incremental Copying Collector Nels Beckman nbeckman@scs.cmu.edu
Background • Two forms of memory: • From Space: Contains the ‘white’ colored objects. • To Space: Contains the ‘black’ and ‘grey’ colored objects.
When it’s ‘time to garbage collect’ • Move the objects referenced by the roots into the ‘to space’ • These roots include registers, global variables, stack, and instruction pointer Root Root To Space From Space
When it’s ‘time to garbage collect’ • Move the objects referenced by the roots into the ‘to space’ • These roots include registers, global variables, stack, and instruction pointer Root Root To Space From Space (Forwardingpointers)
Until All Garbage is Collected • Mutator and Garbage Collector threads alternate
During a GC ‘Turn’ • CG follows references from objects in to-space • Referred Objects are colored grey • When every reference from a grey object is grey, color that object black. Root Root To Space From Space
During a GC ‘Turn’ • CG follows references from objects in to-space • Referred Objects are colored grey • When every reference from a grey object is grey, color that object black. Root Root To Space From Space
During a Mutator ‘Turn’ • If mutator tries to get a reference to a ‘white’ object, that object is copied to the ‘from space.’ • This way, the mutator never has a reference to a white object. Root Root To Space From Space
During a Mutator ‘Turn’ • If mutator tries to get a reference to a ‘white’ object, that object is copied to the ‘from space.’ • This way, the mutator never has a reference to a white object. Root Root To Space From Space
During a Mutator ‘Turn’ • Also, if the mutator allocates an object, this new object goes in the to-space marked black. Root Root To Space From Space malloc()
Afterwards… • When all objects in the to-space are ‘black’, ‘white’ objects can be reclaimed. Root Root To Space From Space
Summary • Baker’s incremental copying algorithm can be used for real-time garbage collection, because GC and mutator threads are intertwined (This algorithm uses a read barrier). • Caveats: • If mutator tries to reference many white objects in a row, performance slows because of all the copying. • Garbage Collection must finish before to-space is filled with newly allocated objects.