简体   繁体   中英

Node.js - Get used memory of executed app (even after it has been killed)

How can I take memory usage of ChildProcess even after it was killed (in exec callback)?

I tried using the pidusage module, but it only works when the process is opened.

What I actually tried:

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 });
});

But why does pidusage send [Error: No maching pid found] ?
Is it because this module can't get info of the already closed one?
And how to get that info in the exec callback?

You could bind a helper to the exit signal of your app and read out memory usage then but since the gc will likely run on unpredictable times I'm not sure what use you get out of that.

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

I belive you could use something like node-heapdump to create heapdumps in your child-processes that you could inspect after the fact.

That way you could also have multiple dumps so you could inspect the memory usage over time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM