I'm reading about garbage collectors implementations, specifically about mark-and-sweep on-the-fly collectors, and the fact that to allow the mutators work when the marking stage isn't finished (and so avoid stopping the world) a memory barrier must be used.
So, for example, if a marked (black) object is modified by the mutator to point to an unmarked (white) object, then the white object must be marked to gray (marked as live but not scanned yet).
But I can't find any detail of how that can be implemented: how can I detect when a black object is changed and perform an action at that moment?
The garbage collector considers unreachable objects garbage and releases the memory allocated for them. During a collection, the garbage collector examines the managed heap, looking for the blocks of address space occupied by unreachable objects.
Garbage collection (GC) is a memory recovery feature built into programming languages such as C# and Java. A GC-enabled programming language includes one or more garbage collectors (GC engines) that automatically free up memory space that has been allocated to objects no longer needed by the program.
A write barrier in a garbage collector is a fragment of code emitted by the compiler immediately before every store operation to ensure that (e.g.) generational invariants are maintained.
Maybe the confusing terminology is to blame. So-called barriers, in garbage collection terminology, are usually snippets of code that the compiler inserts before pointer reads and writes. Your program thus always executes the barrier code before every read and write. (You can also use virtual memory protection to get the effect of barriers.)
To maintain the tricolor invariant you allude to, the collector checks the color of an object before it writes through it. In this case, the write barrier can execute some action whenever it is about to change a black object to a gray one.
See http://www.memorymanagement.org/glossary/s.html#strong.tri-color.invariant for an overview, and this article for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With