简体   繁体   English

Node.js程序中的内存泄漏

[英]Memory Leak in Node.js Program

I'm designing a daemon (specifically a worker process for Heroku) and the code I have slowly accumulates in ram usage. 我正在设计一个守护进程(特别是Heroku的一个工作进程)和我在ram使用中慢慢累积的代码。 It's a small amount (100kb/10 seconds), but I know that for long-term use of this process, its best to keep the ram usage consistent. 它是一个小量(100kb / 10秒),但我知道,对于长期使用这个过程,最好保持ram使用一致。 I am new to creating processes designed to run a long time and still pretty new to developing applications in Node and Javascript. 我是新手创建的程序,设计为运行很长时间,并且仍然是在Node和Javascript中开发应用程序的新手。 Here is most of my code: 这是我的大部分代码:

////////////////
// Queue Loop //
////////////////
var worker = (function() {
var buffer = "";
var couchdb = http.get({
    auth: db_user + ":" + db_pass,
    host: db_host, 
    path: '/queue/_all_docs?include_docs=true',
    port: db_port
}, function( response ) {
    response.setEncoding('utf8');

    response.on("data", function( data ) {
        buffer += data;
    });

    response.on("end", function() {         
        var queue = JSON.parse(buffer);

        if ( !queue.error ) {

            buffer = "";
            couchdb = http.get({
                auth: db_user + ":" + db_pass,
                host: db_host,
                path: '/flights/_all_docs?include_docs=true',
                port: db_port
            }, function( response ) {
                response.setEncoding('utf8');

                response.on("data", function(data) {
                    buffer += data;
                });

                response.on("end", function() {
                    var flights = JSON.parse(buffer);

                    if ( !flights.error ) {
                        for ( row in queue.rows ) {
                            console.log(queue.rows[row].doc.flight);
                        }

            for ( row in flights.rows ) {
                console.log(flights.rows[row].doc.program);
                }

                setTimeout( worker, 10000 );    
            } else {
                // db error
            }
            });
        }).on("error", function() {
            setTimeout( worker, 10000 );    
        });
            } else {
                // db error
            }
        });
    }).on("error", function() {
        console.error("CouchDB is currently not available.");
        setTimeout( worker, 10000 );
    });
});



///////////////
// Run Queue //
///////////////
(function() {
    worker();
})();

So with that, what is the best way to keep a program like this running constantly with the same amount of ram? 那么,使用相同数量的ram来保持这样的程序不断运行的最佳方法是什么? What other design considerations are essential to keeping this running well enough to serve as a worker process on Heroku? 还有哪些其他设计注意事项对于保持其良好运行以充当Heroku上的工作进程至关重要?

Oh and as for what it does, it pulls some entries out of a CouchDB instance and then it runs a prediction based on those entries. 哦,至于它做什么,它从CouchDB实例中提取一些条目,然后它根据这些条目运行预测。

It turns out that Node.js offers a nice function called process.nextTick() that clears the call stack (edit: this may not be the case, but it helps things from accumulating). 事实证明,Node.js提供了一个很好的函数叫做process.nextTick()来清除调用堆栈(编辑:这可能不是这种情况,但它可以帮助积累事物)。 By wrapping all of my calls to worker in it, I could prevent a theoretical stack overflow (which was not happening, but my memory usage was continuously growing). 通过将我的所有调用包装到其中的worker,我可以防止理论堆栈溢出(这没有发生,但我的内存使用量不断增长)。

My understanding of this function is not very deep, but it does what I have been wanting to do. 我对这个功能的理解不是很深刻,但它确实做了我一直想做的事情。

(edit: Turns out node has FANTASTIC garbage collection via V8, it runs at strange intervals though!) (编辑:结果节点通过V8进行FANTASTIC垃圾收集,虽然它以奇怪的间隔运行!)

Reads: 阅读次数:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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