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 usage extension 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:

image

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

NEXT-2023

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 26
  • Comments: 33 (2 by maintainers)

Commits related to this issue

Most upvoted comments

image

Persist in 13.4.7.

Three solutions worked:

  1. add to next.config.js
images: {
 unoptimized: true
},
  1. Change all next.js <Image> to <img>
  2. Add to all next.js <Image> param unoptimized

image

Generally 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. image

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:jammy base image (glibc).

I don’t see the memory leak when using the the recommended node:18-alpine base image (musl).

This is likely related to sharp and documented here:

For reference, I downgraded to 15.1.0 and the next/og memory 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/og causes 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.

image

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 for next/image anymore.

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 ✌️

I’m running it with unoptimized: true. The RAM usage still keep rising. My guess is OG images created by next/og aren’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.

can we re-open #65451?

cc: @leerob (idk who to tag 😅)

cc @huozhi

The memory leak persists on node 18 alpine, here’s the scenario for new nextjs app:

  1. Create nextjs-latest app
  2. Take a large image (~10MB for my case) and put it into public folder
  3. Render that image with next/image on home page
  4. deploy this to docker with node:18.20-alpine
  5. Open home page - this will cause a memory spike to ~2GB. After a bit it will fall to ~1GB and stay there
  6. Open the page in incognito tab and keep refreshing the main tab and the incognito - this will cause a second memory spike and memory usage will remain that way. A handy demo in case it’s needed: https://github.com/art-alexeyenko/next-image-oom

And some memory usage trends from my local tests with Docker Desktop: image

One year passed. Still open. Have the same image memory leak but for opengraph