Node.js garbage collection tip

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.

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.


Leave a Reply