Memory Leaks

Agenda

  • What are memory leaks?
  • How do they get created?
  • How can we detect them?
  • How can we fix Them?
  • Demo
  • Best practices

Definition

"A type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released." (Wikipedia)

Garbage Collection

  • Mark And Sweep
  • roots (window)
  • Unmarked children are GC'ed

Types of Memory Leaks

The easy three:

  • Accidental global variables
  • Timers & Callbacks (watchers etc.)
  • DOM reference from JavaScript

 

The complicated one:

  • Closures

 

Periodic vs. Sporadic

Tools - Timeline

Record your app over time to view:

  • JS Heap
  • Documents
  • Nodes
  • Listeners

 

Find out leak trends.

Tools - Profiles

  • Record heap snapshot
    • Snapshot compression
    • Shallow vs. Retained
    • Yellow vs. Red
    • The 3 snapshot check
  • Record heap allocation over time
  • Record allocation profile

Demo

Best Prectices

  • 'use strict'
  • Cancel timeouts\intervals\callbacks
  • Remove DOM references
  • Understand closure impact
  • Name unanimous function & function expressions
  • Separate short lived from long lived objects

Additional Reading

Memory Leaks

By rotempe4

Memory Leaks

  • 816