-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
src: add percentage support to --max-old-space-size #59082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 8 commits into
nodejs:main
from
Asaf-Federman:feat/max-old-space-size-percentage
Jul 28, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c837d14
src: add percentage support to --max-old-space-size
Asaf-Federman 90f8288
doc: remove unrelated changes
Asaf-Federman 84d3433
src: introduce max-old-space-size-percentage as a new cli flag
Asaf-Federman ebaa850
doc: remove doc related to modified implementation
Asaf-Federman c377756
doc: change max-old-space-size-percentage type to be number
Asaf-Federman a23ece3
doc: revert max-old-space-size-percentage type to be a string
Asaf-Federman b919043
doc: fix linting and doc issues
Asaf-Federman 8e5f5ea
src: handle UINT64_MAX (according to libuv doc) and remove redundant …
Asaf-Federman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
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: #57447
- Loading branch information
commit c837d144d2cd7bba032e3eddf31aa595dc85b797
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| 'use strict'; | ||
|
|
||
| // This test validates the --max-old-space-size=XX% CLI flag functionality. | ||
| // It tests valid and invalid percentage values, NODE_OPTIONS integration, | ||
| // backward compatibility with MB values, and percentage calculation accuracy. | ||
|
|
||
| require('../common'); | ||
| const assert = require('node:assert'); | ||
| const { spawnSync } = require('child_process'); | ||
|
|
||
| // Valid percentage cases | ||
| const validPercentages = [ | ||
| '1%', '10%', '25%', '50%', '75%', '99%', '100%', '25.5%', | ||
| ]; | ||
|
|
||
| // Invalid percentage cases | ||
| const invalidPercentages = [ | ||
| '%', '0%', '101%', '-1%', 'abc%', '100.1%', '0.0%', | ||
| ]; | ||
|
|
||
| // Helper for error message matching | ||
| function assertErrorMessage(stderr, context) { | ||
| assert( | ||
| /illegal value for flag --max-old-space-size=|--max-old-space-size percentage must be|--max-old-space-size percentage must not be empty/.test(stderr), | ||
| `Expected error message for ${context}, got: ${stderr}` | ||
| ); | ||
| } | ||
|
|
||
| // Test valid percentage cases | ||
| validPercentages.forEach((input) => { | ||
| const result = spawnSync(process.execPath, [ | ||
| `--max-old-space-size=${input}`, | ||
| '-e', 'console.log("OK")', | ||
| ], { stdio: ['pipe', 'pipe', 'pipe'] }); | ||
| assert.strictEqual(result.status, 0, `Expected exit code 0 for valid input ${input}`); | ||
| assert.match(result.stdout.toString(), /OK/, `Expected stdout to contain OK for valid input ${input}`); | ||
| assert.strictEqual(result.stderr.toString(), '', `Expected empty stderr for valid input ${input}`); | ||
| }); | ||
|
|
||
| // Test invalid percentage cases | ||
| invalidPercentages.forEach((input) => { | ||
| const result = spawnSync(process.execPath, [ | ||
| `--max-old-space-size=${input}`, | ||
| '-e', 'console.log("FAIL")', | ||
| ], { stdio: ['pipe', 'pipe', 'pipe'] }); | ||
| assert.notStrictEqual(result.status, 0, `Expected non-zero exit for invalid input ${input}`); | ||
| assertErrorMessage(result.stderr.toString(), input); | ||
| }); | ||
|
|
||
| // Test NODE_OPTIONS with valid percentages | ||
| validPercentages.forEach((input) => { | ||
| const result = spawnSync(process.execPath, [ | ||
| '-e', 'console.log("NODE_OPTIONS OK")', | ||
| ], { | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| env: { ...process.env, NODE_OPTIONS: `--max-old-space-size=${input}` } | ||
| }); | ||
| assert.strictEqual(result.status, 0, `NODE_OPTIONS: Expected exit code 0 for valid input ${input}`); | ||
| assert.strictEqual(result.stderr.toString(), '', `NODE_OPTIONS: Expected empty stderr for valid input ${input}`); | ||
| assert.match(result.stdout.toString(), /NODE_OPTIONS OK/, `NODE_OPTIONS: Expected stdout for valid input ${input}`); | ||
| }); | ||
|
|
||
| // Test NODE_OPTIONS with invalid percentages | ||
| invalidPercentages.forEach((input) => { | ||
| const result = spawnSync(process.execPath, [ | ||
| '-e', 'console.log("NODE_OPTIONS FAIL")', | ||
| ], { | ||
| stdio: ['pipe', 'pipe', 'pipe'], | ||
| env: { ...process.env, NODE_OPTIONS: `--max-old-space-size=${input}` } | ||
| }); | ||
| assert.notStrictEqual(result.status, 0, `NODE_OPTIONS: Expected non-zero exit for invalid input ${input}`); | ||
| assertErrorMessage(result.stderr.toString(), `NODE_OPTIONS ${input}`); | ||
| }); | ||
|
|
||
| // Test backward compatibility: MB values | ||
| const maxOldSpaceSizeMB = 600; // Example MB value | ||
| const mbResult = spawnSync(process.execPath, [ | ||
| `--max-old-space-size=${maxOldSpaceSizeMB}`, | ||
| '-e', 'console.log("Regular MB test")', | ||
| ], { stdio: ['pipe', 'pipe', 'pipe'] }); | ||
| assert.strictEqual(mbResult.status, 0, `Expected exit code 0 for MB value ${maxOldSpaceSizeMB}`); | ||
| assert.match(mbResult.stdout.toString(), /Regular MB test/, `Expected stdout for MB value but received ${mbResult.stdout.toString()} for value ${maxOldSpaceSizeMB}`); | ||
| assert.strictEqual(mbResult.stderr.toString(), '', `Expected empty stderr for MB value ${maxOldSpaceSizeMB}`); | ||
|
|
||
| // Test percentage calculation validation | ||
| function getHeapSizeForPercentage(percentage) { | ||
| const result = spawnSync(process.execPath, [ | ||
| `--max-old-space-size=${percentage}%`, | ||
| '-e', ` | ||
| const v8 = require('v8'); | ||
| const stats = v8.getHeapStatistics(); | ||
|
legendecas marked this conversation as resolved.
|
||
| const heapSizeLimitMB = Math.floor(stats.heap_size_limit / 1024 / 1024); | ||
| console.log(heapSizeLimitMB); | ||
| `, | ||
| ], { stdio: ['pipe', 'pipe', 'pipe'] }); | ||
|
|
||
| if (result.status !== 0) { | ||
| throw new Error(`Failed to get heap size for ${percentage}%: ${result.stderr.toString()}`); | ||
| } | ||
|
|
||
| return parseInt(result.stdout.toString(), 10); | ||
| } | ||
|
|
||
| // Test that percentages produce reasonable heap sizes | ||
| const testPercentages = [25, 50, 75, 100]; | ||
| const heapSizes = {}; | ||
|
|
||
| // Get heap sizes for all test percentages | ||
| testPercentages.forEach((percentage) => { | ||
| heapSizes[percentage] = getHeapSizeForPercentage(percentage); | ||
| }); | ||
|
|
||
| // Test relative relationships between percentages | ||
| // 50% should be roughly half of 100% | ||
| const ratio50to100 = heapSizes[50] / heapSizes[100]; | ||
| assert( | ||
| ratio50to100 >= 0.4 && ratio50to100 <= 0.6, | ||
| `50% heap size should be roughly half of 100% (got ${ratio50to100.toFixed(2)}, expected ~0.5)` | ||
| ); | ||
|
|
||
| // 25% should be roughly quarter of 100% | ||
| const ratio25to100 = heapSizes[25] / heapSizes[100]; | ||
| assert( | ||
| ratio25to100 >= 0.2 && ratio25to100 <= 0.4, | ||
| `25% heap size should be roughly quarter of 100% (got ${ratio25to100.toFixed(2)}, expected ~0.25)` | ||
| ); | ||
|
|
||
| // 75% should be roughly three-quarters of 100% | ||
| const ratio75to100 = heapSizes[75] / heapSizes[100]; | ||
| assert( | ||
| ratio75to100 >= 0.6 && ratio75to100 <= 0.9, | ||
| `75% heap size should be roughly three-quarters of 100% (got ${ratio75to100.toFixed(2)}, expected ~0.75)` | ||
| ); | ||
|
|
||
| // Test that larger percentages produce larger heap sizes | ||
| assert(heapSizes[25] <= heapSizes[50], '25% should produce smaller heap than 50%'); | ||
| assert(heapSizes[50] <= heapSizes[75], '50% should produce smaller heap than 75%'); | ||
| assert(heapSizes[75] <= heapSizes[100], '75% should produce smaller heap than 100%'); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.