Now I am using node.js in paas platform. And the container has memory limit. Now I want to get the maximum of the heap size of node.js application. I know that use the parameter "--max-old-space-size", I can set the max heap size, but I want to know how to get the default value of "--max-old-space-size". For example, if the container's memory is 512m , so the maximum of node.js heap size can not be greater than it. How can I get the value?
You can try the v8
module, it is documented here: https://nodejs.org/api/v8.html
I believe getHeapStatistics()
function will give you everything you need
process.memoryUsage
. –
Coxcomb v8.getHeapStatistics().total_available_size / 1024 / 1024
(in GB) to get that max allowed. –
Cult / 1024 / 1024
will give you MB, not GB –
Kettledrum total_available_size != --max-old-space-size
, more specifically it might be that total_available_size > --max-old-space-size
. –
Ambary you can use the process.memoryUsage
https://nodejs.org/api/process.html#process_process_memoryusage
I don't think it's possible. V8 does not expose this information via its API.
The information available via node:v8
library does not include the "maximum old space size", contrary to the accepted answer. The information returned from getHeapStatistics()
only contain total_heap_size
.
There is a chance that getHeapSpaceStatistics()
(notice the "Space
" in the method name) might contain the required information, but from my understanding it's also only runtime information, and the space_available_size
might not reflect the max-old-space-size
implicit setting.
There is an issue in the Node repository to add a simple explanation about the general heap size to docs, but it's still unresolved.
I was able to find the part of the V8 source code that calculates the default, but it was hard for me to understand it and explain in simpler pseudocode. Maybe somebody will find it helpful:
// Initialize max_old_generation_size_ and max_global_memory_.
{
size_t max_old_generation_size = 700ul * (kSystemPointerSize / 4) * MB;
if (constraints.max_old_generation_size_in_bytes() > 0) {
max_old_generation_size = constraints.max_old_generation_size_in_bytes();
}
if (v8_flags.max_old_space_size > 0) {
max_old_generation_size =
static_cast<size_t>(v8_flags.max_old_space_size) * MB;
} else if (v8_flags.max_heap_size > 0) {
size_t max_heap_size = static_cast<size_t>(v8_flags.max_heap_size) * MB;
size_t young_generation_size =
YoungGenerationSizeFromSemiSpaceSize(max_semi_space_size_);
max_old_generation_size = max_heap_size > young_generation_size
? max_heap_size - young_generation_size
: 0;
}
max_old_generation_size =
std::max(max_old_generation_size, MinOldGenerationSize());
max_old_generation_size = std::min(max_old_generation_size,
AllocatorLimitOnMaxOldGenerationSize());
max_old_generation_size =
RoundDown<Page::kPageSize>(max_old_generation_size);
max_global_memory_size_ =
GlobalMemorySizeFromV8Size(max_old_generation_size);
set_max_old_generation_size(max_old_generation_size);
}
I made some tests and extrapolated that v8.getHeapStatistics().heap_size_limit
- 48MiB
= --max-old-space-size
, at least on my machine.
Also, --max-old-space-size = 2048
by default, at least on my machine.
It would be really something if you could confirm / refute / correct these values on your machine, by running this simple script with different values of --max-old-space-size
:
const v8 = require('node:v8');
// console.log('Heap Statistics', v8.getHeapStatistics());
// console.log('Memory usage', process.memoryUsage());
const correction = 48 * 1024 * 1024;
const hypothesis = (v8.getHeapStatistics().heap_size_limit - correction) / 1024 / 1024;
console.log(`\n==> Computed heap size = ${hypothesis}\n`);
For example, I get these results:
$ node --max-old-space-size=3072 test-heap.cjs
==> Computed heap size = 3072
$ node --max-old-space-size=3000 test-heap.cjs
==> Computed heap size = 3000
$ node test-heap.cjs
==> Computed heap size = 2048
© 2022 - 2024 — McMap. All rights reserved.
memwatch.on('stats', function(stats) { ... });
also @alex-rokabilis seems right – Loch