Skip to content

Commit

Permalink
Fix reported memory usage on Linux
Browse files Browse the repository at this point in the history
The os-utils module reports erroneous used memory percentage, in
particular, it considers memory used by the kernel's disk caching
subsystem as used memory, when it should in reality be considered free
as it is transparently let go when memory is low. See
http://linuxatemyram.com.

This changeset fixes this by detecting if we're on Linux, and if so,
runs `free -m` in a child process and screenscrapes its output. os-utils
does provide a .freeCommand() function which is supposed to be doing
this, but it also returns erroneous numbers.
  • Loading branch information
geomaster committed Jan 15, 2015
1 parent a05f893 commit a60d2b7
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions sensors/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

var os = require('os-utils');
var _os = require('os');
var child = require('child_process');

var plugin = {
/**
Expand All @@ -24,15 +26,31 @@ var plugin = {

initialized: false,


currentValue: 0,

isLinux: _os.platform().indexOf('linux') != -1,

/**
* Grab the current value, from 0-100
*/
poll: function() {
plugin.currentValue = (100 - Math.floor(os.freememPercentage() * 100));
var computeUsage = function(used, total) {
return Math.round(100 * (used / total));
};

if (plugin.isLinux) {
child.exec('free -m', function(err, stdout, stderr) {
var data = stdout.split('\n')[1].replace(/[\s\n\r]+/g, ' ').split(' ');

var used = parseInt(data[2]);
var total = parseInt(data[1]);
plugin.currentValue = computeUsage(used, total);
});
} else {
plugin.currentValue = Math.round((1 - os.freememPercentage()) * 100);
}

plugin.initialized = true;
}
};

module.exports = exports = plugin;
module.exports = exports = plugin;

0 comments on commit a60d2b7

Please sign in to comment.