When V8 compacts or reallocates memory, does it change all the references?

Viewed 30

I am digging deeper into how memory works in V8, and this is something I can't understand.

When object grows or memory is compacted, it means that the address on RAM changes. Does it mean that V8 updates all existing references to point to the new address?

1 Answers

(V8 developer here.)

When V8's garbage collector moves an object (as part of compaction, or promotion to old-space), then it updates all references to that object. It needs to find all these references anyway (in order to determine which objects are still alive), so it remembers the references it found and updates them if it decides to move an object.

An indirection is used for C++ code holding pointers to heap objects: C++ code uses so-called Handles, which are essentially pointers-to-pointers to heap objects, and the intermediate pointer is stored in a special list that the GC knows about and can update. That way, the GC doesn't need to know the decisions that the C++ compiler will make.

We don't support in-place growth of objects, because it would be too expensive to move a single object on-demand (as opposed to as part of a GC cycle). Cases where conceptually objects need to grow (such as adding more properties to them) are handled with another indirection pattern: the object itself holds a pointer to a "backing store" for its properties, and that backing store can be replaced with a bigger one when needed; the object itself doesn't move when that happens, so references to it can stay as they are.

Related