13

I know there is a api process.memoryUsage() to get memory usage in current process.

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

CALL ME TZ
  • 209
  • 1
  • 4
  • 10

3 Answers3

12

The easiest way to get child's memoryUsage is installing pidusage

Link: https://www.npmjs.com/package/pidusage

In the console write this to install it:

In Windows Command: npm i pidusage --save
In Mac Command : sudo npm i pidusage --save

let pidusage = require('pidusage');

const cp = require("child_process");

const child = cp.spawn('ls', ['-lh', '/usr']);

pidusage(child.pid, function (err, stats) {

console.log(stats);

});
/*
Output: 
{
   cpu: 10.0,            // percentage (from 0 to 100*vcore)
   memory: 357306368,    // bytes
   ppid: 312,            // PPID
   pid: 727,             // PID
   ctime: 867000,        // ms user + system time
   elapsed: 6650000,     // ms since the start of the process
   timestamp: 864000000  // ms since epoch
}
*/

If you want to get more than one child's memoryUsage you need to change child.pid for and array [child.pid,child2.pid], function (err, stats) ...

Juan Sanchez
  • 543
  • 7
  • 14
  • 2
    Actually this approach does not take into account case when child process itself creates processes. In this case it is better to use `pidusage-tree. – Rvach.Flyver Apr 29 '19 at 09:36
  • 1
    Is this a suitable approach to limiting a child process resource usage? For example, if a child process memory usage reaches x bytes, then I would like to stop the process. – Zach Smith Dec 13 '20 at 14:10
  • That depends on what you are doing, because if you have a process to do just a simple task then you won't need to do that, but if you got a web server for example that receive a lot of requests and you want to dedicate a specific amount of **memory or cpu** you can do so, nothing stop you from doing it. – Juan Sanchez Dec 15 '20 at 20:06
6

We can get multiplatform solution with using nodejs ipc protocol. you just need to setup event for requesting memory usage from parent process, and then send process.memoryUsage() from spawned child process.

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.js it might look like this

process.on('message', function(msg){
    if(msg === 'get_mem_usage'){
         process.send({memUsage: process.memoryUsage()});
    }
});
port115
  • 411
  • 4
  • 9
  • 2
    This only works when the child is also a Node script (`fork`), but the OP asked about an arbitrary process (`spawn`). – Coderer May 24 '21 at 10:57
1

Well you can use ps(uses /proc/<pid>/stat underlying) if you are in a unix environment. Here's an example:

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

This is tested with ubuntu 12.04 and OS X lion. Though don't think it'll work in windows.

Timothy Gu
  • 3,727
  • 28
  • 35
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115