Now, how does the string “hello world” stay alive until this instruction is executed?Something must mark the string object so the garbage collector knows that a reference is being held.

Lovemaking cams-18

I’ve been working on building a compacting garbage collector in Ruby for a while now, and one of the biggest hurdles for implementing a compacting GC is updating references.

For example, if Object A points to Object B, but the compacting GC moves Object B, how do we make sure that Object A points to the new location?

Earlier I wrote that updating references for most objects is fairly straight forward.

Unfortunately there has been one thorn in my side for a while, and that has been Instruction Sequences.

These objects store byte code for your Ruby program, any literals in your code, and some other miscellaneous information about the code that was compiled (source location, coverage info, etc).

Internally, these instruction sequence objects are referred to as “IMEMO” objects.

If the GC know about the internals of some object (for example an Object implemented in a C extension), it doesn’t allow things referred to by that object to move. If the GC doesn’t know how to update the internals of Object A, it won’t allow Object B to move (I call this “pinning” an object).

Of course, the more objects we allow to move, the better.

To update references in the instruction sequences, we have to disassemble the instructions, locate any operands, and update those locations.