Skip to content

Comment on How JavaScript closures are garbage collected

Comments

The author wrote a really short code snippet that demonstrates the problem:

  function f() {
    var some = [];
    while(some.length < 1e6) {
      some.push(some.length);
    }
    function g() { some; } //removing this fixes a massive memory leak
    return function() {};    //or removing this
  }
If you keep a reference to the results of calling this function, it will keep a reference to the "some" variable, even though it is not needed.

He created a page that demonstrates the problem here:

https://s3.amazonaws.com/chromebugs/memory.html

So well that it crashed/froze icweasel...

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.