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?
How does garbage collection work in JavaScript? Is it similar to .NET garbage collection?
Share
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:
When
fooreturns, the objectbarpoints to is automatically available for garbage collection because there is nothing left that has a reference to it.Contrast with:
…now a reference to the object survives the call, and persists until/unless the caller assigns something else to
borbgoes out of scope.Also contrast with:
Here, even after
fooreturns, 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 thebarvariable. As a result, in theory, whatbarrefers to isn’t available for garbage collection immediately whenfooreturns. 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 tobar, and doesn’t contain anyevalornew Functioncode that might refer to it dynamically at runtime, so the JavaScript engine can safely leavebarout 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:
When
fooreturns, the fact thatais referring toband 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 vianew 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 IEbugissue, not a JavaScript thing.Re:
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>