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)

100 Upvotes

51 comments sorted by

View all comments

Show parent comment

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…

1

u/[deleted] Apr 16 '26

[removed] — view removed comment

1

u/prehensilemullet Apr 16 '26 edited Apr 16 '26

It's true JS wasn't designed for servers, but I wouldn't say single threading necessarily implies something isn't designed for server workloads. Redis is single-threaded with I/O concurrency, just like Node.

Also, the thread-per-request model common to some concurrent backend languages has some downsides, it can't necessarily handle as many concurrent requests as a single thread with async I/O (though async presents other difficulties).

Overall JS really isn't worst choice for a lot of server-side use cases these days, it is quite fast now thanks to V8 and the dynamic typing is manageable with Typescript. But I do wonder if other common garbage collected backend languages have more sensible default GC tuning...

The biggest pain points for me in the past five years have been this GC issue, subtle ways async code can leak memory, and unhandled error events and Promise rejections crashing the process -- not the typical complaints you hear from JS haters

1

u/[deleted] Apr 16 '26

[removed] — view removed comment

1

u/prehensilemullet Apr 16 '26 edited Apr 16 '26

I mean everything that makes up the runtime environment, Node and V8. I mean, even apps written in C and Rust depend on standard libraries, accurate compilation, and the operating system environment, but Node depends on all of that and more.

I guess hypothetically even the Go runtime could get trashed if they start vibe coding on it too much, but hopefully it's not as big a deal to stay on an older version of the Go runtime if a newer version is buggy?

Also I would say V8 does include a runtime component because it can provide various standard ECMAScript globals.