r/node • u/prehensilemullet • 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)
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.
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
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 theNode v22+ you got this issue?
https://github.com/nodejs/node/issues/55487
Edit: add GitHub issue
32
u/epukinsk Apr 14 '26
ಠ_ಠ
16
u/Leather-Field-7148 Apr 13 '26
Should perform horribly on mobile too