Next.js Discord

Discord Forum

NextJS runs out of memory when building for production

Unanswered
Odorous house ant posted this in #help-forum
Open in Discord
Odorous house antOP
Hello, so I've been developing a NextJS app for about a month now and I am no longer able to build it, every time I try to do so I end up with an out of memory error from Node, I've even told Node to use a max old space size of up to 32GB and it still fails. As well, when watching the memory usage in top or htop on Linux, I've seen the virtual memory usage go as high as 85GB on a default app scaffolded from create-next-app, in both next@13 and next@14. Something is seriously wrong here, I'm happy to share code I've got for my project in case there's somehow a build-level memory leak on my end (which given I don't have anything special, or really anything at all going on in my config files I doubt it. It also wouldn't explain the absurd virtual memory usage on a default app).

Any help would be greatly appreciated. 🙂

22 Replies

Could you share more information about this project? Number of packages, static pages that you have to build or even code?
Odorous house antOP
Just made it public here https://github.com/teamcbba/teamcbba
Right now I'm going commit by commit and trying builds to sort out what changed, I think it might be something to do with updating to Next 14, I went back about 15 days to 13.5.6 and I just got my first successful build
Static pages there's a bunch that are just PLACEHOLDER: x, then only the login, register, and main pages actually have content
Otherwise it's a bunch of identical or almost identical dashboard routes I've been slowly adding
Data table, edit, and new routes, then sometimes a view route
@Odorous house ant Static pages there's a bunch that are just `PLACEHOLDER: x`, then only the login, register, and main pages actually have content
Odorous house antOP
Unless Next is doing something funky, there's no dynamically generated static routes with path params or anything
I see, I will try to test it tomorrow (CEST timezone)
Odorous house antOP
👍
Odorous house antOP
It doesn't seem to be entirely consistent either, as I'm going back I've come across a few points where it's built on one try then not on another
Right now I'm running with max heap space at 8GB, any higher than that is both unreasonable and means I can't run it in CI which means no more Next JS for me if I can't do that
Cape lion
I'm experiencing similar problems. Was previously using Next 13.5 and would never have memory issues. Switched to 14.0.4 and started seeing
Server is approaching the used memory threshold, restarting..
I created a new app, and just put in my base npm packages (shadcn ui, tanstack-query, clerk) and a few pages, and still seeing this problem. One thing I noticed, in MacOS the process name is
next-server
. Upon starting the server and loading a single page, memory balloons from 175MB to 1.34GB. Upon navigating for just a few minutes, it balloons to 1.83MB. With continued use, it goes to over 5GB.
Odorous house antOP
Yeah, that’s been happening to me for ages. They’ve got a gigantic issue on GitHub about it.
Problem is now I can’t build for production
Odorous house antOP
Okay I've managed to get it to build again, but it's somehow using about 10GB of RAM
Which is just completely obscene, I can build a significantly larger project in Rust for example with only 1GB
Odorous house antOP
There's absolutely a memory leak or absurd inefficiencies somewhere in the chain, watching it run it'll fluctuate from 4GB all the way up to 10GB at various points in time
Actually it just hit as high as 12.6GB
And then just dropped down to 6GB
Odorous house antOP
I've gotten it working on CI now, but I'm now paying for extra RAM which I'm not happy about
Masai Lion
Had issues like this before aka high memory usage specially when using Dev server or building. It was always coz of icons or next Js image optimisation. Try using pure svgs and disable the image optimisation. Hope it helps
Odorous house antOP
I don't have any images, and as for icons I'm just using tabler-icons/react, it should be tree-shakeable, I'm only importing the subset I use, and I've got the lib in other projects and it's not causing gigantic issues, so unless it's loading something like 50-100MB for every page for icons then I should be okay I think