Sub topic 6
How Closures Keep Data Alive
This chapter assumes you know how closures capture variables from an outer scope and how reachability from roots decides what the garbage collector keeps. Closures matter for memory because a function object can be a path from a root to data you thought was gone.
What a closure actually holds
When an inner function uses a variable from an outer function, the engine has to keep that variable after the outer function returns. In V8, captured variables go into a heap-allocated context object, and each closure holds a reference to it. Variables that no inner function uses can stay on the stack and are released normally.
function createHandler() {
const bigData = new Array(1_000_000).fill(0);
return function handler() {
return bigData.length;
};
}
const handler = createHandler();
console.log(handler()); // 1000000
// bigData stays in memory for as long as handler is reachable.The array is no longer reachable through any named variable. It is still reachable through a chain: the global handler binding points to the function, the function points to its context, and the context points to bigData.
Capture only what you need
If the closure needs only a value derived from the large data, compute that value first and capture only the result.
function createHandler() {
const bigData = new Array(1_000_000).fill(0);
const size = bigData.length;
return function handler() {
return size;
};
}
const handler = createHandler();
console.log(handler()); // 1000000
// bigData is not captured, so it can be collected after createHandler returns.You can also release a captured value on purpose by reassigning it, for example bigData = null (with let), once the closure no longer needs it.
The shared-context gotcha
All closures created in the same scope share one context object. If any of them captures a variable, every closure from that scope keeps the variable alive, including closures that never use it.
function setup() {
const bigData = new Array(1_000_000).fill(0);
const logSize = () => console.log(bigData.length);
logSize();
return () => "small";
}
const keep = setup();
console.log(keep()); // "small"
// In V8, bigData is retained: logSize put it in the shared context,
// and the returned arrow function holds that same context.The returned function never mentions bigData, but it holds the context, so the array survives. The fix is the same as before: avoid capturing large values in that scope, or set them to null once you are done with them.
When closures become leaks
A closure on its own does not cause a leak. The leak comes from the closure living longer than the data it holds should. Watch for closures stored in places that live a long time:
| Long-lived holder | What it retains |
|---|---|
| Module-level or global variable | The closure's whole context, until reassigned |
| Cache or registry object | Every cached closure and its captured data |
| Pending promise callback | Captured data until the promise settles |
| Callback held by another API | Captured data until the API drops the callback |
The next chapter covers timers and event listeners, two of the most common long-lived holders.
Exercise
A function creates a large array rows, defines const count = () => rows.length and uses it once, then returns () => Date.now(). The returned function is stored globally. Is rows retained in V8, and how would you fix it?
Show answer
Yes. count captures rows, so rows goes into the context object that all closures in that scope share, and the returned function keeps that context alive. To fix it, set rows to null (declared with let) before returning, or compute rows.length into a plain variable and capture that instead.
Exercise
Why does capturing const size = bigData.length instead of bigData let the array be collected?
Show answer
The closure's context then holds only the number. Once createHandler returns, nothing reachable points to the array, so the collector can free it.