get the max heap size of a node.js application
Asked Answered
B

4

24

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?

Bleeding answered 29/10, 2015 at 15:54 Comment(1)
i used this once npmjs.com/package/memwatch memwatch.on('stats', function(stats) { ... }); also @alex-rokabilis seems rightLoch
N
32

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

Neely answered 29/10, 2015 at 16:24 Comment(7)
I install node.js v0.12.7 and require('v8'), when try to run the program and it shows "Cannot find module 'v8'". Then I install node.js v5.0.0 and it works. How can I make it work in v0.12.7?Bleeding
@Bleeding you basically can't. This function was added on v1.0.0Sightless
This answer is far better than running process.memoryUsage.Coxcomb
@Bleeding this answer should be marked correct imo. To be precise, we can do v8.getHeapStatistics().total_available_size / 1024 / 1024 (in GB) to get that max allowed.Cult
@Cult / 1024 / 1024 will give you MB, not GBKettledrum
Just to reframe the steps : Open Nodejs and then use this formula as suggested to get in GB : (v8.getHeapStatistics().total_available_size / 1024 /1024 / 1024).toFixed(2)Uncurl
I don't believe this is the correct answer. The heap consists of several parts, where the "old space" is only one of them. I don't have proof, but I think that total_available_size != --max-old-space-size, more specifically it might be that total_available_size > --max-old-space-size.Ambary
A
2

you can use the process.memoryUsage https://nodejs.org/api/process.html#process_process_memoryusage

Arrington answered 26/4, 2017 at 12:23 Comment(1)
This does not tell you the maximum heap size, it tells you the current heap size and how much of the heap you are using.Kidney
A
1

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);
  }
Ambary answered 24/2, 2023 at 10:0 Comment(0)
S
1

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
Secular answered 13/6 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.