How does garbage collection work in JavaScript? Is it similar to .NET garbage collection? And is it because the implementation of garbage collection in VBScript is bad that people avoided it and established a preference for JavaScript as their standard client-side language?
-
see also [What is JavaScript garbage collection?](http://stackoverflow.com/q/864516/1048572) – Bergi Mar 27 '15 at 10:40
2 Answers
How does garbage collection work?
The short answer is: When a block of memory (an object, say) is no longer reachable, it is eligible to be reclaimed. When, how, or whether it is reclaimed is entirely up to the implementation, and different implementations do it differently. But at a language level, it's automatic.
For example:
function foo() {
var bar;
bar = new ReallyMassiveObject();
bar.someCall();
}
When foo
returns, the object bar
points to is automatically available for garbage collection because there is nothing left that has a reference to it.
Contrast with:
function foo() {
var bar;
bar = new ReallyMassiveObject();
bar.someCall();
return bar;
}
// elsewhere
var b = foo();
...now a reference to the object survives the call, and persists until/unless the caller assigns something else to b
or b
goes out of scope.
Also contrast with:
function foo() {
var bar;
bar = new ReallyMassiveObject();
bar.someCall();
setTimeout(function() {
alert("Three seconds have passed");
}, 3000);
}
Here, even after foo
returns, the timer mechanism has a reference to the timer callback, and the timer callback — a closure — has a reference to the context where it was created, which in turn contains the bar
variable. As a result, in theory, what bar
refers to isn't available for garbage collection immediately when foo
returns. Instead, it's kept around until the timer fires and releases its reference to the callback, making the callback and the context it refers to eligible for GC. (In practice, modern JavaScript engines can and do optimize closures where they can. For instance, in the above, static analysis shows the callback doesn't refer to bar
, and doesn't contain any eval
or new Function
code that might refer to it dynamically at runtime, so the JavaScript engine can safely leave bar
out of the context the function refers to, thus making what it refers to eligible for GC — and modern ones do). (More about closures in this article.)
JavaScript has no problem handling cleaning up circular references, btw, so for instance:
function foo() {
var a, b;
a = {};
b = {};
b.refa = a;
a.refb = b;
}
When foo
returns, the fact that a
is referring to b
and vice-versa isn't a problem. Since nothing else refers to either of them, they can both get cleaned up. On IE, this is not true if one of the objects is a host-provided object (such as a DOM element or something created via new ActiveXObject
) instead of a JavaScript object. (So for instance, if you put a JavaScript object reference on a DOM element and the JavaScript object refers back to the DOM element, they keep each other in memory even when no one is referencing either of them.) But that's an IE bugissue, not a JavaScript thing.
Re:
is it because the vbscript GC is bad that people reverted to javascript as their standard client side api?
JavaScript was the original client-side web scripting language. VBScript only came later, when Microsoft came out with a browser, and was only ever supported in Microsoft browsers. JavaScript was and is the only client-side scripting game in town if you want to work with the broadest range of browsers. <subjective>It's also about eight times the language classic VBScript ever was. ;-) </subjective>

- 1,031,962
- 187
- 1,923
- 1,875
-
5The short answer is: it depends on the platform. Each browser or JS VM is free to implement it the way they want. But good explanation. – haylem Dec 01 '10 at 12:25
-
2@haylem: Indeed -- well, within limits. The platform can't reclaim things in a way that violates JavaScript's scope rules. And a platform that *never* reclaims anything is likely to be unsuccessful. – T.J. Crowder Dec 01 '10 at 12:27
-
2@T.J. Crowder: yes, but they can obviously have their own optimization tricks and different approaches to reference counting, for instance. – haylem Dec 01 '10 at 12:37
-
2@haylem: Right. *"When, how, or whether it is reclaimed is entirely up to the implementation..."* – T.J. Crowder Dec 01 '10 at 12:48
-
While it may have been true in older browsers that the timer would delay the garbage collection for 3 seconds, nowadays modern V8 compilers are smart enough to figure out the big function won't be used in the inner anonymous function. What would have likely kept the big object in memory is using an eval statement inside that setTimeout statement. – Jack G Sep 23 '17 at 17:49
-
@lolzerywowzery: *"...barring implementation optimizations [and some engines do optimize]..."* Still, I may edit this tomorrow to call out optimization more significantly (not least since, seven years later, optimization is more significant :-) ). – T.J. Crowder Sep 23 '17 at 19:17
-
It might be notable that VBScript does not do garbage collection, it does reference counting, freeing anything that is not referenced anymore immediately. Except for COM objects, which are not part of VBScript, and managed differently (system-, or process-wide garbage collection). – TheBlastOne Jun 17 '18 at 17:59
-
@TheBlastOne - COM uses reference counting too ([ref](https://msdn.microsoft.com/en-us/library/windows/desktop/ms687260(v=vs.85).aspx)). – T.J. Crowder Jun 18 '18 at 06:37
-
1@T.J.Crowder true, I was falsely associating the situation described in http://www.informit.com/articles/article.aspx?p=26993, thx – TheBlastOne Jun 30 '18 at 10:29
Garbage collection, in principle, uses similar methods in all languages. Their implementation will however be different in different environments (e.g. each browser uses a different way of implementing JavaScript GC). For a very brief overview of Chrome's GC, see e.g. this.
As for VBScript, it was created as a JavaScript rival/replacement language that only runs in IE. This was a fairly reasonable decision at the time VBS was introduced - IE had 90+% of the browser share and it looked that VBS can replace the (widely supported, older and feature-poor at the time) JavaScript; not so much nowadays. Also, VBScript is basically Visual Basic Lite, with all the negative connotations to go with that brand.

- 91,498
- 46
- 177
- 222
-
3VBS was introduced with IE in 1996. At the time, _Netscape_ had about 90% browser share. VBS gave MS ways to tie in to native system components, which was all about lock-in at the time. – Julian Jul 17 '14 at 16:43