-
Notifications
You must be signed in to change notification settings - Fork 27.1k
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
JavaScript heap out of memory during next build (Next 15.1) #73793
Comments
Hi, could you move this to a Stackblitz instead of codesandbox? Just type |
On stackblitz, the build doesn't crash but never completes and stay indefinitely on the |
I tried to use workers to get the build going, but... uncaughtException [Error: Worker terminated due to reaching memory limit: JS heap out of memory] {
code: 'ERR_WORKER_OUT_OF_MEMORY'
} Otherwise, you get the: <--- Last few GCs --->
[26998:0x138040000] 21645 ms: Scavenge 4051.8 (4120.5) -> 4049.6 (4123.7) MB, 6.1 / 0.0 ms (average mu = 0.452, current mu = 0.157) allocation failure;
[26998:0x138040000] 21653 ms: Scavenge 4055.0 (4123.7) -> 4052.4 (4142.0) MB, 6.5 / 0.0 ms (average mu = 0.452, current mu = 0.157) allocation failure;
[26998:0x138040000] 23958 ms: Mark-sweep 4065.0 (4142.0) -> 4056.9 (4150.2) MB, 2298.2 / 0.0 ms (average mu = 0.253, current mu = 0.026) allocation failure; scavenge might not succeed
<--- JS stacktrace ---> And so on... so I guess, we have to debug the build script? 🤔 |
Alright, I've done a binary search/bisect, which ever, and found that this has been introduced at Now it is time to check the changes introduced on that canary. |
Now I've been able to make a build work, by locally checking out this commit, and building Next.js up to that point: Which happens right before, this was merged #73358 Checking out, 92b3dd2, where #73358 sits, triggers the error, so in all likelihood, the error comes from that ISR fix for empty |
🤔 next.js/packages/next/src/build/segment-config/app/app-segments.ts Lines 115 to 120 in 7061f94
It looks like the recursion does end... so nope |
Alright, so I found the issue I think, { slug: 'grault' }, { slug: 'garply' }, { slug: 'waldo' },
{ slug: 'fred' }, { slug: 'plugh' }, { slug: 'xyzzy' },
{ slug: 'thud' }, { slug: 'foo2' }, { slug: 'bar2' },
{ slug: 'baz2' },
... 17210268 more items Within, I noticed that your repository example, can handle up to 33 routes, 34 kills it... probably the Array at hand goes way over the Array max length and boom |
One more thing before signing off. I think these lines: next.js/packages/next/src/build/segment-config/app/app-segments.ts Lines 111 to 113 in 2de9722
Are introducing a lot of repeated routes, which ends up with the, next.js/packages/next/src/build/utils.ts Lines 1331 to 1334 in 2de9722
parentParams growing exponentially.
If I do this tweak: // If this is a page segment, we know we've reached a leaf node associated with the
// page we're collecting segments for. We can add the collected segments to our final result.
if (name === PAGE_SEGMENT_KEY) {
currentSegments.forEach((seg) => {
if (
segments.some(
(other) =>
other.name === seg.name &&
other.filePath === seg.filePath &&
other.param === seg.param
)
) {
return
}
segments.push(seg)
})
} Then things build fine. |
@icyJoseph Thanks a lot for your investigation so far. To give you an order of magnitude, I noticed the issue on an application where generateStaticParams is an array of 14 elements with 7 parallel routes (in a dashboard like page). |
@pchab yeah, I saw that it gets extremely bad very quickly with the number of parallel routes. This must be fixed soon! |
This refactors `collectAppPageSegments` to be more efficient especially when dealing with multiple parallel routes, where the number of combinations of segments can drastically increase how many segments are returned per route. Previously this function only considered the `children` parallel route. When it was updated to consider all possible parallel routes in #73358, this had the unintended side effect of increasing the amount of duplicate segments this function would have to process. We only need to return unique segments discovered from a particular loader tree (representing a page), as opposed to the same segment as many times as it might appear across all parallel routes. This PR uses a map to track unique segments with a composite key used to identify the discovered segments. Additionally, this refactors the function to be iterative rather than recursive. We keep track of a queue of segments to be processed (a tuple of `[loaderTree, segments]`), and as we discover new parallel route paths, we process the queue further. Fixes #73793 Closes #73871 (h/t to @icyJoseph for identifying the cause of the memory consumption)
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/distracted-forest-vhlczj?workspaceId=ws_LGaEwH7iNTCahs4YK1orjP
To Reproduce
next build
JavaScript heap out of memory
(usually during the 'Collecting page data' step)Current vs. Expected behavior
next build
process doesn't crash.Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next build (local), Vercel (Deployed)
Additional context
The crash doesn't happen if I reduce the size of the array returned by
generateStaticParams
or if I remove a couple parallel routes. This happens on all platform I tested: Vercel, locally and on codesandbox.io.This used to work correctly with
[email protected]
.The text was updated successfully, but these errors were encountered: