WellWellWell
WellWellWell

Reputation: 161

Nodejs/Javascript Getting Process Memory of any process

I am looking for a way of getting the process memory of any process running.

I am doing a web application. I have a server (through Nodejs), my file app.js, and an agent sending information to app.js through the server.

I would like to find a way to get the process memory of any process (in order to then sending this information to the agent) ? Do you have any idea how I can do this ? I have searched on google but I haven't found my answer :/

Thank you

PS : I need a windows compatible solution :)

Upvotes: 1

Views: 1918

Answers (1)

Paul Rad
Paul Rad

Reputation: 4882

Windows

For windows, use tasklist instead of ps

In the example below, i use the ps unix program, so it's not windows compatible.

Here, the %MEM is the 4st element of each finalProcess iterations.

On Windows the %MEM is the 5th element.

var myFunction = function(processList) {
  // here, your code
};

var parseProcess = function(err, process, stderr) {
    var process = (process.split("\n")),
        finalProcess = [];

    // 1st line is a tab descriptor
    // if Windows, i should start to 2
    for (var i = 1; i < process.length; i++) {
        finalProcess.push(cleanArray(process[i].split(" ")));
    }

    console.log(finalProcess);
    // callback to another function
    myFunction(finalProcess);
};

var getProcessList = function() {

    var exec = require('child_process').exec;
    exec('ps aux', parseProcess.bind(this));
}

// thx http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}


getProcessList();

Upvotes: 2

Related Questions