ubuntunuxt.jsgarbage-collectionpm2

PM2 + Node.js + Ubuntu: Garbage collection does not work as expected


Edit 09.24: I found the culprit. I was cause by the usage of useAsyncData inside a pinia (setup) stores. Don't do that ⚠️

Original message:

We are having problems with our Production Nuxt3 app. It runs on PM2 (cluster mode) on an ubuntu VPS. The memory usage keeps growing over time.

When run locally (Node and PM2 installed on Windows or Mac), a stress test does fill the memory, but eventually the garbage collection seems to do its job:

PM2 monit, seconds after the stress test and one hour after the test: before and after stress test

When running on ubuntu, the memoy usage stays up. PM2 monit (production server, Ubuntu 22.04.4 LTS, node v20.14.0, PM2 5.4.0) after 17 hours of uptime:

ubuntu mem usage

As a result, we have to restart PM2 every couple of days. I tried using --optimize_for_size for Node. This reduced the overall memory usage, but not the growth.

I reproduced this behaviour on my Windows PC with WSL. After the stress test the memory usage stayed high. So the culprit must have something to do with Ubuntu. Any ideas? 🤔


Solution

  • It could be possible that you have a memory leak somewhere in your application. Check if you are not doing things that cannot be garbage collected on the server-side, like wrongly using plugins (see second article), creating intervals that are not cleaned up, etc.

    Here are some articles that could possibly help: