Rainbow Coding

Introducing Node.js garbage collection tip

If you are doing large data processing with node.js you might run into issues with memory. There is a way to manually trigger garbage collection to help with memory problems.

Some node.js libraries leak memory or do not trigger garbage collection properly. This is very common problem especially when running them in a micro sized servers that have less than 2GB memory. For example when crawling web or using loops to process data can cause your long running script can hog all the memory in the system and cause itself to be killed. There is however easy solution for this. When running the script, start it with expose-gc parameter.

node --expose-gc script.js

expose-gc parameter exposes gc handling to the script. Then, at the end of bigger loops in the script you can manually trigger the garbage collection.

© RainbowCoding 2010-2023