12

I have used "os" http://nodejs.org/api/os.html#os_os To attempt to calculate some system stats for use in an app.

However I notice that it cannot actually calculate the memory properly, because it leaves out the cache and buffers witch is needed to properly calculate a single readable percentage. Without it the memory will almost always be 90%+ with most high performance servers (based on my testing).

I would need to calculate it like so:

(CURRENT_MEMORY-CACHED_MEMORY-BUFFER_MEMORY)*100/TOTAL_MEMORY

This should get me a more accurate % of memory being used by the system. But the os module and most other node.js modules I have seen only get me total and current memory.

Is there any way to do this in node.js? I can use Linux but I do not know the ins and outs of the system to know where to look to figure this out on my own (file to read to get this information, like top/htop).

Jordan Ramstad
  • 169
  • 3
  • 8
  • 37

2 Answers2

5

Based on Determining free memory on Linux, Free memory = free + buffers + cache.

Following example includes values derived from node os methods for comparison (which are useless)

var spawn = require("child_process").spawn;
var prc = spawn("free", []);
var os = require("os");

prc.stdout.setEncoding("utf8");
prc.stdout.on("data", function (data) {
    var lines = data.toString().split(/\n/g),
        line = lines[1].split(/\s+/),
        total = parseInt(line[1], 10),
        free = parseInt(line[3], 10),
        buffers = parseInt(line[5], 10),
        cached = parseInt(line[6], 10),
        actualFree = free + buffers + cached,
        memory = {
            total: total,
            used: parseInt(line[2], 10),
            free: free,
            shared: parseInt(line[4], 10),
            buffers: buffers,
            cached: cached,
            actualFree: actualFree,
            percentUsed: parseFloat(((1 - (actualFree / total)) * 100).toFixed(2)),
            comparePercentUsed: ((1 - (os.freemem() / os.totalmem())) * 100).toFixed(2)
        };
    console.log("memory", memory);
});

prc.on("error", function (error) {
    console.log("[ERROR] Free memory process", error);
});

Thanks to leorex.

Check for process.platform === "linux"

hacklikecrack
  • 1,360
  • 1
  • 16
  • 20
4

From reading the documentation, I am afraid that you do not have any native solution. However, you can always call 'free' from command line directly. I put together the following code based on Is it possible to execute an external program from within node.js?

var spawn = require('child_process').spawn;
var prc = spawn('free',  []);

prc.stdout.setEncoding('utf8');
prc.stdout.on('data', function (data) {
  var str = data.toString()
  var lines = str.split(/\n/g);
  for(var i = 0; i < lines.length; i++) {
     lines[i] = lines[i].split(/\s+/);
  }
  console.log('your real memory usage is', lines[2][3]);
});

prc.on('close', function (code) {
    console.log('process exit code ' + code);
});
Community
  • 1
  • 1
leorex
  • 2,058
  • 1
  • 14
  • 15
  • I think `lines[2][3]` in this snippet returns the [intersection of the 'buffers/cache' line with the 'free' column](http://stackoverflow.com/a/23542090/680454) – michielbdejong Apr 03 '15 at 09:01