这个功能有内存泄漏吗?

时间:2015-10-15 09:54:36

标签: javascript node.js memory-leaks

似乎我在以下函数中有内存泄漏,因为脚本运行时会占用大量内存。有人能发现内存泄漏吗?

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

关于修复它的指示会很棒!

1 个答案:

答案 0 :(得分:1)

我猜async.each适用于副本而非实际对象。因此,多个副本可能会导致内存泄漏。