From c633fdb282d6a21df10d4e5f417f75b294963e77 Mon Sep 17 00:00:00 2001 From: Keyhan Vakil Date: Tue, 26 Jul 2022 04:56:28 +0000 Subject: [PATCH] worker: add flag to control old space size This adds a new flag `--thread-max-old-space-size` (name completely provisional). This has two advantages over the existing `--max-old-space-size` flag: 1. It allows setting the old space size for the main thread and using `resourceLimits` for worker threads. Currently `resourceLimits` will be ignored when `--max-old-space-size` is set (see the attached issues). 2. It is implemented using V8's public API, rather than relying on V8's internal flags whose stability and functionality are not guaranteed. The downside is that there are now two flags which (in most cases) do the same thing, so it may cause some confusion. I also think that we should deprecate `--max-old-space-size`, since the semantics feel pretty error-prone, but that's a story for another day. Refs: https://github.com/nodejs/node/issues/41066 Refs: https://github.com/nodejs/node/issues/43991 Refs: https://github.com/nodejs/node/pull/43992 --- doc/api/cli.md | 22 ++++++++++++ doc/node.1 | 5 +++ src/api/environment.cc | 4 ++- src/env.cc | 5 ++- src/env.h | 3 +- src/node_main_instance.cc | 18 ++++++++-- src/node_options.cc | 6 ++++ src/node_options.h | 1 + test/common/allocate-and-check-limits.js | 34 ++++++++++++++++++ test/fixtures/thread-max-old-space-size.js | 3 ++ .../test-thread-max-old-space-size.js | 22 ++++++++++++ .../test-worker-max-old-space-size.js | 35 +++++++++++++++++++ test/parallel/test-worker-resource-limits.js | 29 ++------------- 13 files changed, 153 insertions(+), 34 deletions(-) create mode 100644 test/common/allocate-and-check-limits.js create mode 100644 test/fixtures/thread-max-old-space-size.js create mode 100644 test/parallel/test-thread-max-old-space-size.js create mode 100644 test/parallel/test-worker-max-old-space-size.js diff --git a/doc/api/cli.md b/doc/api/cli.md index f0a2e70b8d7b666..ee58f774b7ae6c1 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1125,6 +1125,20 @@ added: v18.0.0 Configures the test runner to only execute top level tests that have the `only` option set. +### `--thread-max-old-space-size` + + + +Sets the max memory size of V8's old memory section for the main thread (in +megabytes). As memory consumption approaches the limit, V8 will spend more time +on garbage collection in an effort to free unused memory. + +Unlike [`--max-old-space-size`][], this option doesn't affect any additional +[worker threads][]. To configure the old space size for worker threads, pass in +an appropriate [`resourceLimits`][] to their constructor. + ### `--throw-deprecation`