From 0bbe7c36c9b637a070ac8c0dfdf5e6130ffff756 Mon Sep 17 00:00:00 2001 From: Asaf Federman <46937909+Asaf-Federman@users.noreply.github.com> Date: Mon, 28 Jul 2025 08:06:16 +0300 Subject: [PATCH] src: add percentage support to --max-old-space-size This commit adds support for specifying --max-old-space-size as a percentage of system memory, in addition to the existing MB format. A new HandleMaxOldSpaceSizePercentage method parses percentage values, validates that they are within the 0-100% range, and provides clear error messages for invalid input. The heap size is now calculated based on available system memory when a percentage is used. Test coverage has been added for both valid and invalid cases. Documentation and the JSON schema for CLI options have been updated with examples for both formats. Refs: https://github.com/nodejs/node/issues/57447 PR-URL: https://github.com/nodejs/node/pull/59082 Reviewed-By: Chengzhong Wu Reviewed-By: James M Snell Reviewed-By: Moshe Atlow Reviewed-By: theanarkh Reviewed-By: Daeyeon Jeong --- doc/api/cli.md | 17 +++ doc/node-config-schema.json | 3 + doc/node.1 | 10 ++ src/node.cc | 7 + src/node_options.cc | 48 +++++++ src/node_options.h | 4 + .../test-max-old-space-size-percentage.js | 134 ++++++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 test/parallel/test-max-old-space-size-percentage.js diff --git a/doc/api/cli.md b/doc/api/cli.md index ca6e0e684e..1dbdf50462 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1719,6 +1719,22 @@ changes: Specify the maximum size, in bytes, of HTTP headers. Defaults to 16 KiB. +### `--max-old-space-size-percentage=PERCENTAGE` + +Sets the max memory size of V8's old memory section as a percentage of available system memory. +This flag takes precedence over `--max-old-space-size` when both are specified. + +The `PERCENTAGE` parameter must be a number greater than 0 and up to 100. representing the percentage +of available system memory to allocate to the V8 heap. + +```bash +# Using 50% of available system memory +node --max-old-space-size-percentage=50 index.js + +# Using 75% of available system memory +node --max-old-space-size-percentage=75 index.js +``` + ### `--napi-modules`