Node cluster workers memoryUsage

我是研究僧i 提交于 2021-01-27 17:01:56

问题


Does anyone know if there is a platform independent way to get memory usage of a worker? I would expect it would work like this:

console.log('App process memoryUsage: ',process.memoryUsage());
cluster.on('online',function(worker){    // doesn't work! 
  console.log('Workers memory usage: ',worker.process.memoryUsage());  
});

But the workers process hasn't the method memoryUsage().

Is there a valid reason this isn't implemented ?

The only idea to realize this is to work with unix top -pid 1234 (macosx) or top -p 1234 on linux. And switching by process.plattform.


回答1:


Yes indeed you cannot get memoryUsage from worker's process property. I am not sure why it's not implemented, but you can achieve the same with this approach:

var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
  cluster.on('exit', function(worker, code, signal) {
    console.log('worker ' + worker.process.pid + ' died');
  });
  cluster.on('online', function(worker, code, signal) {
    worker.send('memoryUsage');
    worker.on('message', function(dd) {
      if (dd.event == 'memoryUsage') {
        console.log("Worker with ID: %d consumes %imb of memory", worker.id, dd.data.heapTotal / 1024 / 1024);
      }
    });
  });
  console.log("Master consumes %imb of memory", process.memoryUsage().heapTotal / 1024 / 1024);
} else {
  if (cluster.worker.id == 1) {
    a = [];
    for (var i = 0; i < 1000000; i++) {
      //just to see the difference in memory usage.
      a.push(Number.MAX_SAFE_INTEGER);
    };
  }
  process.on('message', function(msg) {
    if (msg == 'memoryUsage') {
      process.send({
        event: msg,
        data: process.memoryUsage()
      });
    }
  });
}

Output on 8-core system:

user$ node ClusterTest.js 
Master consumes 6mb of memory
Worker with ID: 2 consumes 5mb of memory
Worker with ID: 6 consumes 5mb of memory
Worker with ID: 3 consumes 5mb of memory
Worker with ID: 4 consumes 5mb of memory
Worker with ID: 7 consumes 5mb of memory
Worker with ID: 5 consumes 5mb of memory
Worker with ID: 8 consumes 5mb of memory
Worker with ID: 1 consumes 39mb of memory
^C
user$


来源:https://stackoverflow.com/questions/31168351/node-cluster-workers-memoryusage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!