2016-11-01 4 views
0

RESTクライアントとして機能し、大きなJSONオブジェクトをリクエストする単純なNodeJsアプリケーションがあります。問題は、メモリが常に不足していることです(6Gb以上)。私は手動のガベージコレクションを使用しています(アプリケーションは--expose_gcで起動しました)が、それはあまり役に立たないようです。大きなJSONオブジェクトを要求するとメモリリークが発生する

は、ここに私のコードです:私は、要求のライブラリを試してみました

var needle = require('needle'); 
function getAllData() { 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 

    setInterval(function() { 
     getAllData(); 
    }, 10 * 1000); 
} 

function getDataFromUrl(url) { 
    needle.get(url, function (error, response) { 
     if (!error && response.statusCode == 200) { 
      console.log("do something"); 
     } 
    }); 
} 

function scheduleGc() { 
    global.gc(); 
    setTimeout(function() { 
     scheduleGc(); 
    }, 100 * 1000); 
} 

getAllData(); 
scheduleGc(); 

が、私は同じ結果が得られました。私は間違って何をしていますか?

p.s.私のnodejsバージョンは6.9.1、針バージョン1.3.0

答えて

0

漏れを見つけるためにノードインスペクタを使用してください。それは、針と要求からの内部ノードの漏れまたは漏れである可能性があります。私はネイティブhttp.requestを使用して、ライブラリの問題ではないことを確認したいと思います。また、コードを独立した部分に分割し、別々に実行して漏れの原因を見つけることもできます。

メモリリークを検出する方法はanother answerです。

0

エラーが見つかりました。私はそれはあなたがあまりにも多くの情報を使用して作業しているので、の6Gbはストリームで処理されなければならないのです....

var needle = require('needle'); 
function getAllData() { 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
    getDataFromUrl("http://puppygifs.tumblr.com/api/read/json"); 
} 

function getDataFromUrl(url) { 
    needle.get(url, function (error, response) { 
     if (!error && response.statusCode == 200) { 
      console.log("do something"); 
     } 
    }); 
} 

function scheduleGc() { 
    global.gc(); 

    setTimeout(function() { 
     scheduleGc(); 
    }, 100 * 1000); 
} 

setInterval(function() { 
    getAllData(); 
}, 10 * 1000); 

scheduleGc(); 
0

をするsetIntervalの代わりに、setTimeoutメソッドを使用していました。
で非常に簡単ですが、コールバックを避けるだけです。ここ
は一例です:JSONでの6Gbない

'use strict'; 
const needle = require('needle'); 
const fs = require('fs'); 
const out = fs.createWriteStream('bigFile.json'); 
needle.get('http://puppygifs.tumblr.com/api/read/json').pipe(out); 
+0

、それははるかに少ないです。 6Gbは、setTimeoutの代わりにsetIntervalを使用していたため、使用されたRAMの量でした... – Jkam