just remember
@naugtur, meet.js 11.2013
Young generation
New objects, frequent garbage collection
Old generation
Long lasting objects, garbage collection is rare, but done in parallel to JS execution
Objects and structures referenced from global root are copied back in.
delete
keyword does not free memorydelete
just slows you down (changes hidden class)null
doesn't remove the object, it changes the reference
var aBoy = { name: "Johny" },
aGirl = { name: "Sue" },
anAnimal={ name: "Garfield" };
aBoy = null; //Johny can now be garbage-collected
aGirl = { name: "Lucy" }; //Sue can now be garbage-collected
aGirl.ownsAnimal = anAnimal //we have a new reference to Garfield
anAnimal = null; //a reference is removed, but it was not the only one
//Garfield can't be garbage-collected
GMail was leaking so bad that Google is now pioneering in memory leaks debugging tools. True story :)
Collection of items is held in a variable that is scoped incorrectly, or the logic is bad.
var module = (function(){
var keptAsLongAsModuleLives = [];
return {
init:function(someObject){
keptAsLongAsModuleLives.push(someObject);
//do stuff
}
}
})();
A single DOM node is referenced from somewhere and the whole tree cannot be collected (element.parentNode, remember?)
A library might have created a cache,
or it's the browser itself
And that's where it gets sad...
What if I'm not leaking, but I need thousands of objects for short periods of time? I'm implementing a minigun.