vercel / next.js

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

Turbopack dev server and HMR consuming too much memory and freezing at code changes. #66326

Closed devhik0 closed 1 month ago

devhik0 commented 4 months ago

Link to the code that reproduces this issue

https://github.com/devhik0/crm-saas

To Reproduce

  1. Run next dev --turbo and check Memory values at task manager, htop etc. Also check VS Code's process explorer via F1 > Open Process Explorer and look for "<path>\nodejs\node.exe" <path-to-app>\node_modules\next\dist\server\lib\start-server.js

Current vs. Expected behavior

That process above is consuming around 1GB memory at idle state (just running and no code change), thus code change reflections taking around > 30s to appear on UI. Here is the latest hmr result:

○ Compiling /tasks ...
✓ Compiled in 39.9s (sometimes around 60)
✓ Compiled /tasks in 18.2s

Expected: faster rebuilds to see what happens at UI in short time and less memory consumption

I think there is a memory leak at somewhere in process.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home Single Language
  Available memory (MB): 16280
  Available CPU cores: 4
Binaries:
  Node: 20.10.0
  bun: 1
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5

Which area(s) are affected? (Select all that apply)

Developer Experience, Module Resolution, Performance, Runtime, Turbopack, TypeScript

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Here i am sharing process info from my system to show what is happening actually:

memory

The process with red is : <path>\nodejs\node.exe" <path-to-app>\node_modules\next\dist\server\lib\start-server.js

Also this is from process explorer at vscode:

process

devhik0 commented 3 months ago

Anything ?

timneutkens commented 3 months ago

Can you try next@canary first, currently it's on the latest stable version but Turbopack is in active development: https://github.com/devhik0/crm-saas/blob/master/client-app/package.json#L35

1GB of memory usage is not much in general, especially if you have large dependencies like heroicons that are quite large (thousands of files)

louisgv commented 3 months ago

Seeing a similar issue running on Mac. It's smoking ~5GB of CPU:

Screenshot 2024-06-18 at 2 08 15 PM

Updated from 14.1.4 -> 14.2.4

devhik0 commented 3 months ago

Well I tried even next 15@rc and without turbo, still my dev server takes around 1gb memory and takes near minutes to compile.

timneutkens commented 3 months ago

In order to help me investigate this I'll ideally need an application that can be run, if you can't provide that (I understand if you can't) please provide the .next/trace and .next/trace.log file.

Please follow these steps which would give me the best picture to investigate:

devhik0 commented 3 months ago

You know that problematic repo, i will provide env values to run it properly. Here is the project folder and env values, just put lines into a .env.local file inside folder root. project folder

# Supabase
NEXT_PUBLIC_SUPABASE_URL=https://jokkvunyzexyiwypinjd.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Impva2t2dW55emV4eWl3eXBpbmpkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTYzODIyNzEsImV4cCI6MjAzMTk1ODI3MX0.O-_1nkM2Blhm7kjscCL9bC0BIp_yhLeKGVoFKDdo_bw
  1. Clone repo, cd client-app , bun i (to use another package manager, you need to remove bun.lockb before , bun run dev (w/o --turbo flag), click tabs on sidebar to check page loads, try to make changes at related files like app/calendar/page.tsx to see HMR response times. I am seeing it like this:
    ○ Compiling /tasks ...
    ✓ Compiled in 39.9s (sometimes around 60)
    ✓ Compiled /tasks in 18.2s

    Let me know if something goes wrong, thanks for your interest.

timneutkens commented 3 months ago

Can you try to run the steps to grab a trace as well so that we have numbers for your machine? Thanks!

devhik0 commented 3 months ago

Here is files link @timneutkens

devhik0 commented 3 months ago

Anything?

timneutkens commented 2 months ago

Hey @devhik0 sorry I couldn't get around to looking into this yet, could you create a new share link as the old one expired. Thanks!

devhik0 commented 2 months ago

Hey @timneutkens here is files, please download before expire :) files It didnt write trace.log file

Also if helps i have another related issue at typescript repo and we found that typescript has some issues with Generics so i was using generics at my ui library (shadcn) when i remove generics it was faster but i dont know its related. here is the link for issue typescript

devhik0 commented 2 months ago

Anything?

timneutkens commented 2 months ago

@devhik0 seems you're not using Turbopack 🤔 The trace indicates it's using webpack, is that expected?

devhik0 commented 2 months ago

Yes it's webpack, turbopack gives similar results like freezing. We can go with webpack @timneutkens

timneutkens commented 2 months ago

The Turbopack trace has much more context on memory usage, so I'd like to receive that if possible, especially in the case where you're saying it freezes 👍

devhik0 commented 2 months ago

@timneutkens Actually i only want a solution for current situation cuz i won't use turbopack for a long while.

timneutkens commented 2 months ago

Regardless please share the trace file as that allows me to investigate further.

devhik0 commented 2 months ago

I shared files already you can find inside of it @timneutkens

timneutkens commented 1 month ago

It's not the Turbopack trace, which is what I requested above. You only shared a webpack trace which we can't use to investigate memory usage.

Overall 1GB memory usage with the amount of code and modules you're compiling with webpack does not seem off / high.

devhik0 commented 1 month ago

I am not using turbopack at all. But i understand what is problem though, had to upgrade my hardware and problem solved for 2000$ 😂 . Thanks for your all effort

timneutkens commented 1 month ago

Okay I guess I'll close the issue then 👍 In the initial issue you said you were using --turbo btw 😄

github-actions[bot] commented 2 weeks ago

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