Closure memory leaks

David Bruant david.bruant at
Sun May 22 14:38:44 PDT 2011


After reading an article [1], I've been thinking about scopes in
ECMAScript. The idea in the article is that a closure keeps a reference
to everything in its scope. If in turn, one element in the scope keeps a
reference to the function (like as an event listener for an element),
then, it creates a circular reference.

Of course, as said in a couple of comments, circular references aren't a
problem since modern GC (IE8+ and other relevant browsers) aren't
reference-counting GC.
But the problem could exist anyway if, for instance a function has in
its scope an object it doesn't use.
"use strict";
function f(){
    var o = {}; // In g's scope, but unused anyway
    return function g(){
               return 1;
I originally thought that a static analysis of the closure could be
enough to determine whether things in the scope will ever be used or not.
It turns out it's not the case:
"use strict";
function f(){
    var o = {a:1};
    return function(e){ // doesn't strictly use o
               return eval(e);
console.log( f()('o') ); // returns the enclosed o object (at least on FF4)
So, this leads to a couple of questions:
- Is the previous code snippet correct?
- In which cases can static analysis be enough?
- Are there other forms of analysis that automatically prevent scope
memory leaks?
- Are there good practices people could use to avoid such leaks?
- Are "let" or "const" of any help here?
- If they aren't, would it make sense to have an initializer keyword to
create variables which could NOT be captured by closures? It would avoid
memory leak, but also would avoid to leak implementation details.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list