185

How can I monitor the memory usage of Node.js?

0

5 Answers 5

233

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.

5
  • 1
    @MestreSan Which version of Node doesn't need --expose-gc for the gc function?
    – Rob W
    Mar 10, 2016 at 22:44
  • 4
    @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, 2016 at 21:53
  • That's an awesome answer to measure JS-Stuff in the right way. Thank you for that answer.
    – suther
    Nov 7, 2019 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, 2020 at 11:12
  • I believe this should be the accepted answer. Jan 13, 2021 at 4:00
60

Also, if you'd like to know global memory rather than node process':

var os = require('os');

os.freemem();
os.totalmem();

See documentation

4
  • 4
    However, freemem() is not the same as available memory on the server. Any way to find available memory rather than free?
    – Alex
    Aug 4, 2017 at 23:31
  • this is the answer to another question
    – humkins
    Feb 28 at 8:38
  • Seems os.freemem() uses available memory as of this issue: github.com/nodejs/node/issues/23892
    – richytong
    Apr 27 at 21:56
  • @Alex Math.round(OS.totalmem() / 1024 / 1024 * 100) / 100-Math.round(OS.freemem() / 1024 / 1024 * 100) / 100 Sep 13 at 6:25
55

You can use node.js process.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"
}
*/
30

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.

enter image description here

1
  • This is useful! To get basic insights at high level. Sep 13 at 8:50
5

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.

2
  • @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. Jan 18, 2015 at 21:49
  • 4
    use htop instead of top on Linux. It's much better. Oct 4, 2017 at 12:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.