Memory leak in express.js api application Memory leak in express.js api application express express

Memory leak in express.js api application


Analyse the problem more

You need to monitor the activity real time.

We do not have the code, thus we cannot even know what is going on. However, you can use more advanced tool like htop, gtop, netdata and others than top or ps.

The log on pm2 might also tell you more about things. On such situation, the logs will have more data than the process manager. Make sure to thoroughly investigate the logs to see if scripts are responsible, and throwing errors or not,

pm2 logs

Each api call will cost you

Calculate the cost early and prepare accordingly,

  • If you have 1 call, then be prepared to have 100Mb-1GB or more each time. It will cost you just like a browser tab. The cost will be there as long as the tab is open.
  • If the target website is heavy, then it will cost more. Some websites like Youtube will obviously cost you more.
  • Any script running inside the browser tab will cost cpu and memory usage.
  • Say each process is causing 300MB ram, If you don't close the process properly and start making API calls, then only 10 API call will be able to use 3GB ram pretty easily. It can add up pretty quickly.

Make sure to close the tabs

Whether the automation task is successful or not, make sure to properly use browser.close() to ensure the resource it is using gets free. Most of time we forget about such small things and it costs us.

Apply dumb init on docker to avoid ghost process

Something like dumb-init or tini can be used if you have a process that spawns new processes and you doesn't have good signal handlers implemented to catch child signals and stop your child if your process should be stopped etc.

Read more on this SO answer.


I've got the problem solved. It was caused by the underlying Kubernetes system, which wasn't configured with a resource limit on that specific container. Therefore, the container can consume as many memory as possible.

Now I've limited at 2GB an it looks like this:enter image description here