How to monitor the memory usage of Node.js?
Solution 1
node-memwatch : detect and find memory leaks in Node.JS code. Check this tutorial Tracking Down Memory Leaks in Node.js
Solution 2
The built-in process module has a method memoryUsage
that offers insight in the memory usage of the current Node.js process. Here is an example from in Node v0.12.2 on a 64-bit system:
$ node --expose-gc
> process.memoryUsage(); // Initial usage
{ rss: 19853312, heapTotal: 9751808, heapUsed: 4535648 }
> gc(); // Force a GC for the baseline.
undefined
> process.memoryUsage(); // Baseline memory usage.
{ rss: 22269952, heapTotal: 11803648, heapUsed: 4530208 }
> var a = new Array(1e7); // Allocate memory for 10m items in an array
undefined
> process.memoryUsage(); // Memory after allocating so many items
{ rss: 102535168, heapTotal: 91823104, heapUsed: 85246576 }
> a = null; // Allow the array to be garbage-collected
null
> gc(); // Force GC (requires node --expose-gc)
undefined
> process.memoryUsage(); // Memory usage after GC
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4528072 }
> process.memoryUsage(); // Memory usage after idling
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4753376 }
In this simple example, you can see that allocating an array of 10M elements consumers approximately 80MB (take a look at heapUsed
).
If you look at V8's source code (Array::New
, Heap::AllocateRawFixedArray
, FixedArray::SizeFor
), then you'll see that the memory used by an array is a fixed value plus the length multiplied by the size of a pointer. The latter is 8 bytes on a 64-bit system, which confirms that observed memory difference of 8 x 10 = 80MB makes sense.
Solution 3
Also, if you'd like to know global memory rather than node process':
var os = require('os');
os.freemem();
os.totalmem();
Solution 4
The original memwatch is essentially dead. Try memwatch-next instead, which seems to be working well on modern versions of Node.
Solution 5
You can use node.js memoryUsage
const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`
const memoryData = process.memoryUsage()
const memoryUsage = {
rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
}
console.log(memoryUsage)
/*
{
"rss": "177.54 MB -> Resident Set Size - total memory allocated for the process execution",
"heapTotal": "102.3 MB -> total size of the allocated heap",
"heapUsed": "94.3 MB -> actual memory used during the execution",
"external": "3.03 MB -> V8 external memory"
}
*/
Comments
-
fsiaonma over 2 years
How can I monitor the memory usage of Node.js?
-
Wottensprels over 10 yearsA little more details could be helpful
-
-
Ingwie Phoenix over 9 years@majidarif Go to
Applications > Utilities
and you will find anActivity Monitor
app. That one is the equivalent of Task Manager. OS X also has thetop
command as well. -
Golo Roden over 9 yearsnode-memwatch does not seem to be alive any more (last updated in March 2013). Are there any alternatives?
-
fre2ak almost 9 years@GoloRoden npm install memwatch-next works fine. Here is the repo: github.com/marcominetti/node-memwatch
-
saintedlama over 8 yearsA more up to date resource for hunting down memory leaks apmblog.dynatrace.com/2015/11/04/…
-
Mike over 8 yearsmemwatch isn't maintained anymore and won't work on an recent version of node so don't even bother.
-
Damodaran over 8 years
-
Damodaran over 8 years
-
Rob W about 8 years@MestreSan Which version of Node doesn't need
--expose-gc
for thegc
function? -
Rob W about 8 years@MestreSan I never said that you need
--expose-gc
forprocess.memoryUsage()
.gc()
(requiring--expose-gc
) was used in the answer to deterministically trigger garbage collection to make it easier to see what theprocess.memoryUsage
reports. -
Alex almost 7 yearsHowever, freemem() is not the same as available memory on the server. Any way to find available memory rather than free?
-
Ryan Shillington over 6 yearsuse
htop
instead of top on Linux. It's much better. -
Frank Meulenaar over 4 yearsThe king is dead; long live the king: npmjs.com/package/node-memwatch
-
suther over 4 yearsThat's an awesome answer to measure JS-Stuff in the right way. Thank you for that answer.
-
Andrew almost 4 yearsYou did the lords work with this one. I just realized all the methods exposed by calling process which will help me create a more efficient application. Thanks.
-
ΔO 'delta zero' over 3 yearsI believe this should be the accepted answer.
-
Raghav Garg about 3 years
node-memwatch
at least works with node version 10. Thanks, @FrankMeulenaar :+1: -
Silvio Guedes about 3 yearsthe both libraries (memwatch-next and node-memwatch) are deprecated or not working.
-
Gaëtan Boyals over 2 yearsIf you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
-
spechter over 2 years@SarathKumar I think you are asking about "node-memwatch-new", so you just want to go here, and follow the instructions: npmjs.com/package/node-memwatch-new
-
SarathKumar over 2 yearsOk Thankyou @spechter