2014-11-29 1 views
0

다음 힙을 지속적으로 늘리는 프로그램이 있습니다. require을 통해 외부 파일 (SyntaxError)을 반복적으로로드하는 매우 간단한 프로그램입니다. 외부 모듈이 구문 오류로 인해로드되지 않습니다.node.js - 오류가 발생하면 메모리 누수가 발생합니까?

노드 가비지 수집기를 실행하고 힙 크기를 덤프하여 메모리가 누수되는지 확인합니다. 외부 파일 (SyntaxError.js)에서 구문 오류를 수정하면 누수가 사라지고 메모리 사용량이 백만 회 반복 된 후에도 일정하게 유지됩니다.

노드 버전을 사용 : 0.10.29

--expose-gc으로 일어나고 어떻게 MEM 누출을 방지 할 수 있습니다 수 있습니다 무엇에 어떤 통찰력을 크게 감상 할 수있다.

테스트 코드 :

(function() { 
    var MAX_ITER_COUNT, MEGABYTE, dumpMem, gc, iterCount, noop, peakMem, test2; 

    iterCount = 0; 

    MAX_ITER_COUNT = 1000 * 1000; 

    peakMem = 0; 

    MEGABYTE = 1024 * 1024; 

    noop = function() {}; 

    dumpMem = function() { 
    var currentMem; 
    currentMem = Math.round(process.memoryUsage().heapUsed/MEGABYTE); 
    if (currentMem > peakMem) { 
     peakMem = currentMem; 
    } 
    return console.log("" + iterCount + " - memory is: " + currentMem + "/" + peakMem + " MB"); 
    }; 

    gc = function() { 
    return global.gc(); 
    }; 

    process.on("uncaughtException", function(err) { 
    return console.log("Unhandled exception! ", err, err.stack); 
    }); 

    test2 = function() { 
    var e; 
    iterCount++; 
    try { 
     /* 
     Load a module that has a syntax error to create mem leak, and ignore exception 
     */ 

     require("./SyntaxError"); 
    } catch (_error) { 
     e = _error; 
    } 
    gc(); 
    dumpMem(); 
    return setTimeout(test2, 0); 
    }; 

    test2(); 

}).call(this); 

그리고 SyntaxError.js :이 노드 버그처럼

(function() { 
    'use strict'; 
    /* y does not exist, and results in exception*/ 

    var x; 

    x = y; 

}).call(this); 

답변

1

보인다. SyntaxError.js 본문을 try/catch하여 내부에 넣을 수 있습니다. SyntaxError.js.

MEM-누출 bug.js : 위의에서 간단하게 내 테스트,

for (i=0; i<1000000; i++) { 
    try { 
     require('./mem-leak--error.js'); 
    } 
    catch (e) { } 

    if (i % 1000 === 0) { 
     global.gc(); 
     console.log(i, process.memoryUsage().heapUsed/1e6|0, process.memoryUsage().heapTotal/1e6|0); 
    } 
} 

MEM-누출 - error.js :
참고 : 에서 명시 적으로 반환이 있어야합니다

% node --expose-gc ar/mem-leak-bug.js 
0 1 5 
1000 2 14 
2000 2 14 
3000 3 14 
4000 3 23 
5000 3 23 
6000 4 24 
01 : catch 블록은, 다른 사람이 그것을 역시 메모리 누수는 시도 - 캐치없이 메모리

// throw an undefined variable exception 
try { 
    return x; 
} 
catch(e) { 
    return; 
} 

에게 누설 SytaxError 내부의 시도 - 캐치와 23,516,

, 그것은하지 않습니다

% node --expose-gc ar/mem-leak-bug.js 
0 1 5 
1000 1 7 
2000 1 7 
3000 1 7 
4000 1 7 
5000 1 7 
6000 1 7 
7000 1 7 
8000 1 7 
+0

감사 @Andras을. 이것은 나의 관측과 일치합니다. – PKK

관련 문제