next.js: appDir + RSC: "The server is running out of memory, restarting to free up memory"
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.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000
Binaries:
Node: 19.7.0
npm: 9.5.0
Yarn: N/A
pnpm: 7.28.0
Relevant packages:
next: 13.2.4-canary.1
eslint-config-next: 13.2.3
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
No response
Link to the code that reproduces this issue
https://github.com/timfee/www/tree/meet2hire1
To Reproduce
Observe the video, where the node
process gradually exceeds 4GB after about 4 minutes of using the app. During this time, I refreshed/reloaded several times (appDir/RSC pages), as well as triggered hot reloading in the browser.
This has been happing since 13.1.x and continues.
I couldn’t find any obvious leaks in the sever-side code (you can see some CPU / memory heap dumps in the root directory of the repro repository), e.g.: https://github.com/timfee/www/blob/4a813e95eb866e0fee39795f38e461427bee9795/vscode-profile-2023-03-03-13-51-06.heapsnapshot
I’m happy to do additional repro steps/troubleshooting on my end!
Here’s me intentionally trying to overwork it:
https://user-images.githubusercontent.com/3246342/222865281-1b1be172-b777-4466-bd2b-062b1b46947b.mov
Describe the Bug
After 5 mins of using the app (interacting and hot reloading), the server consumes > 4 GB and I get the message:
The server is running out of memory, restarting to free up memory.
Expected Behavior
This either wouldn’t happen, or Next would inform me if there was something egregiously wrong.
Which browser are you using? (if relevant)
110.0.5481.177 (Official Build) (arm64)
How are you deploying your application? (if relevant)
next dev
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 99
- Comments: 97 (9 by maintainers)
I am having the same problem with v13.4.4
For me it was MUI with Next 13’s app directory. I got it improved drastically by importing named exports directly from
@mui/material
and@mui/icons-material
.But the truth is that it was still slow. Just better.
Hope it helps someone.
Update
I found the solution for MUI/Next 13 slowness. Update your
next.config.js
with amodularizedImports
key like this:I saw compilation speed that was ranging from 5 to 10 seconds before to now down to about 200-300 milliseconds. I’m happy now. And of course update your MUI imports in your code as well.
this is really driving me nuts, how can we profile or find the root cause?
I hope this is a dev bug and not a production issue.
While building out an app, every 30m or so I get:
But it never properly restarts, so I have to manually do it.
I was able to mitigate the issue on my side by changing my icon lib(react icons) import to import only the specific files.
from:
to:
This reduced the issue substantially. I do still notice my ram usage grow but not enough to cause a restart or slow down the compiles.
I wonder if it’s related to Styled Components. There used to be a memory issue with Global Styles. Any thumbs up from people using Styled Components?
@mujud, thanks for the suggestion, I do not use
react-icons
, but you gave me a hint in the right direction.Next.js v13.2.4 compiler is really slow, looping and running out of memory in development. This was caused by the MUI icon import. I changed the import as below.
After changing this 20x in the sources the development environment is running fine. I do no longer notice the compiler.
Same issue here, since the newest version (13.4.19) instead of restarting, the dev server just crashes Other than the the emory issue still persists, no notable improvement
~I use a custom server~ (see below) and recently switched to the app router and started seeing this. Unfortunately, the HMR socket (that actually started working again for custom servers using the app router in 13.4.19) stays open and can’t recover.
Edit: The HMR socket does not recover even when no custom server is used.
Same issue with:
"next": "^13.4.2"
,"react": "^18.2.0"
,node v18.16.0
with 32GB of RAM on Ubuntu 20.04.6 LTS. Not using react-icons or MUI.We’ve been having the same problem. It would run out of memory after 3-4 page transitions.
@mui/icon-material/*
@tabler/icons-react/*
next@13.4.19
Initially I thought removing
tRPC
would solve the problem, but it just frees up enough memory to last longer, however it doesn’t solve the memleak. A few more page transitions and it OOMs and crashes.same issue here in 13.4.18
on server start, the memory usage creeps up towards 3-4GB+.
also as mentioned from comments, server ends suddenly in 500 error at times
When I cleaned up my code, I started facing the issue after changing module export from default to named export. Upgrade next from
13.4.7
to13.4.19
, the problem persists though I don’t see it happening on every development run. I’m unsure if the icons are the issue. But I do have both lucide icons and react-icons installed in the project.node: v18.17.0 npm: 9.6.7
i am using the latest version of nextjs (13.4.10) and still getting “warn The server is running out of memory, restarting to free up memory” also with this warning “getServerSession` is used in a React Server Component” and the compile is compiling too slowly
I think you can focus on other things more important than deciding what is false and negative with this tone of arrogance and entitlement. No one really needs or is interested in your favor!!
And BTW did you hear about “Turn of Notifications” Thank me later
Everyone is trying to help with code, screenshots, and experience.
If it wasn’t for people’s solutions here I wasn’t gonna fix it.
So calm down your arrogance and entitlement. And find other things useful to do.
Getting the same issue on app directory in
Nextjs v13.4.8
production mode.For everyone struggling: I have moved to
@tabler/react-icons
with import modularization. This fixed the issue for me.I added a comment to the app directory feedback thread to hopefully get more visibility on this: https://github.com/vercel/next.js/discussions/41745?sort=new#discussioncomment-5470560
I’m not sure what initiates it, but sometimes a page will just keep making requests to the server as if I’m editing files related to that page. The response seems to always be empty. When this starts happening, the memory usage of the node process just keeps increasing until it eventually crashes with the out of memory message.
After navigating to a page and waiting a few seconds, Next made over 200 requests in a row and the memory usage increased by about 1.5Gb without me editing any files or interacting with the page. Here’s what the network tab looks like:
same issue…
Same here (Next 13.2.4). Do you have suggestions how I could investigate the problem and/or get more log information? Would it be possible to see which code is being recompiled?
I’d be in favour of moving the chatter to a discussion, there’s no point playing guess who and y’all are simply pointing out false positives (Yes it’s a false positive if you’re just turning off features that use CPU/RAM without doing a stack trace, ofc they will reduce your usage. Instructions for reporting things other than that: https://github.com/vercel/next.js/issues/48748).
Most people are just waiting on this to be closed vs people’s theories as to what it is.
I’m using 13.4.12. this is what I’m seeing in development: next-router-worker (one per app started) next-render-worker-app (one per appDir) next-render-worker-pages (one per appPage)
the router-worker seems to grow quickly 2.6Gb after a couple of clicks. This router-worker is connected to the render-worker-app. the render-worker-app is a bit more stable, but grows to 1.6Gb.
Over development time both of these processes are only consuming more memory. Navigating to the same pages seems only increase memory.
@timneutkens : Could it be that also data is cached? is there a way to disabled that completely? We are using apollo, but caching is set to network-only, so disabled. Please let me know if you need more details.
@jamilAbbas for the slow compiling you’ll definitely want to upgrade to at least 13.4.8+ as we did a bunch of optimizations. Can you provide the CPU profile and trace on #48748 following these steps: https://github.com/vercel/next.js/issues/48748#issuecomment-1578374105
I think this issue just happening on the app directory in dev mode. Because on production the issue doesn’t persist. And interesting this is if I use the pages directory the issue is also gone.
Bumping, having the same issue, this is unusable please do something. Fast refresh takes roughly 10seconds every single change i have to wait for the page to reload…
There are two issues here:
Same same, but no MUI or
react-icons
.For me they are the same problems, except that I am using MUI and the routes are extremely slow, but this only happens in dev mode with the hot reload, running in production mode the site is performatico, my computer is not bad to be so slow to compile and perform the hotreload besides it has a lot of ram memory and it only increases until it exceeds the ram memory limit, that is, maybe MUI is causing memory leaks, or even the routes in dev mode
I’m not seeing 100s of requests when using
v13.2.5-canary.20
, but the server is still running out of memory after a few minutes of use.The same issue today with “next”: “13.4.12”, Since I started the project a lot of Lag, Freeze, and crashes in my Chrome and the website itself I suspected everything until I closed the dev server, and everything was back to normal.
High memory usage with every simple website it’s all static no data fetching.
the higher usage starts as soon as I navigate to any route.
Are you experiencing the instabilities with Next 13 + appDir? In cases where I’ve used Next 13 without experimental features, I haven’t noticed the same stability issues.
Just had it crash for me as well.
v13.2.5-canary.20
No runaway requests, but still OOM issues.Same here as @danmartens noted above.
Running on
v13.2.5-canary.20
, the memory issue seems somewhat relieved as dev is overall snappier and takes longer to run out of memory and restart. No absurd bursts of requests seen so far, but it still is consistently running out of memory and restarting every few minutes, and pages are still compiling much slower than similar pages in the Nextv <= 12
apps I’ve worked on.This has only really been a noticeable issue a few weeks into the project, so it seems related to either
13.1
/13.2
or a scaling issue with the size of a project and certain files withinSure, it seems like the issue has been resolved in the latest update v13.2.5-canary.20. Thank you very much, Next.js. Certainly, I will notify you again if there are any new issues. Update OOM still exist
It seems that they’ve just fixed it in v13.2.5-canary.20 few hours ago. You can update it with
npm install next@canary
FWIW, my original issue didn’t use mui so might be something else too
same issue 😦
If this helps:
I tried:
It seems particularly bad in active development, e.g. lots of HMRs vs when I’m just using the app.