1

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 :)

4
  • Do you want the memory usage of any process, or any node process? Commented Mar 6, 2014 at 16:34
  • I want the memory usage of any process Commented Mar 6, 2014 at 16:35
  • You want the process memory size or the actual data? Commented Mar 6, 2014 at 16:57
  • hmmm (I think) I would like the actual data. Trying to be clear, I want to display on a graph the evolution of the % of the memory used by the process (each second, I want the % of memory). Do you see what I mean ? Commented Mar 6, 2014 at 17:04

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

6 Comments

arf, I should have specified I am working with windows But thank you for your answer, I hope it would help other people :)
Updated answer, it should work on Windows with this little fix (use tasklist instead of ps).
and may I bother you one last time ? :) How can I put the array in a variable, to use it after ? Because I have tried to put some " return" in parseProcess and getprocessList, but it is not working and I always retrieve an object with stdout etc. Thank you
You need to set a callback in parseProcess (view edit)
Vote up for this answer please
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.