Node Async Looping - Why does the memory grow and just drop all of the sudden?

孤街醉人 提交于 2020-01-06 14:52:13

问题


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);
};

回答1:


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


来源:https://stackoverflow.com/questions/11472450/node-async-looping-why-does-the-memory-grow-and-just-drop-all-of-the-sudden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!