vbscript - How does garbage collection work in JavaScript? -
how garbage collection work in javascript? similar .net garbage collection? , because implementation of garbage collection in vbscript bad people avoided , established preference javascript standard client-side language?
how garbage collection work?
the short answer is: when block of memory (an object, say) no longer reachable, eligible reclaimed. when, how, or whether reclaimed entirely implementation, , different implementations differently. @ language level, it's automatic.
for example:
function foo() { var bar; bar = new reallymassiveobject(); bar.somecall(); }
when foo
returns, object bar
points automatically available garbage collection because there nothing left has reference it.
contrast with:
function foo() { var bar; bar = new reallymassiveobject(); bar.somecall(); return bar; } // elsewhere var b = foo();
...now reference object survives call, , persists until/unless caller assigns else 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, object not available garbage collection when foo
returns. because timer stuff has reference anonymous function we've created, , anonymous function has reference execution context created call foo
, includes bar
, , therefore bar
continues reference object. 3 seconds. timer stuff releases reference anonymous function, availble gc'd, anonymous function referencing no 1 else referencing (and execution context containing bar
, object bar
points become available gc well).
(this business of function having reference "execution context" in created why javascript functions called "closures" — term mathematics, means "close over" data. more closures in this article. , yes, anonymous function above does keep object around while function exists, though doesn't explicitly reference bar
, because of how javascript closures work — barring implementation optimizations [and engines optimize], required not have side-effects observable in code.)
javascript has no problem handling cleaning circular references, btw, instance:
function foo() { var a, b; = {}; b = {}; b.refa = a; a.refb = b; }
when foo
returns, fact a
referring b
, vice-versa isn't problem. since nothing else refers either of them, can both cleaned up. on ie, not true if 1 of objects host-provided object (such dom element or created via new activexobject
) instead of javascript object. (so instance, if put javascript object reference on dom element , javascript object refers dom element, keep each other in memory when no 1 referencing either of them.) that's ie bugissue, not javascript thing.
re:
is because vbscript gc bad people reverted javascript standard client side api?
javascript original client-side web scripting language. vbscript came later, when microsoft came out browser, , ever supported in microsoft browsers. javascript , client-side scripting game in town if want work broadest range of browsers. <subjective>it's 8 times language classic vbscript ever was. ;-) </subjective>
Comments
Post a Comment