Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -4351,6 +4351,7 @@ one is included in the list below.
* `--localstorage-file`
* `--max-http-header-size`
* `--max-old-space-size-percentage`
* `--max-old-space-size`
* `--network-family-autoselection-attempt-timeout`
* `--no-addons`
* `--no-deprecation`
Expand Down Expand Up @@ -4463,7 +4464,6 @@ V8 options that are allowed are:
* `--interpreted-frames-native-stack`
* `--jitless`
* `--max-heap-size`
* `--max-old-space-size`
* `--max-semi-space-size`
* `--perf-basic-prof-only-functions`
* `--perf-basic-prof`
Expand Down
7 changes: 5 additions & 2 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -1656,8 +1656,9 @@ changes:
including no `ArrayBuffer`s. Even if these limits are set, the process may
still abort if it encounters a global out-of-memory situation.
* `maxOldGenerationSizeMb` {number} The maximum size of the main heap in
MB. If the command-line argument [`--max-old-space-size`][] is set, it
overrides this setting.
MB. Defaults to the value inherited from the parent thread (including
[`--max-old-space-size`][] or [`--max-old-space-size-percentage`][]). If
[`--max-heap-size`][] is set, it overrides this setting.
* `maxYoungGenerationSizeMb` {number} The maximum size of a heap space for
recently created objects. If the command-line argument
[`--max-semi-space-size`][] is set, it overrides this setting.
Expand Down Expand Up @@ -2228,6 +2229,8 @@ thread spawned will spawn another until the application crashes.
[`'close'` event]: #event-close
[`'exit'` event]: #event-exit
[`'online'` event]: #event-online
[`--max-heap-size`]: cli.md#--max-heap-size
[`--max-old-space-size-percentage`]: cli.md#--max-old-space-size-percentagepercentage
[`--max-old-space-size`]: cli.md#--max-old-space-sizesize-in-mib
[`--max-semi-space-size`]: cli.md#--max-semi-space-sizesize-in-mib
[`AsyncResource`]: async_hooks.md#class-asyncresource
Expand Down
4 changes: 4 additions & 0 deletions doc/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@
"type": "number",
"description": "set the maximum size of HTTP headers (default: 16384 (16KB))"
},
"max-old-space-size": {
"type": "string",
"description": "set V8's max old space size (in MiB)"
},
"max-old-space-size-percentage": {
"type": "string",
"description": "set V8's max old space size as a percentage of available memory (e.g., '50%'). Takes precedence over --max-old-space-size."
Expand Down
4 changes: 2 additions & 2 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,8 @@ one is included in the list below.
.It
\fB--max-old-space-size-percentage\fR
.It
\fB--max-old-space-size\fR
.It
\fB--network-family-autoselection-attempt-timeout\fR
.It
\fB--no-addons\fR
Expand Down Expand Up @@ -2574,8 +2576,6 @@ V8 options that are allowed are:
.It
\fB--max-heap-size\fR
.It
\fB--max-old-space-size\fR
.It
\fB--max-semi-space-size\fR
.It
\fB--perf-basic-prof-only-functions\fR
Expand Down
3 changes: 3 additions & 0 deletions src/api/embed_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ CommonEnvironmentSetup::CommonEnvironmentSetup(
// it will be owned by the snapshot creator and needs to be cleaned up
// before serialization.
if (flags & Flags::kIsForSnapshotting) {
SetIsolateMaxOldGenerationSize(
&params.constraints,
per_process::cli_options->per_isolate->max_old_space_size_mb);
// The isolate must be registered before the SnapshotCreator initializes the
// isolate, so that the memory reducer can be initialized.
isolate = impl_->isolate = Isolate::Allocate(GetOrCreateIsolateGroup());
Expand Down
27 changes: 21 additions & 6 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,32 @@ void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
delete allocator;
}

void SetIsolateMaxOldGenerationSize(v8::ResourceConstraints* constraints,
uint64_t max_old_space_size_mb) {
if (max_old_space_size_mb == 0) return;
constexpr size_t kMB = 1024 * 1024;
constraints->set_max_old_generation_size_in_bytes(
static_cast<size_t>(
std::min<uint64_t>(max_old_space_size_mb, SIZE_MAX / kMB)) *
kMB);
}

void SetIsolateCreateParamsForNode(Isolate::CreateParams* params) {
const uint64_t constrained_memory = uv_get_constrained_memory();
const uint64_t total_memory = constrained_memory > 0 ?
std::min(uv_get_total_memory(), constrained_memory) :
uv_get_total_memory();
if (total_memory > 0 &&
params->constraints.max_old_generation_size_in_bytes() == 0) {
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
// This default is based on browser use-cases. Tell V8 to configure the
// heap based on the actual physical memory.
params->constraints.ConfigureDefaults(total_memory, 0);
if (params->constraints.max_old_generation_size_in_bytes() == 0) {
if (total_memory > 0) {
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively.
// This default is based on browser use-cases. Tell V8 to configure the
// heap based on the actual physical memory.
params->constraints.ConfigureDefaults(total_memory, 0);
}
Mutex::ScopedLock lock(per_process::cli_options_mutex);
SetIsolateMaxOldGenerationSize(
&params->constraints,
per_process::cli_options->per_isolate->max_old_space_size_mb);
}

#ifdef NODE_ENABLE_VTUNE_PROFILING
Expand Down
22 changes: 19 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,16 @@ static void CheckGlobalProcessTimeoutOptions(
per_process::cli_options->CheckProcessTimeoutOptions(errors, argv);
}

static bool HasMaxHeapSizeFlag(const std::vector<std::string>& v8_args) {
return std::ranges::any_of(v8_args | std::views::drop(1), [](std::string a) {
std::ranges::replace(a, '_', '-');
std::string_view s = a;
if (!s.starts_with('-')) return false;
s.remove_prefix(s.starts_with("--") ? 2 : 1);
return s == "max-heap-size" || s.starts_with("max-heap-size=");
});
}

static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
std::vector<std::string>* exec_args,
std::vector<std::string>* errors,
Expand Down Expand Up @@ -801,11 +811,17 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
return ExitCode::kInvalidCommandLineArgument2;
}

if (!per_process::cli_options->per_isolate->max_old_space_size_percentage
.empty()) {
static bool has_max_heap_size_flag = false;
has_max_heap_size_flag |= HasMaxHeapSizeFlag(v8_args);

// Only hand --max-old-space-size to V8 when --max-heap-size needs it;
// otherwise it's applied per isolate so workers can override it.
if (has_max_heap_size_flag &&
per_process::cli_options->per_isolate->max_old_space_size_mb != 0) {
v8_args.emplace_back(
"--max_old_space_size=" +
per_process::cli_options->per_isolate->max_old_space_size);
std::to_string(
per_process::cli_options->per_isolate->max_old_space_size_mb));
}

auto env_opts = per_process::cli_options->per_isolate->per_env;
Expand Down
2 changes: 2 additions & 0 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ class InitializationResultImpl final : public InitializationResult {

void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
void SetIsolateMaxOldGenerationSize(v8::ResourceConstraints* constraints,
uint64_t max_old_space_size_mb);

#if HAVE_INSPECTOR
namespace profiler {
Expand Down
26 changes: 23 additions & 3 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ void PerProcessOptions::CheckProcessTimeoutOptions(
}
}

void PerIsolateOptions::HandleMaxOldSpaceSize(
std::vector<std::string>* errors) {
uint64_t parsed_mb = 0;
const char* begin = max_old_space_size.data();
const char* end = begin + max_old_space_size.size();
auto [ptr, ec] = std::from_chars(begin, end, parsed_mb);
if (ec != std::errc() || ptr != end) {
errors->push_back("--max-old-space-size must be a valid non-negative "
"integer. Got: " +
max_old_space_size);
return;
}
max_old_space_size_mb = parsed_mb;
}

void PerIsolateOptions::HandleMaxOldSpaceSizePercentage(
std::vector<std::string>* errors,
std::string* max_old_space_size_percentage) {
Expand Down Expand Up @@ -280,12 +295,14 @@ void PerIsolateOptions::HandleMaxOldSpaceSizePercentage(
uint64_t memory_mb = available_memory / (1024 * 1024);
uint64_t calculated_mb = static_cast<size_t>(memory_mb * percentage / 100.0);

// Convert back to string
max_old_space_size = std::to_string(calculated_mb);
max_old_space_size_mb = calculated_mb;
}

void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors,
std::vector<std::string>* argv) {
if (!max_old_space_size.empty()) {
HandleMaxOldSpaceSize(errors);
}
if (!max_old_space_size_percentage.empty()) {
HandleMaxOldSpaceSizePercentage(errors, &max_old_space_size_percentage);
}
Expand Down Expand Up @@ -1574,7 +1591,10 @@ PerIsolateOptionsParser::PerIsolateOptionsParser(
V8Option{},
kAllowedInEnvvar);
AddOption("--max-heap-size", "", V8Option{}, kAllowedInEnvvar);
AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvvar);
AddOption("--max-old-space-size",
"set V8's max old space size (in MiB)",
&PerIsolateOptions::max_old_space_size,
kAllowedInEnvvar);
AddOption("--max-old-space-size-percentage",
"set V8's max old space size as a percentage of available memory "
"(e.g., '50%'). Takes precedence over --max-old-space-size.",
Expand Down
2 changes: 2 additions & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class PerIsolateOptions : public Options {

std::string max_old_space_size_percentage;
std::string max_old_space_size;
uint64_t max_old_space_size_mb = 0;
std::string report_signal = "SIGUSR2";
std::string build_snapshot_config;

Expand All @@ -362,6 +363,7 @@ class PerIsolateOptions : public Options {
inline EnvironmentOptions* get_per_env_options();
void CheckOptions(std::vector<std::string>* errors,
std::vector<std::string>* argv) override;
void HandleMaxOldSpaceSize(std::vector<std::string>* errors);
void HandleMaxOldSpaceSizePercentage(std::vector<std::string>* errors,
std::string* max_old_space_size);

Expand Down
9 changes: 9 additions & 0 deletions src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ void Worker::UpdateResourceConstraints(ResourceConstraints* constraints) {
constraints->set_max_old_generation_size_in_bytes(
static_cast<size_t>(resource_limits_[kMaxOldGenerationSizeMb] * kMB));
} else {
SetIsolateMaxOldGenerationSize(constraints,
per_isolate_opts_->max_old_space_size_mb);
resource_limits_[kMaxOldGenerationSizeMb] =
constraints->max_old_generation_size_in_bytes() / kMB;
}
Expand Down Expand Up @@ -575,6 +577,8 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {

if (args[1]->IsObject() || args[2]->IsArray()) {
per_isolate_opts.reset(new PerIsolateOptions());
per_isolate_opts->max_old_space_size_mb =
env->isolate_data()->options()->max_old_space_size_mb;

HandleEnvOptions(per_isolate_opts->per_env, [&env_vars](const char* name) {
return env_vars->Get(name).value_or("");
Expand Down Expand Up @@ -614,6 +618,11 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) {
per_process_opts.get(),
kAllowedInEnvvar,
&errors);
// Inherit the parent's effective limit; execArgv may still override it.
per_isolate_opts->max_old_space_size_mb =
env->isolate_data()->options()->max_old_space_size_mb;
per_isolate_opts->max_old_space_size.clear();
per_isolate_opts->max_old_space_size_percentage.clear();
} else {
options_parser::Parse(&env_argv,
nullptr,
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-worker-resource-limits.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --max-old-space-size=512
'use strict';
const common = require('../common');
const assert = require('assert');
Expand All @@ -6,6 +7,20 @@ const { Worker, resourceLimits, isMainThread } = require('worker_threads');

if (isMainThread) {
assert.deepStrictEqual(resourceLimits, {});
const defaultLimitWorker = new Worker(
'const { parentPort, resourceLimits } = require("worker_threads");' +
'parentPort.postMessage(resourceLimits.maxOldGenerationSizeMb);',
{ eval: true });
defaultLimitWorker.on('message', common.mustCall((limit) => {
assert.strictEqual(limit, 512);
}));
const execArgvWorker = new Worker(
'const { parentPort, resourceLimits } = require("worker_threads");' +
'parentPort.postMessage(resourceLimits.maxOldGenerationSizeMb);',
{ eval: true, execArgv: ['--max-old-space-size=128'] });
execArgvWorker.on('message', common.mustCall((limit) => {
assert.strictEqual(limit, 128);
}));
}

const testResourceLimits = {
Expand Down
Loading