How can I monitor the memory usage of Node.js?
-
4A little more details could be helpful – Wottensprels Nov 16 '13 at 12:44
8 Answers
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.
- 328,606
- 78
- 779
- 666
-
1@MestreSan Which version of Node doesn't need `--expose-gc` for the `gc` function? – Rob W Mar 10 '16 at 22:44
-
3@MestreSan I never said that you need `--expose-gc` for `process.memoryUsage()`. `gc()` (requiring `--expose-gc`) was used in the answer to deterministically trigger garbage collection to make it easier to see what the `process.memoryUsage` reports. – Rob W Mar 11 '16 at 21:53
-
That's an awesome answer to measure JS-Stuff in the right way. Thank you for that answer. – suther Nov 07 '19 at 16:35
-
You 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. – Andrew Jul 31 '20 at 11:12
-
node-memwatch : detect and find memory leaks in Node.JS code. Check this tutorial Tracking Down Memory Leaks in Node.js
- 10,322
- 10
- 58
- 80
-
3node-memwatch does not seem to be alive any more (last updated in March 2013). Are there any alternatives? – Golo Roden Feb 10 '15 at 13:00
-
6@GoloRoden npm install memwatch-next works fine. Here is the repo: https://github.com/marcominetti/node-memwatch – fre2ak Jun 19 '15 at 16:11
-
A more up to date resource for hunting down memory leaks http://apmblog.dynatrace.com/2015/11/04/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js/ – saintedlama Dec 01 '15 at 10:19
-
37memwatch isn't maintained anymore and won't work on an recent version of node so don't even bother. – Mike Dec 18 '15 at 20:43
-
1[Understanding Garbage Collection and hunting Memory Leaks in Node.js](http://apmblog.dynatrace.com/2015/11/04/understanding-garbage-collection-and-hunting-memory-leaks-in-node-js/) – Damodaran Jan 25 '16 at 04:27
-
[Did you know you can inspect your node application’s memory without instrumenting your code ? It’s possible using a tool called mdb_v8](https://medium.com/@betable/getting-started-with-mdb-v8-5401c9c0e98f#.jgad0864t) – Damodaran Feb 12 '16 at 03:45
Also, if you'd like to know global memory rather than node process':
var os = require('os');
os.freemem();
os.totalmem();
- 4,056
- 1
- 40
- 50
-
4However, freemem() is not the same as available memory on the server. Any way to find available memory rather than free? – Alex Aug 04 '17 at 23:31
The original memwatch is essentially dead. Try memwatch-next instead, which seems to be working well on modern versions of Node.
- 14,654
- 5
- 35
- 58
-
8The king is dead; long live the king: https://www.npmjs.com/package/node-memwatch – Frank Meulenaar Sep 30 '19 at 07:57
-
`node-memwatch` at least works with node version 10. Thanks, @FrankMeulenaar :+1: – Raghav Garg Feb 25 '21 at 05:54
-
3the both libraries (memwatch-next and node-memwatch) are deprecated or not working. – Silvio Guedes May 13 '21 at 14:53
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"
}
*/
- 2,782
- 3
- 19
- 30
- 1,836
- 1
- 16
- 36
If you are using express.js framework then you can use express-status-monitor. Its very easy to integrate and it provides CPU usage, memory usage, response time etc in graphical format.
- 730
- 1
- 10
- 24
On Linux/Unix (note: Mac OS is a Unix) use top and press M (Shift+M) to sort processes by memory usage.
On Windows use the Task Manager.
- 30,802
- 16
- 100
- 180
-
@majidarif Go to `Applications > Utilities` and you will find an `Activity Monitor` app. That one is the equivalent of Task Manager. OS X also has the `top` command as well. – Ingwie Phoenix Jan 18 '15 at 21:49
-
4
node memwatch and memwatch next these are not supported on nodejs 14.17.0 version. only support for a new node-memwatch-new module. Please tellme how to install node-memwatch install on nodejs 14.17 version.
- 59
- 1
- 11
-
If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30620831) – Gaëtan Boyals Dec 20 '21 at 10:27
-
@SarathKumar I think you are asking about "node-memwatch-new", so you just want to go here, and follow the instructions: https://www.npmjs.com/package/node-memwatch-new – spechter Jan 10 '22 at 05:07
-