Guide: Debugging Memory Usage - dsriseah/ursys GitHub Wiki

Built-in Debugging Modules (NodeJS)

process.memoryUsage() returns props like rss, heapTotal, heapUsed, and external in bytes

const memoryUsage = process.memoryUsage();
console.log(memoryUsage);

v8.getHeapStatistics() returns stats like total_heap_size, used_heap_size, total_heap_size_executable, and so on

const v8 = require('v8');
const heapStatistics = v8.getHeapStatistics();
console.log(heapStatistics);

Can also use runtime switches

  • node --inspect script
  • node --trace-gc script emits gc events to console

to get system memory info

const os = require('os');
const freeMemory = os.freemem();
console.log(`Free memory: ${freeMemory} bytes`);

or from the terminal:

  • free -h
  • `vm_stat | grep "Pages free"
  • top also provides free memory space info

V8 Notes

via memory management in v8 and memory management in v8

  • stack memory - probably not an issue
  • heap memory (old space, new space, large object, code, cell, map, property)

"old objects" is for stuff that hangs around and is less likely to die, compared to "young objects". They use different GCs:

  • young generation uses Scavenge, which is fast but space consuming. Every time a new object is created the GC Scavenge is run, and if survives GC twice it's moved to "Old Space"
  • old generation uses Mark-Sweep-Compact

Node has two flags that affect this that aren't officially part of the API but pass them along to V8. Via nodejs command line options

  • --max-old-space-size=SIZE (in megabytes): It's suggested to set this to max mem size - 512MB for other stuff. Note that our basic droplet has about 40% memory in use while doing nothing at all, leaving 300ish MB. The default is 2GB for a 64-bit system.
  • --max-semi-space-size=SIZE (in megabytes). This applies to the Scavenger GC which uses three spaces (each sized at SIZE), which increases the size of the "young object" cache by 3 times. Default on 64bit is 16MB.
⚠️ **GitHub.com Fallback** ⚠️