Skip to content

Instantly share code, notes, and snippets.

@mistakster
Created April 6, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistakster/d8844b15f852d9b4cd33cb3a9b2d6385 to your computer and use it in GitHub Desktop.
Save mistakster/d8844b15f852d9b4cd33cb3a9b2d6385 to your computer and use it in GitHub Desktop.
The memory usage logger for a Node.js process
/**
* The memory usage logger for a Node.js process
*
* Resident Set Size (RSS) is the amount of space occupied in the main memory
* device for the process, which includes the heap, code segment and stack.
* Variables are stored in the stack and the actual JavaScript code resides in the code segment.
*
* The heap is where objects, strings, and closures are stored.
*
* @see https://nodejs.org/api/process.html#process_process_memoryusage
*/
setInterval(function () {
const mem = process.memoryUsage();
function fmt(v) {
return (v / (1024 * 1024)).toFixed(3);
}
console.error(
'RSS = ' + fmt(mem.rss) + ', ' +
'Heap Total = ' + fmt(mem.heapTotal) + ', ' +
'Heap Used = ' + fmt(mem.heapUsed)
);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment