Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
149
rated 0 times [  155] [ 6]  / answers: 1 / hits: 89913  / 10 Years ago, fri, december 5, 2014, 12:00:00

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:




  1. Check how much memory the app is using ?

  2. 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 ?


More From » node.js

 Answers
32

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();
}

[#68572] Wednesday, December 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
laytonlamontm

Total Points: 745
Total Questions: 130
Total Answers: 130

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
;