Follow up on memory


less warnings, more good practises

@naugtur, meet.js 04.2014

Cool story

Visitor Recording app started at ~4GB RAM, went to ~200MB

  • Changed canvases to SVGs
    (canvas allocates memory for every pixel)
  • Added destroymethods everywhere
  • Assigned null to all collections after use, etc.
  • Tracked down memory leak in V8 engine

Good memory management

Some constructive tips

Identify what's big

Maybe you can live without it

destroy()stuff once you stop using it

Frameworks help, but it's easy to do yourself too.

drop references to DOMnodes

If you keep a reference to a node, removing any of its parents will create a detached DOM tree.

What if I don't know when?

Destroy old instances before you create new.
That's good enough.

If you're using delete to free memory


You're doing it wrong

Helpful tools

  • Run V8 with GC access enabled
    node --expose-gc app.js 
    chrome --js-flags="--expose-gc"
    global.gc();
    window.gc();
  • New node profiling toy:
    https://github.com/thanpolas/profy
  • Firefox triggers GC when you alert()

Demooo

https://github.com/naugtur/js-memory-demo

this.destroy();

@naugtur, naugtur.pl