Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How JavaScript Collects Your Garbage

How JavaScript Collects Your Garbage

Avatar for Rory Fitzpatrick

Rory Fitzpatrick

November 13, 2014

More Decks by Rory Fitzpatrick

Other Decks in Technology

Transcript

  1. In computer science, garbage collection (GC) is a form of

    automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
  2. function sayHello(data) { setTimeout(function() { console.log(data.hello); }, 1000); } !

    var foo = {hello: “EdinJS”}; sayHello(foo); foo = null;
  3. function sayHello(data) { setInterval(function() { console.log(data.hello); }, 1000); } !

    var foo = {hello: “EdinJS”}; sayHello(foo); foo = null;
  4. var el = document.getElementById(‘myBtn’); var data = { el: el,

    msg: ‘hello’ }; ! el.addEventListener(‘click’, function() { alert(data.msg); }); ! // Some time later ! el.parentNode.removeChild(el); IE<8
  5. Hidden classes var data = { foo: “bar” }; !

    doSomething(data); delete data.foo;
  6. V8 is clever function sayHello(data) { console.log(data.hello); ! setInterval(function() {

    console.log(‘interval’); debugger; // console.log(data) -> undefined }, 1000); } ! var foo = {hello: “EdinJS”}; sayHello(foo); foo = null;