Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 8

Young and old objects

Generational garbage collection in V8

Theme
Try itYoung and old objects
A model of the chapter’s experiment: a loop allocates objects, the collector sweeps the young generation and moves survivors to the old one. Switch escape analysis on and off, move the share of survivors and watch how many collections it takes and how many objects get copied.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

What is generational garbage collection?

Generational garbage collection splits the heap by the age of objects, on the observation that most objects die young. New objects go into a small young generation. When it fills up, a minor collection, the scavenge, copies the few live objects out and treats the rest of the space as free, so its cost depends on the survivors, not on how much was allocated. Objects that survive a couple of minor collections are promoted to the old generation, which is collected less often by a slower major collection. In V8 the young generation is collected by a scavenger and the old one by mark-compact.

A garbage collector bets that objects die young. The heap is split in two: a small nursery for new objects and an old generation for those that lasted. When the nursery fills up, the collector copies out whatever is still alive and declares the rest free. If everything died, there is nothing to copy and the collection costs almost nothing. Each survivor has to be copied, and one that survives twice moves to the old generation, which gets its own, slower collection.

This is a model of the chapter’s experiment, since a page cannot watch the browser’s collector. Press Run and the loop allocates objects into the nursery cell by cell. The three buttons set up the book’s three runs: young with escape analysis, young without it, and old, where every tenth object goes into a ring buffer and outlives several collections. The slider changes the share of survivors, and the counters show collections, copies, promotions and an estimate of the pauses.

In the book, on Node 24, twenty million objects in young mode took 37–39 ms with only 2–6 minor collections, because the optimizing compiler proved the object never left the loop and did not create it at all. With escape analysis off there were 461 minor collections, 9.9 ms in all. In old mode the run took 197–202 ms, of which 131–136 ms went to 40–41 minor collections, the longest pause was 9–16 ms, and there were 2 major collections.

The chapter’s three conclusions follow from that. The compiler can make an object disappear before the collector ever sees it. A nursery where everything died is collected almost for free. And you pay for survivors that have to be copied. Which objects of a program will actually be created, and which the compiler will dissolve into variables, cannot be seen from its text; it can only be measured.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 8

Garbage, Profiles, and the Limits of JIT

Next to this experiment in the chapter
The garbage collector is gambling too. It bets that objects will die young, shapes its design around that bet and loses when the program behaves otherwise. As with the compiler, the programmer takes no part in this game and does not see the bets. Which of their objects will be created at all, and which the compiler will dissolve into variables, cannot be told from the text of the program; the only way to find out is to measure.