In response to @ Beetroot-Beetroot doubts (which, admittedly, I have too), I did some more digging. I installed this script and used the chrome dev-tools' timeline in this article as a guide. In a fiddle, two almost identical handlers create a closure with two date objects. The first links are a , the second refers to both a and b . Although in both cases only a can really be set (hard-coded values), the first close uses significantly less memory. Regardless of whether it is JIC (only at compile time) or V8 JS optimization, I can not say for sure. But from what I read, I would say that it is a V8 GC that frees b when the tst function tst , which it cannot in the second case ( bar refers to b when tst2 returns). I have a feeling that this is not so wild, and I would not be surprised to know that FF and even IE will work in a similar way.
Just adding this, possibly irrelevant, update for completeness and because I feel that the link to Google documentation on dev-tools is an added value.
It depends on a simple rule: as long as you can no longer refer to the classInstance variable, it must be GC'ed, regardless of your own circular references. I tested quite a few constructs similar to the one you describe here. Maybe it's worth a look
I found that closures and mem leaks are not so common or easily accessible (at least not more),
But since the accepted answer says: it's almost impossible to find out when the code will leak.
Re-reading your question, I would say: no, you are not going to leak a memory: the classInstance variable classInstance not been created in the global scope, but it is passed to various functions (and, therefore, various fields of application). These areas fall apart every time the function returns. classInstance will not be GC'ed if it is passed to another function / scope. But as soon as the last function referencing classInstance , the object is tagged for GC. Of course, this may be a circular link, but it is a link that cannot be accessed anywhere except in its own area.
You cannot call it closure: closure occurs when there is some form of external impact, which does not happen in your example.
I painfully explain such things, but simply repeat:
var foo = (function() { var a, b, c, d; return function() { return a; } })();
GC will remove the link mem b , c and d : they are out of scope, there is no access to them ...
var foo = (function() { var a, b, c, d; return function() { a.getB = function() { return b; } a.getSelf = function() { return a;
In this case, b for obvious reasons, will not get GC'ed. foo provides a and b , where a is an object containing a circular reference. Although, as soon as foo = new Date() , foo loses the reference to a . Of course, a still refers to itself, but a no longer affected: he can refer to everything that he likes. Most browsers do not care and will be GC a and b . In fact, I checked both Chrome, FF and IE8 all the GC code above is fine ... no worries, then.