Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak in Edge Runtime #63796

Closed
floatingdino opened this issue Mar 28, 2024 · 4 comments
Closed

Memory Leak in Edge Runtime #63796

floatingdino opened this issue Mar 28, 2024 · 4 comments
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@floatingdino
Copy link

Link to the code that reproduces this issue

https://github.com/floatingdino/nextjs-edge-memory-bug

To Reproduce

  1. Run test command npm run test
  2. (Optionally) Turn off edge runtime on app/page.js and run test command again

Current vs. Expected behavior

When requesting pages in edge runtime, I expect the memory footprint to remain consistent or self-correct spikes as shown when using the Node runtime

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: N/A
  pnpm: 8.10.2
Relevant Packages:
  next: 14.1.4
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: N/A
Next.js Config:
  output: standalone

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

App Router, Middleware / Edge (API routes, runtime)

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

next dev (local), next start (local)

Additional context

Edge runtime memory usage:
Screenshot 2024-03-28 at 2 15 45 pm
Node runtime memory usage:
Screenshot 2024-03-28 at 2 15 51 pm

When deploying on Vercel, this seems to be fine (I assume each request is serverless and the memory footprint is completely cleansed once the connection is closed) however, when deploying with Docker, this inevitably causes OOM issues. This doesn't seem to be a Docker-specific issue as running with node locally also exhibits memory inflation.

@floatingdino floatingdino added the bug Issue was opened via the bug report template. label Mar 28, 2024
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Mar 28, 2024
@chane81
Copy link

chane81 commented Nov 11, 2024

@floatingdino
I'm not sure if this will help, but in my case, I was using Next.js version 14.2.2, Node.js 18 (in a Docker environment), and middleware (with edge runtime). While other services using Next.js 14 didn't experience memory leaks, the environment using middleware did show memory leak issues.
After conducting various tests, I resolved the memory leak issue by changing the Node version in Docker from 18 to 22.11.0 (LTS).

[before] - docker node.js version 18
CleanShot 2024-11-11 at 13 09 36@2x

[after] - docker node.js version 22.11.0 alpine
CleanShot 2024-11-11 at 13 11 13@2x

CleanShot 2024-11-11 at 13 12 56@2x

@Fonger
Copy link
Contributor

Fonger commented Nov 11, 2024

We had the same issue as @chane81 described.

Are you using node.js 20.16.0 or 20.17.0? If so, please try 20.18.0, or 20.15.1
There's some bugs in underlying undici fetch in these node.js versions.

nodejs/node#54274

@floatingdino
Copy link
Author

Thanks @chane81 @Fonger for your input, upgrading my environment from node v20.10 to v22.11 solved the memory leak in my environment.

Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants