I'm writing a program in Node that uses an async loop. The goal is to get this program to run on Heroku for an extended period of time. It grows in memory, as expected. But then once the memory usage hits about 57MiB, it drops back down to 22MiB (where it started). What causes the memory usage to drop out of nowhere like that?
Here is my code, if it helps at all. database.read
is just a simplification of http.request
.
var http = require("http");
var util = require('util');
var fnstraj = require("./predictors/fnstraj.js");
var database = require("./library/database.js");
var COUNT = 0;
////////////////
// Queue Loop //
////////////////
var worker = function() {
setTimeout(function() {
COUNT++;
console.log("Worker Clock: " + COUNT + ".");
console.log(util.inspect(process.memoryUsage()));
database.read('/queue/', function( results, error ) {
if ( typeof error !== "undefined" && error ) {
process.nextTick( worker );
} else {
var queue = results;
database.read('/flights/', function ( results, error ) {
if ( typeof error !== "undefined" && error ) {
process.nextTick( worker );
} else {
var flights = results;
if ( !flights.error && typeof queue.rows[0] !== "undefined" ) {
for ( flight in flights.rows ) {
if ( flights.rows[flight].doc._id === queue.rows[0].doc._id ) {
var thisFlight = flights.rows[flight].doc;
console.log("Flight " + thisFlight._id + " started");
thisFlight.duration = fnstraj.vertPred(thisFlight.launch.altitude, thisFlight.balloon.burst, thisFlight.balloon.radius, thisFlight.balloon.lift);
fnstraj.predict(thisFlight, function() {
database.remove('/queue/' + thisFlight._id);
console.log("Flight " + thisFlight._id + " completed");
process.nextTick( worker );
});
var found = true;
}
}
if ( !found ) {
process.nextTick( worker );
}
}
}
});
}
});
}, 25);
};
This is related to the V8 Garbage collection. You may tweak it with the '--gc_interval ' option of node but note that this is an advanced parameter.
node --gc_interval <allocation count interval>
This may also be related to the heap compression of the GC. Tis is the process of gathering all the previously freed space and eventually give is back to the Operating System.
for more tweaks, you can experiments with V8 specific options:
node --v8-options