Skip to content

Instantly share code, notes, and snippets.

@horatio-sans-serif
Created April 23, 2010 16:35
Show Gist options
  • Save horatio-sans-serif/376770 to your computer and use it in GitHub Desktop.
Save horatio-sans-serif/376770 to your computer and use it in GitHub Desktop.
$ node /tmp/foo.js
23 Apr 16:34:09 - system memory total 368864 KB
23 Apr 16:34:10 - peak RSS 6264 KB (2% of total)
23 Apr 16:34:10 - private memory (current) 5304 KB (1% of total)
23 Apr 16:34:11 - peak RSS 6752 KB (2% of total) (grew by 488 KB)
23 Apr 16:34:11 - private memory (current) 5796 KB (2% of total) (grew by 492 KB)
23 Apr 16:34:12 - peak RSS 6816 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:12 - private memory (current) 5864 KB (2% of total) (grew by 68 KB)
23 Apr 16:34:13 - peak RSS 6884 KB (2% of total) (grew by 68 KB)
23 Apr 16:34:13 - private memory (current) 5928 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:14 - peak RSS 6948 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:14 - private memory (current) 5992 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:15 - peak RSS 7012 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:15 - private memory (current) 6060 KB (2% of total) (grew by 68 KB)
23 Apr 16:34:16 - peak RSS 7080 KB (2% of total) (grew by 68 KB)
23 Apr 16:34:16 - private memory (current) 6124 KB (2% of total) (grew by 64 KB)
23 Apr 16:34:17 - peak RSS 7332 KB (2% of total) (grew by 252 KB)
23 Apr 16:34:17 - private memory (current) 6372 KB (2% of total) (grew by 248 KB)
23 Apr 16:34:18 - peak RSS 7332 KB (2% of total)
23 Apr 16:34:18 - private memory (current) 6372 KB (2% of total)
23 Apr 16:34:19 - ---- allocating big value! ----
23 Apr 16:34:19 - peak RSS 18064 KB (5% of total) (grew by 10732 KB)
23 Apr 16:34:19 - private memory (current) 16168 KB (4% of total) (grew by 9796 KB)
23 Apr 16:34:20 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:20 - private memory (current) 16176 KB (4% of total) (grew by 8 KB)
23 Apr 16:34:21 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:21 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:22 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:22 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:23 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:23 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:24 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:24 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:25 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:25 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:26 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:26 - private memory (current) 16176 KB (4% of total)
23 Apr 16:34:27 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:27 - private memory (current) 16212 KB (4% of total) (grew by 36 KB)
23 Apr 16:34:28 - peak RSS 18064 KB (5% of total)
23 Apr 16:34:28 - private memory (current) 16276 KB (4% of total) (grew by 64 KB)
23 Apr 16:34:29 - ---- allocating big value! ----
23 Apr 16:34:29 - peak RSS 23728 KB (6% of total) (grew by 5664 KB)
23 Apr 16:34:29 - private memory (current) 22768 KB (6% of total) (grew by 6492 KB)
23 Apr 16:34:30 - peak RSS 23732 KB (6% of total) (grew by 4 KB)
23 Apr 16:34:30 - private memory (current) 22772 KB (6% of total) (grew by 4 KB)
23 Apr 16:34:31 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:31 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:32 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:32 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:33 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:33 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:34 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:34 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:35 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:35 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:36 - peak RSS 23732 KB (6% of total)
23 Apr 16:34:36 - private memory (current) 22772 KB (6% of total)
23 Apr 16:34:37 - peak RSS 23764 KB (6% of total) (grew by 32 KB)
23 Apr 16:34:37 - private memory (current) 22804 KB (6% of total) (grew by 32 KB)
23 Apr 16:34:38 - peak RSS 23828 KB (6% of total) (grew by 64 KB)
23 Apr 16:34:38 - private memory (current) 22868 KB (6% of total) (grew by 64 KB)
23 Apr 16:34:39 - ---- allocating big value! ----
23 Apr 16:34:39 - peak RSS 30072 KB (8% of total) (grew by 6244 KB)
23 Apr 16:34:39 - private memory (current) 29112 KB (8% of total) (grew by 6244 KB)
23 Apr 16:34:40 - peak RSS 30076 KB (8% of total) (grew by 4 KB)
23 Apr 16:34:40 - private memory (current) 29116 KB (8% of total) (grew by 4 KB)
23 Apr 16:34:41 - peak RSS 30076 KB (8% of total)
23 Apr 16:34:41 - private memory (current) 29116 KB (8% of total)
23 Apr 16:34:42 - peak RSS 30076 KB (8% of total)
23 Apr 16:34:42 - private memory (current) 29116 KB (8% of total)
23 Apr 16:34:43 - peak RSS 30076 KB (8% of total)
23 Apr 16:34:43 - private memory (current) 29116 KB (8% of total)
23 Apr 16:34:44 - peak RSS 30076 KB (8% of total)
23 Apr 16:34:44 - private memory (current) 29116 KB (8% of total)
23 Apr 16:34:45 - peak RSS 30080 KB (8% of total) (grew by 4 KB)
23 Apr 16:34:45 - private memory (current) 29120 KB (8% of total) (grew by 4 KB)
23 Apr 16:34:46 - peak RSS 30080 KB (8% of total)
23 Apr 16:34:46 - private memory (current) 29120 KB (8% of total)
23 Apr 16:34:47 - peak RSS 30080 KB (8% of total)
23 Apr 16:34:47 - private memory (current) 29120 KB (8% of total)
23 Apr 16:34:48 - peak RSS 30080 KB (8% of total)
23 Apr 16:34:48 - private memory (current) 29120 KB (8% of total)
23 Apr 16:34:49 - ---- allocating big value! ----
23 Apr 16:34:49 - peak RSS 34212 KB (9% of total) (grew by 4132 KB)
23 Apr 16:34:49 - private memory (current) 33252 KB (9% of total) (grew by 4132 KB)
23 Apr 16:34:50 - peak RSS 34220 KB (9% of total) (grew by 8 KB)
23 Apr 16:34:50 - private memory (current) 33260 KB (9% of total) (grew by 8 KB)
23 Apr 16:34:51 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:51 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:52 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:52 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:53 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:53 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:54 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:54 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:55 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:55 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:56 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:56 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:57 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:57 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:58 - peak RSS 34220 KB (9% of total)
23 Apr 16:34:58 - private memory (current) 33260 KB (9% of total)
23 Apr 16:34:59 - ---- allocating big value! ----
23 Apr 16:34:59 - peak RSS 38360 KB (10% of total) (grew by 4140 KB)
23 Apr 16:34:59 - private memory (current) 37400 KB (10% of total) (grew by 4140 KB)
23 Apr 16:35:00 - peak RSS 38364 KB (10% of total) (grew by 4 KB)
23 Apr 16:35:00 - private memory (current) 37404 KB (10% of total) (grew by 4 KB)
^C
var
fs = require("fs"),
sys = require("sys");
if (!process.platform.match(/linux/i))
throw new Error("unsupported platform");
function getSystemMemoryTotal() {
var contents = fs.readFileSync("/proc/meminfo");
if (!contents) throw new Error("Failed to read /proc/meminfo");
var m = contents.match(/^MemTotal:\s*(\d+)/m);
return m ? parseInt(m[1], 10) : 0;
}
function getMemoryUsage(pid, callback) {
fs.readFile("/proc/" + pid + "/status", function (err, contents) {
if (err) {
callback(err, 0);
return;
}
var stats = {};
contents.split(/\n/g).forEach(function (line) {
var m = line.match(/^(Vm[^\t]+):\t\s*(\d+)/);
if (m) stats[m[1]] = parseInt(m[2], 10);
});
callback(null, stats);
});
}
function getPrivateMemoryUsage(pid, callback) {
fs.readFile("/proc/" + pid + "/smaps", function (err, contents) {
if (err) {
callback(err, 0);
return;
}
var privateMemory = 0;
contents.split(/\n/g).forEach(function (line) {
var m = line.match(/^Private_(?:Dirty|Clean):\s*(\d+)/);
if (m) privateMemory += parseInt(m[1], 10);
});
callback(null, privateMemory);
});
}
var count=0;
var bigValues = []; // to store references
var previousPeakRSS = 0;
var previousPrivate = 0;
var totalMemory = getSystemMemoryTotal();
sys.log("system memory total " + totalMemory + " KB");
setInterval(function () {
getMemoryUsage(process.pid, function (err, stats) {
if (err) throw err;
var peakRSS = stats.VmHWM;
if (previousPeakRSS == 0)
previousPeakRSS = peakRSS;
var delta = peakRSS - previousPeakRSS;
previousPeakRSS = peakRSS;
if (delta > 0) delta = "(grew by " + delta + " KB)";
else if (delta < 0) delta = "(shrunk by " + delta + " KB)";
else delta = "";
var peakPercentage = Math.round((peakRSS / totalMemory) * 100) + "% of total";
sys.log("peak RSS " + peakRSS + " KB (" + peakPercentage + ") " + delta);
});
getPrivateMemoryUsage(process.pid, function (err, currentPrivate) {
if (err) throw err;
if (previousPrivate == 0)
previousPrivate = currentPrivate;
var delta = currentPrivate - previousPrivate;
previousPrivate = currentPrivate;
if (delta > 0) delta = "(grew by " + delta + " KB)";
else if (delta < 0) delta = "(shrunk by " + delta + " KB)";
else delta = "";
var privatePercentage = Math.round((currentPrivate / totalMemory) * 100) + "% of total";
sys.log("private memory (current) " + currentPrivate + " KB (" + privatePercentage + ") " + delta);
});
if (++count % 10 == 0) {
sys.log("---- allocating big value! ----");
var x = [];
for (var i=0;i<1000000;++i)
x.push('a');
bigValues.push(x);
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment