Sub topic 13
Shorter Pauses: Incremental, Parallel, and Concurrent GC
This chapter assumes you know how mark-and-sweep works and how V8 splits the heap into a young generation and an old generation. Mark-and-sweep explains what the collector frees. This chapter covers when it does that work, and how V8 keeps the work from freezing your page or server.
The problem: stop-the-world pauses
The simplest collector stops all JavaScript execution, marks every reachable object, sweeps the rest, and only then lets your code continue. This is called a stop-the-world pause. On a heap of a few hundred megabytes, a full mark can take tens of milliseconds or more. A browser has about 16 ms to draw each frame at 60 fps, so one long pause causes visible jank. On a server, the same pause shows up as a latency spike on every request in flight.
V8's garbage collector (the project was called Orinoco) cuts these pauses with three techniques. Each one moves work to a different place.
| Technique | Where the work goes | Main thread blocked? | Typical use in V8 |
|---|---|---|---|
| Incremental | Small slices on the main thread, interleaved with JS | Yes, briefly per slice | Old-generation marking |
| Parallel | Several helper threads during one pause | Yes, but for less time | Young-generation scavenge, compaction |
| Concurrent | Background threads while JS keeps running | No (mostly) | Old-generation marking, sweeping |
| Idle-time | Gaps when the main thread has nothing to do | Only when idle | Browser frames with spare time |
Incremental marking
Incremental marking breaks one long mark phase into many short steps. V8 marks some objects, returns to your code, then marks a few more. The total work stays about the same, but no single pause is long.
The catch is that your code keeps changing the object graph between steps. Suppose the collector has already finished marking object A, and your code then stores a pointer to an unmarked object B inside A. Without some fix, B would never be visited and would be freed while still in use.
V8 solves this with a write barrier. A write barrier is a small piece of code the engine runs on every pointer write while marking is in progress. If a marked object gains a reference to an unmarked one, the barrier records the new target so the collector will visit it. This keeps marking correct at the cost of slightly slower property writes during a GC cycle.
Parallel collection
Parallel collection still stops JavaScript, but splits the work across several threads. V8's young-generation scavenger copies live objects using multiple helper threads at once. The pause still happens, but it ends sooner. Parallel work is easier to get right than concurrent work because your code is not running and cannot change the graph.
Concurrent collection
Concurrent collection runs GC work on background threads while your JavaScript keeps running. V8 does most old-generation marking this way and sweeps memory pages in the background. The main thread only pauses briefly at the end to finish marking and handle anything the write barrier recorded. Concurrent marking uses the same write barrier as incremental marking, since your code mutates the graph while the collector reads it.
Watching it happen
You can see these phases in Node.js with the --trace-gc flag. Save this as gc-demo.js:
const retained = [];
for (let i = 0; i < 200; i++) {
// Short-lived garbage: triggers young-generation scavenges
for (let j = 0; j < 10_000; j++) {
const temp = { index: j, label: `item-${j}` };
temp.label.length;
}
// Long-lived data: eventually triggers old-generation marking
retained.push(new Array(50_000).fill(i));
}
console.log(`retained ${retained.length} arrays`);node --trace-gc gc-demo.jsLines labelled Scavenge are quick young-generation collections. Lines labelled Mark-Compact are major collections. V8 often annotates these with how many incremental steps ran before the final pause. The exact output format differs between Node versions.
Exercise
Incremental and concurrent marking both need a write barrier, but a parallel scavenge does not. Why?
Show answer
During incremental and concurrent marking, JavaScript runs between or alongside GC work and can add references from already-marked objects to unmarked ones. The write barrier records those new references so live objects are not freed. A parallel scavenge happens while JavaScript is stopped, so the object graph cannot change during the work.
Exercise
A browser game drops frames every few seconds, and the profiler shows a long "Major GC" pause. Which of the three techniques reduces that pause, and what change in the game's code would help even more?
Show answer
Concurrent marking (helped by incremental steps) moves most major-GC marking off the main thread, leaving only a short final pause. Allocating fewer long-lived objects per frame, for example by reusing objects instead of creating new ones, reduces how often major GCs happen at all.