How to get memory usage of a child process in node.js?

I know there is api process.memoryUsage () to use memory in the current process.

But if I start a new child process using child_process.spawn (command, [args], [options]) and I get a ChildProcess object, then how can I get a new use of the process memory?

+5
source share
2 answers

We can get a multi-platform solution using the nodejs ipc protocol. you just need to set up an event to request memory usage from the parent process, and then send process.memoryUsage()from the child process generated.

parent.js

var ChildProcess = require('child_process'),
    child = ChildProcess.fork('./child.js');

child.on('message', function(payload){
    console.log(payload.memUsage);
});

child.send('get_mem_usage');

and in child.jsit might look like this:

process.on('message', function(msg){
    if(msg === 'get_mem_usage'){
         process.send({memUsage: process.memoryUsage()});
    }
});
+1

, ps ( /proc/<pid>/stat), unix. :

// Spawn a node process
var child_process = require('child_process');
var child = child_process.spawn('node');

// Now get its pid.
child_process.exec('ps -p' + child.pid + ' -o vsize=',  function (err, stdout, stderr) {
  err = err || stderr;
  if (err) {
      return console.log('BAD Luck buddy: ', err);
  }
  console.log('YOU\'ve done it', parseInt(stdout, 10));
});

ubuntu 12.04 OS X lion. , .

0

All Articles