2016-11-01 2 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

오류가 발견되었습니다. 난 당신이 너무 많은 정보로 작업 때문입니다

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

....에서는 setTimeout 대신하여 setInterval을 사용했는데, 6 기가 바이트는 스트림으로 처리해야한다.
바늘은 매우 간단합니다. 콜백을 피하십시오.

'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

json으로는 많이 이하, 6 기가 바이트되지 않습니다 :
다음은 예입니다. 6Gb는 setTimeout 대신 setInterval을 사용했기 때문에 사용 된 RAM의 양이었습니다 ... – Jkam