NodeJS memory consumption in an infinite loop

前端 未结 3 962
我在风中等你
我在风中等你 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 13:09

    The answer by @VyacheslavEgorov seems right on but I would guess that deferring to the event loop would solve the problem. You might want to compare how your infinite for-loop compares to this infinite looping strategy:

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

    The idea is to use process.nextTick(cb) to defer to the event loop and (presumably) allow the GC to do its thing.

提交回复
热议问题