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).
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With