liveBook · Manning

Remembered set

As explained in JavaScript in Depth

A collection of references from older-generation objects to younger-generation objects that must be considered during young-generation collection.

A young-generation collector can miss a live object when an older object points to it. A remembered set preserves that cross-generation path without scanning the entire old generation. section 9.2.4

During V8's minor collection, the mechanism works as follows:

  • A write barrier observes property assignments and checks whether the stored reference crosses from an old object to a young object. section 9.2.4
  • The barrier records each relevant old-to-young reference in the remembered set. section 9.2.4
  • The Scavenger traces from ordinary roots and the remembered set, then copies surviving young objects into to-space. section 9.2.2 section 9.2.4
  • The bookkeeping makes assignments to long-lived objects slightly more expensive, with accumulated cost in mutation-heavy code. section 9.2.4

The hidden edge

Suppose an old-generation cache receives a newly allocated young object. Without a remembered set, the Scavenger ignores the cache and could classify that object as garbage. The write barrier records the cache-to-object edge, allowing the next scavenge to retain it. section 9.2.4

Figure 9.4 During a scavenge, V8 traces live objects in from-space and copies them into to-space. Dead objects are never visited. Once copying is complete, the semi-space labels swap: the old from-space is abandoned and the old to-space becomes the new from-space for future allocations.

Visualizes the Scavenger's copying process and shows where remembered-set references supplement ordinary roots. section 9.2.2

How JavaScript in Depth covers it

How deep it goes
Explains write-barrier recording and tracing use, then stops short of barrier implementation details.
Examples
Uses an old object receiving a young reference and contrasts ordinary roots with remembered-set tracing.
What it assumes
Assumes generational objects and roots, while defining the cross-generation hazard and its remedy.
What you can do after
Evaluate how old-object mutations preserve young objects and add garbage-collection bookkeeping cost.
How it connects
Connects generational garbage collection with the Scavenger, write barriers, object properties, and hidden classes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.