I thought that I understood how node / v8 memory management works but evidently not.
The following program exhibits an rss that grows more or less constantly while the heapUsed grows and then shrinks when the garbage collector runs.
A recent test had the RSS start at ~10 MB and grow to ~14MB over the course of 7 minutes. Then it continues to grow, hour after hour.
I've tried setting all the vars within report() to null and I've tried deleting them but neither of these has any effect.
I've tried manually invoking the garbage collector. That slows the growth but does not eliminate it.
Could someone explain why this is? Is this normal behavior and I'm just not waiting long enough for the gc to work on the RSS? This is just one simple example of software I've written that exhibits similar behavior.
var startTime = new Date();
var memInfo = {
rss: 0,
heapTotal: 0,
heapUsed: 0
}
function report () {
var endTime, timeDiff, seconds, minutes, hours, days, mem;
endTime = new Date();
timeDiff = (endTime - startTime) / 1000;
seconds = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
minutes = Math.round(timeDiff % 60);
timeDiff = Math.floor(timeDiff / 60);
hours = Math.round(timeDiff % 24);
timeDiff = Math.floor(timeDiff / 24);
days = timeDiff;
console.log('Up for %d days %d hours %d minutes %d seconds', days, hours, minutes, seconds);
var mem = process.memoryUsage();
if (mem.rss > memInfo.rss) { memInfo.rss = mem.rss; }
if (mem.heapTotal > memInfo.heapTotal) { memInfo.heapTotal = mem.heapTotal; }
if (mem.heapUsed > memInfo.heapUsed) { memInfo.heapUsed = mem.heapUsed; }
console.log('Latest', process.memoryUsage());
console.log('Max ', memInfo);
};
var timer = setInterval(report, 1000);
An even simpler example that does the same thing is:
var memInfo = {
rss: 0,
heapTotal: 0,
heapUsed: 0
}
var timer = setInterval(function () {
var mem = process.memoryUsage();
if (mem.rss > memInfo.rss) { memInfo.rss = mem.rss; }
if (mem.heapTotal > memInfo.heapTotal) { memInfo.heapTotal = mem.heapTotal; }
if (mem.heapUsed > memInfo.heapUsed) { memInfo.heapUsed = mem.heapUsed; }
console.log('Latest', process.memoryUsage());
console.log('Max ', memInfo);
}, 1000);