Hints for diagnosing memory leaks in NodeJS

NodeJS

A couple of things I keep having to rediscover when diagnosing crashes in Node programs caused by running out of memory:

  • Set --max-old-space-size=100 (or some other smallish number of megabytes) to make the crash happen faster and reduce the chance of it bringing down other things on the system.
  • Set --heapsnapshot-near-heap-limit=1 to generate a *.heapsnapshot file when the runtime thinks it’s close to running out of memory. This process takes a long time and a lot of memory of its own (seemingly roughly twice the heap size, more or less), so setting max-old-space-size to something relatively small seems mandatory when using this option, to avoid the snapshot process itself running out of memory.
  • The resulting profile can be loaded in the Chrome developer tools’ Memory tab for viewing.