Suppose a long-lived object acquires a reference to a newly allocated object. A young-generation collector that scans only young objects could miss that path and reclaim the new object incorrectly. A write barrier prevents this by doing collector bookkeeping when the reference is stored. section 9.2.4
In V8, a store from an old object to a young object is recorded in a remembered set. The Scavenger treats those recorded references as additional tracing roots, so it can find live young objects without scanning the entire old generation. This makes property writes to long-lived objects slightly more expensive; repeated writes can make the cost noticeable. section 9.2.4
Concurrent marking presents a related problem: JavaScript can change the object graph while background threads trace it. A reference added after an object has been scanned must not be overlooked. V8’s advancing-wavefront barrier marks the newly referenced target. JavaScriptCore’s retreating-wavefront barrier instead flags the modified source object for another scan. In JavaScriptCore, that revisit strategy can serve both concurrent and generational collection; V8 uses separate barrier logic for those concerns. section 9.2.3 section 9.3.3
object.field = newValue; #A
if (object.cellState == black) #B
addToRememberedSet(object); #C
The pseudocode shows the retreating approach: after a field write, an already-scanned (black) source object is added to a remembered set for revisiting. section 9.3.3
const sessions = new Map(); #A
function handleRequest(req) {
let session = sessions.get(req.sessionId);
if (!session) {
session = { id: req.sessionId, created: Date.now() };
sessions.set(req.sessionId, session);
}
// Every request mutates the long-lived session object
// with fresh short-lived objects
session.lastAccess = Date.now();
session.lastRequest = { #B
url: req.url,
headers: { ...req.headers },
timestamp: Date.now(),
};
session.history = (session.history || [])
.slice(-99)
.concat([req.url]); #C
return buildResponse(req, session);
}
A persistent session object receives fresh request data on every call. Under heavy traffic, those writes can repeatedly trigger barriers while requests also create young objects. section 9.3.4
The practical distinction is between allocating new objects and repeatedly attaching them to objects that have survived for a long time. Both create collection work, but the latter also exercises write barriers. In JavaScriptCore, enough such writes can force revisits faster than concurrent marking can make progress, increasing collection latency. section 9.2.4 section 9.3.4
