javascript - Do we manually need to clean up unreferenced variables in a closure? -


i'm reading article (http://javascript.info/tutorial/memory-leaks#memory-leak-size) memory leaks mentions memory leak:

function f() {     var data = "large piece of data";      function inner() {         return "foo";     }      return inner; } 

javascript interpreter has no idea variables may required inner function, keeps everything. in every outer lexicalenvironment. hope, newer interpreters try optimize it, not sure success.

the article suggests need manually set data = null before return inner function.

does hold true today? or article outdated? (if it's outdated, can point me resource current pitfalls)

modern engines not maintain unused variables in outer scope.

therefore, doesn't matter if set data = null before returning inner function, because inner function not depend on ("close over") data.

if inner function did depend on data--perhaps returns it--then setting data = null not want to, because then, well, null instead of having original value!

assuming inner function depend on data, yes, long inner being pointed (referred by) something, value of data have kept around. but, that's saying want! how can have available without having available?

remember @ point variable holds return value of f() go out of scope. @ point, @ least until f() called again, data garbage collected.

the general rule don't need worry memory , leaks javascript. that's whole point of gc. garbage collector excellent job of identifying needed , not needed, , keeping former , garbage collecting latter.

you may want consider following example:

function foo() {   var x = 1;   return function() { debugger; return 1; }; }  function bar() {   var x = 1;   return function() { debugger; return x; }; }  foo()(); bar()(); 

and examine execution in chrome devtools variable window. when debugger stops in inner function of foo, note x not present local variable or closure. practical purposes, not exist.

when debugger stops in inner function of bar, see variable x, because had preserved accessible in order returned.

does hold true today? or article outdated?

no, doesn't, , yes, is. article 4 years old, lifetime in web world. have no way know if jquery still subject leaks, i'd surprised if were, , if so, there's easy enough way avoid them--don't use jquery. leaks article's author mentions related dom loops , event handlers not present in modern browsers, mean ie10 (more ie9) , above. i'd suggest finding more up-to-date reference if want understand memory leaks. actually, i'd suggest stop worrying memory leaks. occur in specialized situations. it's hard find on topic on web these days precise reason. here's 1 article found: http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -