Categories
JavaScript Answers

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

Spread the love

To request the Garbage Collector in node.js to run with Node.js, we call the global.gc method.

For instance, we write

try {
  global?.gc?.();
} catch (e) {
  console.log("`node --expose-gc index.js`");
  process.exit();
}

to call global.gc to request the garbage collector if it’s available.

We make the garbage collection available with the --expose-gc flag.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *