How to request the Garbage Collector in node.js to run?

后端 未结 3 827
说谎
说谎 2020-11-30 00:58

At startup, it seems my node.js app uses around 200MB of memory. If I leave it alone for a while, it shrinks to around 9MB.

Is it possible from within the app to:

3条回答
  •  离开以前
    2020-11-30 01:22

    Node allows us to manually trigger Garbage Collection. This can be accomplished by running Node with --expose-gc flag (i.e. node --expose-gc index.js).
    Once node is running in that mode, you can programmatically trigger a Garbage Collection at any time by calling global.gc() from your program.

    ex -

    // Force garbage collection every time this function is called
    try {
      if (global.gc) {global.gc();}
    } catch (e) {
      console.log("`node --expose-gc index.js`");
      process.exit();
    }
    

提交回复
热议问题