0

我正在用 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);
};
4

1 回答 1

1

这与 V8 垃圾收集有关。您可以使用 node 的 '--gc_interval ' 选项对其进行调整,但请注意这是一个高级参数。

node --gc_interval <allocation count interval>

这也可能与GC的堆压缩有关。这是收集所有先前释放的空间并最终返回给操作系统的过程。

如需更多调整,您可以使用 V8 特定选项进行试验:

node --v8-options
于 2012-07-13T14:56:45.557 回答