Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory barrier implementation for a garbage collector

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?

like image 356
Damian Avatar asked Mar 14 '11 23:03

Damian


People also ask

How does a garbage collector reclaim memory?

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.

What is garbage collection in memory management?

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.

What is a write barrier in GC?

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.


1 Answers

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.

like image 97
EmeryBerger Avatar answered Sep 19 '22 23:09

EmeryBerger