【问题标题】:Node.js - Get used memory of executed app (even after it has been killed)Node.js - 获取已执行应用程序的已用内存(即使它已被杀死)
【发布时间】:2019-05-02 05:52:17
【问题描述】:

即使在 ChildProcess 被杀死后(在 exec 回调中),我如何获取内存使用情况?

我尝试使用pidusage 模块,但它只在进程打开时有效。

我实际尝试过的:

var proc = exec(execComm,(error, stdout, stderr) => {
    if (error) {
        callback({status: -1, reason:stderr });
    }

    var pidusage = require("pidusage");

    pidusage(proc.pid,function(err,stat){
        console.log(err,stat);
    });

    callback({ status:0, file: out });
});

但是为什么pidusage会发送[Error: No maching pid found]呢?
是不是因为这个模块无法获取已经关闭的模块的信息?
以及如何在exec 回调中获取该信息?

【问题讨论】:

  • 你在回调中有额外的)

标签: javascript node.js memory


【解决方案1】:

您可以将帮助程序绑定到应用程序的退出信号并读取内存使用情况,但由于 gc 可能会在不可预测的时间运行,我不确定您能从中得到什么。

const ExitHandler = () => { /* your code */ };
process.on( 'exit', ExitHandler.bind( null, { withoutSpace: false } ) );              // on closing
process.on( 'SIGINT', ExitHandler.bind( null, { withoutSpace: true } ) );             // on [ctrl] + [c]
process.on( 'uncaughtException', ExitHandler.bind( null, { withoutSpace: false } ) ); // on uncaught exceptions

【讨论】:

  • 对我正在做的事情的一点解释:我想创建可以使用模板测试一些外部代码的程序...但是程序需要检查:1.应用程序是否有时间溢出? (限时setTimeout) 2.app有内存溢出吗? (我现在尝试这样做)所以我需要检查:使用了多少内存?
【解决方案2】:

我相信您可以使用 node-heapdump 之类的东西在您的子进程中创建堆转储,以便事后检查。

这样您还可以进行多次转储,以便随着时间的推移检查内存使用情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多