vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.52k stars 27.04k forks source link

Bug: extremely high memory usage with `next dev` #42514

Closed budchirp closed 1 year ago

budchirp commented 2 years ago

Verify canary release

Provide environment information

Operating System:
  Platform: android
  Arch: arm64
  Version: #2 SMP PREEMPT Fri Aug 5 15:52:33 AST 2022
Binaries:
  Node: 18.10.0
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 13.0.3-canary.0
  eslint-config-next: 13.0.2
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome Canary v109.0.5400.0 (Android)

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

The memory usage on the last next 13 (13.0.3-canary.0 and 13.0.2) releases are too high. next dev command uses about 1gb of ram. Next 12 is using about 300mb-500mb's of ram.

Screenshot_20221104-225938_Termux

Expected Behavior

next dev should use less ram.

Link to reproduction

.

To Reproduce

  1. Create project with Next 13
  2. Run next dev
  3. Check the ram usage
Nefcanto commented 1 year ago

How about the production environment?

feedthejim commented 1 year ago

Hi @Nefcanto, can you open a new issue with a repro of your setup/what operations you noticed increased memory usage?

JustinPraas commented 1 year ago

Watching this carefully, as I have a Next project with about 12 pages and 260 static pages. 547MB free mem, and it dies rather quickly

adshodgson commented 1 year ago

The Production build seems to run fine for me, really quick.

The fixes @feedthejim suggested, regarding changing the Memory Limit, and also the patches introduced have stopped the server from crashing in Development mode.

I think the only serious problem for me, seems to be how slow the App is in Development mode - when trying to navigate between pages, I can sometimes wait for up to 8-10 seconds for each page to switch - Whilst in production it's almost instantaneous.

I'm unsure the cause, just hinders productivity quite dramatically.

Alexandredc commented 1 year ago

The Production build seems to run fine for me, really quick.

The fixes @feedthejim suggested, regarding changing the Memory Limit, and also the patches introduced have stopped the server from crashing in Development mode.

I think the only serious problem for me, seems to be how slow the App is in Development mode - when trying to navigate between pages, I can sometimes wait for up to 8-10 seconds for each page to switch - Whilst in production it's almost instantaneous.

I'm unsure the cause, just hinders productivity quite dramatically.

Do you fetch data in layout ? I just found out that fetching in layout reduce a lot development mode.

feedthejim commented 1 year ago

@adshodgson do you mind sharing a repro (preferably via codesanbox or similar tool)? I can take a look.

There's a few things that can slow down dev times:

async function Layout() { const somethingThatDoesntNeedToBlockRendering = await something();

return <> {somethingThatDoesntNeedToBlockRendering && {children} </> }

// better alternative that leverages streaming

function SomethingThatDoesntNeedToBlockRendering() { return somethingThatDoesntNeedToBlockRendering && }

function Layout() { return <>

{children} } ```
Nefcanto commented 1 year ago

@feedthejim I sent you an invitation here. This is one of our real-world examples. We are a website design company. We use Next.js as our website tech and other technologies for our backend and API. We're still using Next.js 12. We used ASP.NET Core Razor Pages before and we had much more density per server. We could host 10 websites per 1GB of RAM. Now we're limited to 1 or 2 at most. This has increased our hosting costs drastically and we're thinking of migrating to another tech, like Qwik maybe.

But if we can get lower RAM in production, then we're good to continue using Next.js.

@feedthejim, I would gladly send you remote access to my PC and I would continue the debugging session with you if you want to. I would guide you through our architecture (though it's not hard at all), and explain how we use Next.js. Just let me know. Thank you so much.

tomscript commented 1 year ago

I'm also encountering this and can share a repo.

"next": "^13.0.8-canary.0",

One thing that i find really strange is how slow my app takes to compile when switching pages during development mode,

  • this is exactly what i notice as well
hamza512b commented 1 year ago

Hi all! I do not know if this helpful, but this happens also when testing with next/jest when you I run jest --watch.

quiet-node commented 1 year ago

Any official fix yet? still happening for me and export NODE_OPTIONS=--max_old_space_size=8192 won't help at all. I'm running next 13.0.3-canary.0

feedthejim commented 1 year ago

@logann131 can you try upgrading and report back if it still happens?

mooijtech commented 1 year ago

Also facing this issue, memory climbs to ~5GB while it should be ~30MB. Trying to remove all code until it's fixed to find the cause.

feedthejim commented 1 year ago

@mooijtech on a dev or prod instance?

mooijtech commented 1 year ago

@feedthejim Long story short, turns out the memory leak is not related to NextJS but Tauri (https://tauri.app/) caused by the tauri-plugin-persisted-scope.

I will open an issue there.

Thanks for your reply.

dexter4life commented 1 year ago

I notice using dynamic to render component seems to provide some improvement mostly if the SSR is set to true, just in case anyone here want to experiment on that too.

dexter4life commented 1 year ago

image I am having this error, sometimes, can someone explain this?

feedthejim commented 1 year ago

@dexter4life can you perhaps open an issue with a repo?

I'll be closing this issue as the frequency of the reports seems to have lessened a bit. Please open an issue with repro steps if you encounter issues again!

github-actions[bot] commented 1 year ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.