Repository: nodejs/node
Pull request: Add --max-heap-size option

This PR fixs #58686

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: doc/api/cli.md
patch: @@ -3742,6 +3742,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`
@@ -4102,6 +4103,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>

filename: src/node_options.cc
patch: @@ -1207,6 +1207,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 "

filename: test/common/v8-max-heap-size-option.js
patch: @@ -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`,
+);