Facebook Pixel

Garbage Collection and Block Scope in JavaScript

Block scope helps the garbage collector free memory. Here is how and why closures can prevent it.

Garbage Collection and Block Scope in JavaScript

Block scope helps the garbage collector (GC) reclaim memory sooner. But closures can keep block-scoped variables alive longer than expected.

How Block Scope Helps GC

When a block ends, its lexical environment is eligible for garbage collection (unless a closure keeps it alive). The GC can free the block's variables immediately.

function process() { const big = new Array(1_000_000); // large array // ... use big ... // after the function returns, big is eligible for GC } `` With block scope, you can free memory even sooner: ```js function process() { { const big = new Array(1_000_000); // large array // ... use big ... } // big is eligible for GC here, before the function returns // ... more work, big is already freed ... } `` ### `var` Does Not Help GC Here ```js function process() { if (true) { var big = new Array(1_000_000); // leaks to function scope } // big is still alive here! GC cannot free it until the function returns } `` `var` is function-scoped, so it stays alive until the function returns, even if it was declared in a block that has ended. ### Closures Keep Variables Alive ```js function makeBigHolder() { const big = new Array(1_000_000); return () => big.length; // closure keeps big alive } const getLen = makeBigHolder(); // big is still alive here, because getLen closes over it `` The GC cannot free `big` because the returned function holds a reference to the lexical environment. This is a common source of memory leaks: closures that keep large objects alive unnecessarily. ### Memory Leak Example ```js const handlers = []; function setup() { const big = new Array(1_000_000); handlers.push(() => big.length); // keeps big alive } setup(); // big is alive as long as handlers[0] exists `` If `handlers` grows unbounded, each closure keeps its own `big` alive, causing a memory leak. ### Avoiding Closure Memory Leaks - Remove handlers when they are no longer needed. - Do not close over large objects unless necessary. - Use weak references (`WeakRef`, `WeakMap`) where appropriate. - Keep closures short-lived. ### The Takeaway Block scope helps the GC free variables as soon as the block ends. `var` does not (it is function-scoped). Closures keep block-scoped variables alive as long as the closure exists, which can cause memory leaks. Use `let`/`const` in blocks for better memory behavior, and avoid closing over large objects unnecessarily.

When a block ends, its lexical environment is eligible for GC (unless a closure keeps it alive). This lets the GC free block-scoped variables sooner than function-scoped var variables, which stay alive until the function returns.

Because var is function-scoped, not block-scoped. A var inside a block leaks to the enclosing function scope and stays alive until the function returns, even if the block has ended. let and const are freed when the block ends.

Yes. A closure keeps a reference to its lexical environment. As long as the closure exists, the closed-over variables cannot be garbage collected. This is a common source of memory leaks.

Remove handlers when no longer needed, do not close over large objects unless necessary, use WeakRef or WeakMap where appropriate, and keep closures short-lived. Avoid accumulating closures that each keep a large object alive.

Pushing closures into an array or map, where each closure captures a large object. As the collection grows, each entry keeps its large object alive, and the memory grows unbounded. Remove entries when they are no longer needed.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.