Created
April 6, 2018 06:36
-
-
Save mistakster/d8844b15f852d9b4cd33cb3a9b2d6385 to your computer and use it in GitHub Desktop.
The memory usage logger for a Node.js process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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