ChatGPT解决这个技术问题 Extra ChatGPT

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

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:

Check how much memory the app is using ? Request the garbage collector to run ?

The reason I ask is, I load a number of files from disk, which are processed temporarily. This probably causes the memory usage to spike. But I don't want to load more files until the GC runs, otherwise there is the risk that I will run out of memory.

Any suggestions ?

How are you loading these files? If you need to load a huge file, you must use the Stream approach and you can process the file while still reading. Have you consider it?
@well That was 3 years ago. I'm working on some other project now and can't remember much about it.

e
evandrix

If you launch the node process with the --expose-gc flag, you can then call global.gc() to force node to run garbage collection. Keep in mind that all other execution within your node app is paused until GC completes, so don't use it too often or it will affect performance.

You might want to include a check when making GC calls from within your code so things don't go bad if node was run without the flag:

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

Where the docs for "--expose-gc"? I am not find any docs in nodejs.org.
@pea3nut it's a option of the V8 runtime, which node uses. This and other options can be listed with node --v8-options
You have to pass true to the function for full GC otherwise it will just do a minor GC
FWIW, passing true (these days, at least) effects a minor collection: github.com/nodejs/node/blob/…
When would the catch block run? If --expose-gc is missing then global.gc will just be undefined
l
lwang135

One thing I would suggest, is that unless you need those files right at startup, try to load only when you need them.

EDIT: Refer to the post above.


I need the files at startup. And actually, once the GC runs, my app uses only 9MB of memory for the lifetime of its existence. All I do is require a JSON object (that is a dictionary). Since this code is internal to node.js I can't optimise it. But I need the dictionary to be available for search suggestions.
I'm a bit confused. How does it go from almost no memory down to 9MB if you need this object for search suggestions? I assume those should remain for the lifetime for your app..
what do you mean by almost no memory ? Yes I "require" the json file doing something like var foo = require('./mydictionary'), and it exists for the lifetime of my app. At startup the memory usage of my node.js app goes up to 200MB! (I don't understand why you say almost no memory), and then after some time goes down to 9MB, even though the dictionary is still in memory.
Sorry I asaid "almost no memory" because you said " But I don't want to load more files until the GC runs, otherwise there is the risk that I will run out of memory." Perhaps the right thing to do, is to look for what makes your app go to 200MB. If the dictionary is in the memory, that shouldn't be the cause of it.
Its just a require statement require('./foo'). JSON object with lists of strings. My server has just 1GB of memory, so I'm just playing it safe.