我正在用 Node 编写一个使用异步循环的程序。目标是让这个程序在 Heroku 上运行更长时间。正如预期的那样,它在内存中增长。但是一旦内存使用量达到大约 57MiB,它就会回落到 22MiB(它开始的地方)。是什么导致内存使用量突然下降?
这是我的代码,如果它有帮助的话。database.read
只是 的简化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);
};