FG
๐ŸŒ Web & Full-StackVercel

Next 9.5.1 out of memory after some hot reloads

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Bug report Describe the bug Since updating to 9.5.x (from 9.4.x), i get an out of memory error after 10 something hot reloads: `FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory` it did rarely happen in 9.4.1, but it happens very consistantly in 9.5.x To Reproduce thats probably tricky. it happens on big projects and might be related to some bug in the hot reload / rebuild. Maybe it happens when there are some import circles? Expected behavior nextjs should not go out-of-memory System information - OS:macOS - Browser: chrome - Version of Next.js: 9.5.1 - Version of Node.js: 12.13.1 Additional information we are using a custom server with typescript

Error Output

error after 10 something hot reloads:

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Increase Node.js Memory Limit for Hot Reloads

Medium Risk

The out of memory error occurs due to the default memory limit set for Node.js processes being insufficient for large projects during hot reloads. Each hot reload may accumulate memory usage, especially with circular dependencies, leading to exhaustion of the JavaScript heap.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Increase Node.js Memory Limit

    Modify the start script of your Next.js application to increase the memory limit for Node.js. This can help mitigate the out of memory errors during hot reloads.

    bash
    node --max-old-space-size=4096 node_modules/.bin/next
  2. 2

    Identify Circular Dependencies

    Use a tool like Madge to identify circular dependencies in your project. Circular dependencies can lead to increased memory usage during hot reloads.

    bash
    npx madge --circular src/
  3. 3

    Refactor Code to Remove Circular Dependencies

    Refactor your code to eliminate any identified circular dependencies. This can help reduce memory consumption during hot reloads.

    typescript
    // Example refactor
    // Before: A imports B, B imports A
    // After: Create a new module C that both A and B can import instead.
  4. 4

    Monitor Memory Usage

    Use Node.js built-in tools or third-party monitoring tools to track memory usage during development. This will help you identify if the changes have improved memory management.

    bash
    node --inspect node_modules/.bin/next

Validation

Confirm the fix by running your Next.js application and performing multiple hot reloads. Monitor the memory usage and ensure that the out of memory error no longer occurs. If the application runs smoothly for at least 20 hot reloads without errors, the fix is successful.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

nextjsreactssrplease-add-a-complete-reproduction