Sub topic 10
Generational Collection: Young and Old Generations
This chapter assumes you know how mark-and-sweep works from the previous chapter and can run Node.js from a terminal. It covers how V8 splits the heap by object age, and why that split makes most collections cheap.
The generational hypothesis
Marking and sweeping the whole heap costs time in proportion to how many live objects it holds. Most programs share a useful property, known as the weak generational hypothesis: most objects die young. Temporary arrays, intermediate strings, and objects returned from a function and then dropped are all unreachable within moments of being created. A few objects, such as caches, configuration, and module state, live for the whole program.
V8 takes advantage of this by dividing the heap into two regions and collecting each with a different algorithm.
| Region | What lives there | Collector | How often |
|---|---|---|---|
| Young generation | Newly allocated objects | Scavenger (minor GC) | Very often, fast |
| Old generation | Objects that survived young-generation collections | Mark-Compact (major GC) | Less often, slower |
The young generation and the Scavenger
New objects are allocated in the young generation. This region is small, usually a few megabytes up to a few tens of megabytes depending on the platform and settings. V8 splits it into two equal halves called semi-spaces. Allocation only bumps a pointer forward in the active half, which makes it very cheap.
When the active half fills up, the Scavenger runs:
- Starting from the roots, it finds the live objects in the young generation.
- It copies each live object into the empty half, or into the old generation if the object has already survived a previous scavenge.
- It swaps the two halves. Everything left behind in the old half is garbage and gets reused without being visited.
The Scavenger's cost depends on the number of surviving objects. Dead objects cost nothing. A young generation full of short-lived garbage can be cleared in about a millisecond.
Promotion and the old generation
An object that survives two scavenges is promoted (moved) into the old generation. The old generation is much larger and is collected by Mark-Compact. Mark-Compact marks live objects starting from the roots, sweeps the dead ones, and compacts fragmented pages by moving live objects together. Because the old generation can be hundreds of megabytes, a major GC costs far more than a scavenge.
Pointers from old to young
A scavenge only scans the young generation, but an old object can point to a young one, for example when you push a new item into a long-lived array. To keep that young object from being freed by mistake, V8 runs a write barrier on every pointer store. When an old object gets a reference to a young object, the barrier records that slot in a remembered set, and the Scavenger treats those slots as extra roots.
Watching it happen
Save this as gen.js:
const keep = [];
for (let i = 0; i < 2_000_000; i++) {
const temp = { index: i, label: `item-${i}` }; // dies almost immediately
if (i % 1000 === 0) {
keep.push(temp); // survives and gets promoted
}
}
console.log(`kept ${keep.length} objects`);Run it with GC tracing turned on:
node --trace-gc gen.jsThe output shows many Scavenge lines and few or no Mark-Compact lines. That ratio is what the generational design aims for.
Common misreadings
- Frequent scavenges are normal. A leak usually shows up as old-generation size rising after each Mark-Compact.
- A larger young generation (
--max-semi-space-size) reduces how often scavenges run and how early objects get promoted, but it uses more memory. Measure before you change it.
Exercise
A service allocates millions of small objects per second, and nearly all of them are unreachable within one request. Which collector does most of the work, and why is it cheap?
Show answer
The Scavenger, which collects the young generation. Its cost scales with the number of surviving objects. Dead objects are never visited, so a young generation that is mostly garbage is cleared quickly.
Exercise
Why does V8 need a write barrier and remembered set to run a scavenge correctly?
Show answer
A scavenge scans only the young generation. Without a record of old-to-young pointers, a young object reachable only through an old object would look unreachable and be freed. The write barrier records those pointers so the Scavenger can treat them as roots.