Next.js Discord

Discord Forum

Memory Leak without doing anything

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
Situation:
- Next.js 13.4.4 (pages)
- All pages are SSR, no SSG included.
- NextAuth used as auth layer
- URQL used for Graphql calls

Problem:
Our Next.js project hosted in Google Cloud through kubernetes just keeps up building memory (without anyone doing something on the website) and restarts the pod when the memory limit has reached. (I've provided a screenshot). Back in the old days we also had this problem, but adding "sharp" as package and making all images "unoptimized" fixed this problem for us.

Now I cant figure out how to solve this problem, because we already updated to the latest packages. Anyone an idea? MAybe a direction we might can search? Thanks!

1 Reply

Hello! The issue that most people consider to be a leak is actually an accumulation of the file system cache in Linux.

If you examine the Nextjs caching algorithm in detail, you will see that it does not remove files that have already "expired" based on the expiredAt value. The Linux operating system, to optimize file access, tries to keep metadata (inodes) in RAM.

We observe this behavior because most often our application is deployed in an isolated container in a production environment on a cluster with large memory resources, and there are no parallel processes putting pressure on the RAM; as a result, the memory tries to fill up until it starts affecting performance.

I have created a script that automatically cleans up some of the cache files—you can give it a try.
Link: https://github.com/VadimOnix/next-image-cache-cleaner

If this helps you, I would be grateful for your star on my repository ✌️