next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Investigate memory usage with Image Optimization enabled

Open timneutkens opened this issue 2 years ago • 10 comments

Verify canary release

  • [X] 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 avatar Aug 24 '23 08:08 timneutkens

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!

AlphaBravoIndia avatar Aug 31 '23 06:08 AlphaBravoIndia

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).

miketdonahue avatar Sep 14 '23 21:09 miketdonahue

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

csi-lk avatar Sep 14 '23 22:09 csi-lk

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

MariuzM avatar Sep 26 '23 18:09 MariuzM

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

mengqing avatar Oct 11 '23 10:10 mengqing

Is there any update? Facing the same issue!

MuhammadMinhaj avatar Oct 27 '23 09:10 MuhammadMinhaj

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

rasfa98 avatar Nov 14 '23 08:11 rasfa98

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.

khuezy avatar Nov 19 '23 01:11 khuezy

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.

houxd1992 avatar Jan 19 '24 09:01 houxd1992

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)

csi-lk avatar Jan 29 '24 23:01 csi-lk

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.

cwatson272 avatar Mar 07 '24 23:03 cwatson272

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.

khantseithu avatar May 02 '24 12:05 khantseithu

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.

webdevcody avatar May 08 '24 22:05 webdevcody

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?

Innei avatar May 09 '24 03:05 Innei

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

thilak-rao avatar May 10 '24 07:05 thilak-rao

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

akhrarovsaid avatar May 18 '24 00:05 akhrarovsaid

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

Module32 avatar Jul 22 '24 16:07 Module32

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.

patriksimms avatar Aug 04 '24 13:08 patriksimms