简体   繁体   中英

Is there a memory leak in this function?

It seems as though I have a memory leak in the following function, as the script eats tons of memory as this runs. Can anyone spot a memory leak?

Base.prototype.optimize = function(configurations, data, investment, profitability, callback) {
    var self = this;

    process.stdout.write('Optimizing...');

    // Exclude configurations that have already been backtested.
    self.removeCompletedConfigurations(configurations, function() {
        var configurationCompletionCount = -1;
        var configurationsCount = configurations.length;

        async.each(configurations, function(configuration, asyncCallback) {
            configurationCompletionCount++;
            process.stdout.cursorTo(13);
            process.stdout.write(configurationCompletionCount + ' of ' + configurationsCount + ' completed');

            // Instantiate a fresh strategy.
            var strategy = new self.strategyFn();

            // Backtest the strategy using the current configuration and the pre-built data.
            var results = strategy.backtest(configuration, data, investment, profitability);

            // Record the results.
            var backtest = {
                symbol: self.symbol,
                strategyName: strategy.constructor.name,
                configuration: configuration,
                profitLoss: results.profitLoss,
                winCount: results.winCount,
                loseCount: results.loseCount,
                tradeCount: results.winCount + results.loseCount,
                winRate: results.winRate
            };
            Backtest.collection.insert(backtest, function(error) {
                // Ensure memory is freed.
                strategy = null;
                results = null;
                backtest = null;

                asyncCallback(error);
            });
        }, function(error) {
            if (error) {
                console.log(error.message || error);
            }
            process.stdout.cursorTo(13);
            process.stdout.write(configurationsCount + ' of ' + configurationsCount + ' completed\n');
            callback();
        });
    });
};

And pointers on fixing it would be great!

I guess the async.each works on the copies rather that actual objects. so multiple copies are may be causing a memory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM