Skip to content

Commit 0dd92b4

Browse files
committed
cli: Add --max-heap-size option
1 parent 5fe7800 commit 0dd92b4

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

doc/api/cli.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,7 @@ V8 options that are allowed are:
34543454
* `--expose-gc`
34553455
* `--interpreted-frames-native-stack`
34563456
* `--jitless`
3457+
* `--max-heap-size`
34573458
* `--max-old-space-size`
34583459
* `--max-semi-space-size`
34593460
* `--perf-basic-prof-only-functions`
@@ -3796,6 +3797,10 @@ documented here:
37963797

37973798
### `--jitless`
37983799

3800+
### `--max-heap-size`
3801+
3802+
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, helping to prevent excessive memory usage or out-of-memory errors.
3803+
37993804
<!-- Anchor to make sure old links find a target -->
38003805

38013806
<a id="--max-old-space-sizesize-in-megabytes"></a>

src/node_options.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
10701070
"help system profilers to translate JavaScript interpreted frames",
10711071
V8Option{},
10721072
kAllowedInEnvvar);
1073+
AddOption("--max-heap-size", "", V8Option{}, kAllowedInEnvvar);
10731074
AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvvar);
10741075
AddOption("--max-semi-space-size", "", V8Option{}, kAllowedInEnvvar);
10751076
AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvvar);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { spawnSync } = require('child_process');
5+
const path = require('path');
6+
const fs = require('fs');
7+
const tmpdir = require('./tmpdir');
8+
9+
const testScript = `
10+
const v8 = require('v8');
11+
const stats = v8.getHeapStatistics();
12+
const maxHeapSizeMB = Math.round(stats.heap_size_limit / 1024 / 1024);
13+
console.log(maxHeapSizeMB);
14+
`;
15+
16+
tmpdir.refresh();
17+
const scriptPath = path.join(tmpdir.path, 'heap-limit-test.js');
18+
fs.writeFileSync(scriptPath, testScript);
19+
20+
const child = spawnSync(
21+
process.execPath,
22+
[scriptPath],
23+
{
24+
encoding: 'utf8',
25+
env: {
26+
...process.env,
27+
NODE_OPTIONS: '--max-heap-size=750',
28+
},
29+
},
30+
);
31+
32+
assert.strictEqual(
33+
child.status,
34+
0,
35+
[
36+
`Child process did not exit cleanly.`,
37+
` Exit code: ${child.status}`,
38+
child.stderr ? ` Stderr: ${child.stderr.toString()}` : '',
39+
child.stdout ? ` Stdout: ${child.stdout.toString()}` : '',
40+
].filter(Boolean).join('\n'),
41+
);
42+
const output = child.stdout.trim();
43+
const heapLimit = Number(output);
44+
45+
assert.strictEqual(
46+
heapLimit,
47+
750,
48+
`max heap size is ${heapLimit}MB, expected 750MB`,
49+
);

0 commit comments

Comments
 (0)