0

My nodejs script fails with following error,

<--- Last few GCs --->

[78:0x29f56b0]    31470 ms: Scavenge 2041.7 (2050.3) -> 2041.9 (2051.5) MB, 9.3 / 0.0 ms  (average mu = 0.231, current mu = 0.213) allocation failure 
[78:0x29f56b0]    31547 ms: Scavenge 2042.8 (2051.5) -> 2042.5 (2047.8) MB, 14.9 / 0.0 ms  (average mu = 0.231, current mu = 0.213) allocation failure 


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x134e879]
Security context: 0x01e8352c0919 <JSObject>
...

Now, I want to know the memory consumption by nodejs's V8 engine in runtime while my script is being executed. How can I do that ?

laxman
  • 1,781
  • 4
  • 14
  • 32
  • 2
    Does this answer your question? [How to monitor the memory usage of Node.js?](https://stackoverflow.com/questions/20018588/how-to-monitor-the-memory-usage-of-node-js) – Kael Watts-Deuchar Jan 03 '20 at 08:13
  • Hope this will be helpful - https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27 – RRR Jan 03 '20 at 08:16

2 Answers2

2

If you want to go with native nodejs solution you can use process.memoryUsage() function.

You can read more about it here.

I have written a small helper script for myself that returns formatted memory consumption that is easy to read (values are calculated to MBs):

function getMemory() {
    return Object.entries(process.memoryUsage()).reduce((carry, [key, value]) => {
        return `${carry}${key}:${Math.round(value / 1024 / 1024 * 100) / 100}MB;`;
    }, "");
};

The output of this function looks like this:

rss:282.61MB;heapTotal:239.46MB;heapUsed:129.36MB;external:22.13MB;
Goran
  • 3,292
  • 2
  • 29
  • 32
0

if you want to check for global memeory go for

var os = require('os');

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

or you want node specific then go for

memwatch-next

for tutorial click

abhinavsinghvirsen
  • 1,853
  • 16
  • 25