英文:
How to monitor memory consumption by nodejs
问题
现在,我想知道在我的脚本执行时,Node.js 的 V8 引擎在运行时的内存消耗是多少?如何做到这一点?
英文:
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 ?
答案1
得分: 2
如果你想使用原生Node.js解决方案,可以使用process.memoryUsage()函数。
你可以在这里了解更多信息。
我为自己编写了一个小的辅助脚本,它返回易于阅读的格式化内存消耗(值已计算为MB):
function getMemory() {
return Object.entries(process.memoryUsage()).reduce((carry, [key, value]) => {
return `${carry}${key}:${Math.round(value / 1024 / 1024 * 100) / 100}MB;`;
}, "");
};
这个函数的输出如下所示:
rss:282.61MB;heapTotal:239.46MB;heapUsed:129.36MB;external:22.13MB;
英文:
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;
答案2
得分: 0
> 如果您想检查全局内存,请使用
var os = require('os');
os.freemem();
os.totalmem();
> 或者如果您需要针对Node.js的特定内存信息,请使用
memwatch-next
有关教程,请点击此处
英文:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论