cli: add --max-heap-size option

PR-URL: https://github.com/nodejs/node/pull/58708
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
tannal authored and Antoine du Hamel committed 2026-04-10 14:52:21 +02:00
1 parent 71a2f82d7c
commit 3d87ecacbc
3 files changed
+57

No files matched your search

+7
View File
@@ -3612,6 +3612,7 @@ V8 options that are allowed are:
* `--expose-gc`
* `--interpreted-frames-native-stack`
* `--jitless`
* `--max-heap-size`
* `--max-old-space-size`
* `--max-semi-space-size`
* `--perf-basic-prof-only-functions`
@@ -3968,6 +3969,12 @@ documented here:
### `--jitless`
### `--max-heap-size`
Specifies the maximum heap size (in megabytes) for the process.
This option is typically used to limit the amount of memory the process can use for its JavaScript heap.
<!-- Anchor to make sure old links find a target -->
<a id="--max-old-space-sizesize-in-megabytes"></a>
+1
View File
@@ -1157,6 +1157,7 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
"help system profilers to translate JavaScript interpreted frames",
V8Option{},
kAllowedInEnvvar);
AddOption("--max-heap-size", "", V8Option{}, kAllowedInEnvvar);
AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvvar);
AddOption("--max-old-space-size-percentage",
"set V8's max old space size as a percentage of available memory "
+49
View File
@@ -0,0 +1,49 @@
'use strict';
const assert = require('assert');
const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const tmpdir = require('./tmpdir');
const testScript = `
const v8 = require('v8');
const stats = v8.getHeapStatistics();
const maxHeapSizeMB = Math.round(stats.heap_size_limit / 1024 / 1024);
console.log(maxHeapSizeMB);
`;
tmpdir.refresh();
const scriptPath = path.join(tmpdir.path, 'heap-limit-test.js');
fs.writeFileSync(scriptPath, testScript);
const child = spawnSync(
process.execPath,
[scriptPath],
{
encoding: 'utf8',
env: {
...process.env,
NODE_OPTIONS: '--max-heap-size=750',
},
},
);
assert.strictEqual(
child.status,
0,
[
`Child process did not exit cleanly.`,
` Exit code: ${child.status}`,
child.stderr ? ` Stderr: ${child.stderr.toString()}` : '',
child.stdout ? ` Stdout: ${child.stdout.toString()}` : '',
].filter(Boolean).join('\n'),
);
const output = child.stdout.trim();
const heapLimit = Number(output);
assert.strictEqual(
heapLimit,
750,
`max heap size is ${heapLimit}MB, expected 750MB`,
);