Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate memory usage with Image Optimization enabled #54482

Open
1 task done
timneutkens opened this issue Aug 24, 2023 · 18 comments
Open
1 task done

Investigate memory usage with Image Optimization enabled #54482

timneutkens opened this issue Aug 24, 2023 · 18 comments
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization.

Comments

@timneutkens
Copy link
Member

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Latest version of Next.js.

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 or a replay of the bug

Reproduction was left out in reports in #49929

To Reproduce

Unfortunately we haven't received a reproduction of these reports so this needs a reproduction in order to investigate. We'll keep this issue open for the standard 30 days before closing it if there is no reproduction provided.

Describe the Bug

In #49929 some people reported high memory usage / some claim a memory leak when image optimization is enabled. Unfortunately we haven't been able to verify this as there is no reproduction provided for these, only monitoring tool screenshots.

We'll keep this issue open temporarily hoping that someone will provide a reproduction so that it can be investigated.

Expected Behavior

Unclear as of right now, as there is no reproduction yet.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@timneutkens timneutkens added the bug Issue was opened via the bug report template. label Aug 24, 2023
@github-actions github-actions bot added the Image (next/image) Related to Next.js Image Optimization. label Aug 24, 2023
@AlphaBravoIndia
Copy link

Hey Tim,

I've been looking into this for our site that is deployed on Railway and realized that it may be purely driven by the fact that optimized images are stored in memory. If a site has a lot of images, I assume that the build up of optimized images could look like a memory leak? Our images aren't huge but we do have quite a few, say 10,000, and this is what our memory usage looks like over a few hours with optimized images enabled:

CleanShot 2023-08-31 at 16 28 41@2x
CleanShot 2023-08-31 at 16 28 59@2x

This builds up to like 7gb over a ~24 hour period which then forces us to restart the server.

A similar thing happens in this reproduction: https://github.com/AlphaBravoIndia/next-image-memory-leak

Whenever I refresh the page a bunch of times it generates more optimized images and memory usage spikes and stays higher than the previous point. This makes me think that it's purely the optimized images pushing memory usage up rather than a leak. If I stop refreshing the page, the memory usage stays consistent (at the new level).

CleanShot 2023-08-31 at 16 39 18@2x

Should optimized images be dumped from memory after a certain amount of time automatically or is this something that we need (or can) configure?

Cheers!

@miketdonahue
Copy link

miketdonahue commented Sep 14, 2023

image
(Digital Ocean App Platform - Next.js Deploy - v13.4.20-canary.16)

I have far less images, call it maybe 30 on the site. The more I click around (and the more images that load, presumably sitting in memory), the higher the memory becomes. It eventually grinds to a halt making <Image> unusable. I am using <Image> in a very straight forward way. The site is basically a blog (for all intents and purposes).

@csi-lk
Copy link
Contributor

csi-lk commented Sep 14, 2023

Can confirm we are seeing this issue in production on ^13.4.19, if we start curl'ing an image it will cause it to 500 after a short period of time

Setting unoptimised images in the next.config.js seems to workaround this for now

{
  images: { unoptimized: true },
}

Note: we are not using sharp

@MariuzM
Copy link

MariuzM commented Sep 26, 2023

Thanks @csi-lk images: { unoptimized: true }, this is really good, i also noticed that images now loading faster with this.

@mengqing
Copy link

Any ideas on how to fix this issue other than setting unoptimized? We would still like to optimize the image but this memory leak is forcing us no choice but to turn optimization off

@MuhammadMinhaj

This comment has been minimized.

@rasfa98
Copy link

rasfa98 commented Nov 14, 2023

Do we have an update on this? As @mengqing mentioned it would be great to have the possibility to still optimize the images 😃

@khuezy
Copy link
Contributor

khuezy commented Nov 19, 2023

It's probably better to offload the compute to its own service rather than forcing the main app server to handle it... which will block the main server and significantly reduce your throughput/latency.
See: https://nextjs.org/docs/app/api-reference/next-config-js/images
You can set up a distributed/scalable service on fly.io... fairly simple to implement it in rust or some other speedy language.

@houxd1992
Copy link

Can confirm we are seeing this issue in production on ^13.4.19, if we start curl'ing an image it will cause it to 500 after a short period of time

Setting unoptimised images in the next.config.js seems to workaround this for now

{
  images: { unoptimized: true },
}

Note: we are not using sharp

image

Do you guys think the cause of the memory leak is sharp or image optimised?
I'm confused.

@csi-lk
Copy link
Contributor

csi-lk commented Jan 29, 2024

Hmmm, as I mentioned above "Note: we are not using sharp" so I doubt it's related to sharp

I also haven't looked back into this for a while as we are just "pre-optimizing" our images manually (using img-optim then shipping those)

@cwatson272
Copy link

cwatson272 commented Mar 7, 2024

We have the same issue. We have thousands of images and very quickly our server gets crushed if we have optimization enabled across the site, even with very little traffic routed that server (lets say 100 page views/hour). We've since kept it to our ~30 or so static images and have offloaded everything else to Cloudinary, but it would be nice to use this function in more places.

Edit: turns out this is likely because we are on Heroku and it's filesystem is ephemeral. I think this looks for the file in the filesystem, can't find it, then generates a new image, but doesn't purge from memory until it can be found in or saved to the filesystem or something.

@khantseithu
Copy link

Are you guys seriously not gonna fix that? It's been a long time now, and I'm getting frustrated overtime, by having poor developer experience.

@webdevcody
Copy link

I think I'm also seeing this issue. I'm deploying to a linux VM and I find myself often restarting my machines every few days. My apps almost no users, but still the memory slowly climbs from 300mb up to 700mb over days. I'm assuming it's related to images but I haven't verified. I'm using Sharp as well if that helps debug anything.

@Innei
Copy link

Innei commented May 9, 2024

I think I'm also seeing this issue. I'm deploying to a linux VM and I find myself often restarting my machines every few days. My apps almost no users, but still the memory slowly climbs from 300mb up to 700mb over days. I'm assuming it's related to images but I haven't verified. I'm using Sharp as well if that helps debug anything.

Are you using @vercel/og or next/og?

@thilak-rao
Copy link

thilak-rao commented May 10, 2024

Are you guys seriously not gonna fix that? It's been a long time now, and I'm getting frustrated overtime, by having poor developer experience.

Memory leaks are great if you’re an infrastructure company. IIRC, some of Amazon’s libs also used to be memory leak ridden.

Screenshot 2024-05-10 at 9 16 18 AM

@akhrarovsaid
Copy link

akhrarovsaid commented May 18, 2024

I'm also experiencing this issue running Nextjs in a DO Droplet with sharp. Same thing where the app consumes significantly more memory as images are fetched. Irony is that it's a demo app that gets maybe 1 or 2 users every now and then, and has like 30 images in total. Next is consuming 300mb of ram almost immediately after serving the app and slowly gains more.

  • Next v13.5.2

@Module32
Copy link

My vercel deployment's taking up 6gb ram on my computer when I open a production build 😵‍💫😵‍💫 we need an update on this Nextjs team

@patriksimms
Copy link

For me this issue is also present in development. Running a simple 2 pages app with next 14.2.5 and having a big hero taser (9.4mb).

When setting images.unoptimized to true, memory maxes out at 900mb (already quite a lot but okay). When removing this option and refreshing the page with the image in the browser a few times it spikes at 3.5-4GB. Also removing the image from the page decreses memory significantly so I am sure this is the reason for my high consumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Image (next/image) Related to Next.js Image Optimization.
Projects
None yet
Development

No branches or pull requests