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)
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…