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

The Meteor leak: two closures, one environment

A memory leak through closures in V8

Theme
Try itThe Meteor leak: two closures, one environment
Call replaceThing() once, ten at a time or up to two hundred and watch a chain of objects nobody asked to keep grow longer. Switch between the experiment’s variants: with Glasser’s fix memory stays flat, in the others two hundred calls come to about 203 MB, as in the book’s measurement.

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

About this experiment

How can a closure cause a memory leak in JavaScript?

A closure keeps the variables of the function it was created in alive for as long as the closure itself can be reached. In V8, the variables that any nested function needs are stored in one context object per call of the outer function, and every closure created in that call points to the whole object, not only to the variables it uses. So a long-lived closure can keep alive a large value that only another, short-lived closure referred to. If that value holds an earlier closure in turn, the chain grows with every call and the garbage collector can free none of it.

In 2013 David Glasser of the Meteor team found a memory leak in the part of Meteor that re-renders templates when data changes, fixed it for version 0.6.5 and published an example a few lines long. Inside replaceThing there are two functions: unused, which looks at the previous object through originalThing and is never called, and someMethod, which is stored on the new object and never touches originalThing. Read by eye, the code has nothing to leak.

Press replaceThing() once, ten times or up to two hundred, and the chain of objects the garbage collector can still reach grows under the code: the global theThing holds someMethod, someMethod holds the environment of its call, the environment holds originalThing, and that is the previous theThing with its own someMethod. Switch the variants of the book’s experiment: orig as in Glasser’s post, shared where someMethod uses a local variable, fixed with originalThing = null at the end, and control where unused is kept on purpose. This one is a model, not a live run, because a page cannot force a garbage collection.

In the book, after 200 calls and a forced garbage collection, heapUsed was 202.7 MB on Node 20 and 203.7 MB on Node 24 in the orig, shared and control variants, and 3.6 and 4.7 MB with the fix. Getting there took a correction. With the original payload, a string of a million asterisks made with join, memory stayed flat even in the control variant: V8 keeps such a string as a ConsString, and 200 of them took 11 MB, while after slice they took 194 MB. The payload became an array of numbers, and the control variant is now written first.

The chapter’s point is who answers for the bug. The specification says which variables a function sees and is silent about how to store them; V8 keeps them in one shared object per call, and twelve years after Glasser’s post the leak is still there. Having handed memory to the garbage collector, the programmer has to know more about the engine than the specification promised.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 3

Functions as Data

Next to this experiment in the chapter
Both bugs in this chapter are built the same way and differ in who answers for them. The shared variable in a loop with var was invented by the specification, and the specification fixed it in 2015 by adding let. The shared object of variables was invented by the engine, nobody is obliged to change it, and twelve years later it is still there. By handing memory to the garbage collector, the programmer also got V8’s habits, about which the language specification has not a single line.