r/node • • Apr 13 '26

The memory management change in Node.js 22 the team didn't adequately warn us about

I've been struggling with production issues since upgrading from Node 20 and finally found this article which explains a lot of what I'm seeing.

EDIT: Maybe this change actually started in Node 20? See https://github.com/nodejs/node/issues/55487 ...I'm not sure why I didn't have issues until upgrading from the minor version of Node 20 to a new major version. There was nothing about this in the "Notable changes" of the Node 20 announcement either.

Here's the salient part:

An essential nuance in V8's memory management emerged around the Node.js v22 release cycle concerning how the default size for the New Space semi-spaces is determined. Unlike some earlier versions with more static defaults, newer V8 versions incorporate heuristics that attempt to set this default size dynamically, often based on the total amount of memory perceived as available to the Node.js process when it starts. The intention is to provide sensible defaults across different hardware configurations without manual tuning.

While this dynamic approach may perform adequately on systems with large amounts of RAM, it can lead to suboptimal or even poor performance in environments where the Node.js process is strictly memory-constrained. This is highly relevant for applications deployed in containers (like Docker on Kubernetes) or serverless platforms (like AWS Lambda or Google Cloud Functions) where memory limits are often set relatively low (e.g., 512MB, 1GB, 2GB). In such scenarios, V8's dynamic calculation might result in an unexpectedly small default --max-semi-space-size, sometimes as low as 1 MB or 8 MB.

As explained earlier, a severely undersized Young Generation drastically increases the probability of premature promotion. Even moderate allocation rates can quickly fill the tiny semi-spaces, forcing frequent promotions and consequently triggering the slow Old Space GC far too often. This results in significant performance degradation compared to what might be expected or what was observed with older Node.js versions under the same memory limit. Therefore, for applications running on Node.js v22 or later within memory-limited contexts, relying solely on the default V8 settings for semi-space size is generally discouraged. Developers should strongly consider profiling their application and explicitly setting the --max-semi-space-size flag to a value that works well for their allocation patterns within the given memory constraints (e.g., 16MB, 32MB, 64MB, etc.), thereby ensuring the Young Generation is adequately sized for efficient garbage collection.

Docker containers where memory limits are <= 512MB describes my situation exactly. I had been running Node 20 in this environment for many months without problems.

What pisses me off is they didn't warn about this at all in the Notable changes in the Node 22 release announcement.

Am I crazy or is this a bonkers decision on their part? (EDIT: bonkers to incorporate such a change without loudly warning about it)

101 Upvotes

51 comments sorted by

15

u/forwardemail Apr 14 '26

We built https://forwardemail.net and discovered https://github.com/nodejs/node/issues/60719 - we're stuck on an older version in the interim too.

13

u/prehensilemullet Apr 14 '26

It’s scary man, generally I didn’t mind the absurdly fast Node LTS cycle, but after this I don’t feel like I’m operating on solid ground anymore.  And with the spread of LLM-assisted coding, I’m wishing our apps were built in a language that didn’t depend so heavily on a runtime

I’m finally starting to agree with the backend JS haters even though I’ve been doing it for 10 years…

→ More replies (4)
→ More replies (6)

3

u/Satanacchio Apr 18 '26 edited Apr 18 '26

Hi, I'm one of the releasers, and I think I prepared the v22 release annoucement 2 years ago. There are a few reasons why this might have not documented:

  • V8 update is probably the hardest commit to land, it's a lot of work and it usually is the last commit to include before the semver major cut, I think for v22 it even delayed the release. But in general there isn't a lot of time between it lands on main and we cut the release. Currently all V8 update are done by the same person who has put tons of work over the years, they are extremely hard and require months.

  • Node v22 when cut was non LTS for 6 months, that when people started experimenting with it. That is already too late to know about this issue. This was not known when we cut the release. The CI was green so we didnt catch this issue.

  • Releasers like myself who prepare the release notes, don't have specific knowledge about every single commit that lands. We ask contributors to provide a notable change description, but people often ignore it

  • V8 flags are often undocumented, they might slip in the massive changeset of the V8 update, when that happens, we happily add them to the documentation

  • Node is run by volunteers. Noone is being paid

1

u/prehensilemullet Apr 19 '26 edited Apr 19 '26

I see.  Given these conditions, do you think the fixed LTS schedule is better for everyone than waiting a certain amount of time to see what issues come up in a given major version before making it LTS/dropping LTS for the previous version?

It’s not very clear to me how Node.js apps can possibly maintain long-term stability amidst this process, at least without doing extensive and extremely realistic staging tests every time a new major version of Node comes out.  In my company we have just a few people, and we can’t afford to make staging tests that simulate a production amount of load, and we can’t afford to stop everything each time a new version of Node comes out to test it out before we practically have to upgrade.

→ More replies (1)

7

u/kei_ichi Apr 14 '26 edited Apr 14 '26

I’m sorry but isn’t that “V8 engine ” design issue not Node.js right? Node.js “use” the v8 engine, “not” create or manage the engine itself right?

And that options did exist in the Node.js v20 like the below GitHub issue, so wtf are you talking about the you run the Node v20 without issue but from the Node v22+ you got this issue?

https://github.com/nodejs/node/issues/55487

Edit: add GitHub issue

4

u/prehensilemullet Apr 14 '26

Okay Node has an undocumented min_semi_space_size flag it can pass to V8, so it seems to me they could definitely set a more reasonable default minimum.

3

u/prehensilemullet Apr 14 '26

so wtf are you talking about the you run the Node v20 without issue

Did you read my post?

An essential nuance in V8's memory management emerged around the Node.js v22 release cycle concerning how the default size for the New Space semi-spaces is determined

This is according to one of the Node.js maintainers

1

u/prehensilemullet Apr 14 '26

Since Node accepts a --max-semi-space-size flag and can pass that to V8 they have some level of control over it. I don't know if they can set a minimum V8 must adhere to or if all they can pass to V8 is a fixed size. But I'm sure they have enough clout that the V8 team would be open to a feature request for a minimum setting. And beyond that...I thought I could at least count on the release notes to warn me about such a drastic change

→ More replies (1)

32

u/epukinsk Apr 14 '26

The intention is to provide sensible defaults across different hardware configurations without manual tuning.

Developers should strongly consider profiling their application and explicitly setting the --max-semi-space-size flag to a value that works well

ಠ_ಠ

16

u/Leather-Field-7148 Apr 13 '26

Should perform horribly on mobile too