next.js: Another Memory Leak in `next/image`
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:03:51 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T6000 Binaries: Node: 19.0.1 npm: 9.2.0 Yarn: 1.22.19 pnpm: 7.21.0 Relevant packages: next: 13.1.1 eslint-config-next: 13.1.1 react: 18.2.0 react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
Image optimization (next/image, next/legacy/image)
Link to the code that reproduces this issue
https://github.com/jaunruh/next-image-test
To Reproduce
Set up docker image:
docker build --no-cache -t next-test .docker run -p 3000:3000 --name next-test next-test
Surf the app:
- open browser at
localhost:3000 - scroll the home page and the project pages randomly
- use the
Resource usageextension to monitor the memory usage - see the memory only increase and never decrease
Describe the Bug
The apps memory usage only ever increases and never really decreases anymore. On docker I have easily managed to pass 1gb in memory usage. The app was run on digitalocean as a bare nodejs app and as a docker container locally and in digitalocean. The memory usage in digitalocean (1 CPU, 512mb memory) looks as follows, but can also be reproduced with docker:

In the provided repo there are also commented-out image tags. As soon as these are used the problem disappears.
Expected Behavior
No continuous memory increase.
Which browser are you using? (if relevant)
All major browsers
How are you deploying your application? (if relevant)
docker run and build
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 26
- Comments: 33 (2 by maintainers)
Commits related to this issue
- fix: disable image optimization Image optimization causes high memory usage, [source](https://github.com/vercel/next.js/issues/44685). Temporarily disabling this until a fix is found. Manually compre... — committed to GTD-IT-XXIV/gtd-xxvi-website by gillwong 3 years ago
- fix: disable image optimization Image optimization causes high memory usage, [source](https://github.com/vercel/next.js/issues/44685). Temporarily disabling this until a fix is found. Manually compre... — committed to GTD-IT-XXIV/gtd-xxvi-website by gillwong 3 years ago
- fix: disable image optimization Image optimization causes high memory usage, [source](https://github.com/vercel/next.js/issues/44685). Temporarily disabling this until a fix is found. Manually compre... — committed to GTD-IT-XXIV/gtd-xxvi-website by gillwong 3 years ago
- feat(next/image)!: remove `squoosh` in favor of `sharp` as optional dependency (#63321) ## History Previously, we added support for `squoosh` because it was a wasm implementation that "just worke... — committed to vercel/next.js by styfle 3 years ago
- feat(next/image)!: remove `squoosh` in favor of `sharp` as optional dependency (#63321) ## History Previously, we added support for `squoosh` because it was a wasm implementation that "just worke... — committed to vercel/next.js by styfle 3 years ago
Persist in 13.4.7.
Three solutions worked:
<Image>to<img><Image>paramunoptimizedGenerally using unoptimized images seems to fix the memory leak. But well, the images are unoptimized… so I still think this is a bug that needs to be fixed.
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 ✌️
Here is our memory chart when using the Next 14.2.3. I confirmed it has a memory leak issue with open graph-image and twitter-image. From 27/6, we disable this function, and this chart becomes normal.
Solution
Try not to use dynamically generated metadata images using tsx,js,ts.
The memory leak seems to only be present when running in
ubuntu:jammybase image (glibc).I don’t see the memory leak when using the the recommended
node:18-alpinebase image (musl).This is likely related to
sharpand documented here:For reference, I downgraded to 15.1.0 and the
next/ogmemory leak issue seems to be gone.Can we get some help here? @leerob
EDIT: Nope, problem is back even on 15.1.0.
ImageResponse and FigmaImageResponse that means
@vercel/ogcauses memory leak?That repo is missing sharp.
You need to run “npm install sharp” for production.
I am seeing similar in 13.4.1.
Deployed a small website to Digital Ocean that uses RSC and Image. There’s maybe 50 mid-size images total in this entire web-site.
In the screenshot, after the black line is after image optimisation disabled in config. The other dips in the graph before that point are due to server restarts.
As per a warning in the NextJS documentation I am using jemalloc in my deployment.
I have the same issue with using nextjs Image. I am using nextjs 13.1.1 and docker container will be killed. my question is that if the solution is setting the images unoptimized, so is there still any benefit of using nextjs Image instead of usual Html img?
I have seen this option. I am going to try it in my repo. But even if it solves the memory leak problem, it creates the issue of unoptimized images. I can then just use a general
<img/>tag instead. No need fornext/imageanymore.I’m running it with
unoptimized: true. The RAM usage still keep rising. My guess is OG images created bynext/ogaren’t being cleaned up as well. I think source of the issue is not relevant though. NextJS could let us configure that as well since most people hosting with Node don’t want their RAM usage to constantly rise regardless of how much RAM they have. I’m not sure if they do let you configure this already. I looked and couldn’t find anything.cc @huozhi
The memory leak persists on node 18 alpine, here’s the scenario for new nextjs app:
And some memory usage trends from my local tests with Docker Desktop:
One year passed. Still open. Have the same image memory leak but for opengraph