NodeJS memory consumption in an infinite loop

前端 未结 3 902
我在风中等你
我在风中等你 2021-02-05 12:35

I don\'t know if this is a bug with Node or V8, but if I run the following code the node process leaks memory. The GC never seems to kick in and in a few seconds it\'s consuming

3条回答
  •  攒了一身酷
    2021-02-05 12:58

    As Node.js v0.10 has been released, setImmediate should be used as the first choice instead of process.nextTick when calling the recursive callback.

    function loginf() {
      console.log(1+1);
      setImmediate(loginf);
    }
    loginf();
    

    The memory consumption of this chunk of code kept low ( <10MB ) after running for about 15 mins on my computer.

    On the contrary, Running the infinite for loop caused memory leek and the process.nextTick throwed an Maximum call stack size exceeded error.

    Check this Q&A also: setImmediate vs. nextTick

提交回复
热议问题