22

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?

qibobo
  • 463
  • 1
  • 5
  • 13
  • i used this once https://www.npmjs.com/package/memwatch `memwatch.on('stats', function(stats) { ... });` also @alex-rokabilis seems right – Aishwat Singh Oct 29 '15 at 16:49

3 Answers3

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

Alex Michailidis
  • 4,078
  • 1
  • 16
  • 35
  • 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? – qibobo Nov 03 '15 at 05:39
  • @qibobo you basically can't. This function was added on v1.0.0 – LEQADA Jan 11 '18 at 16:23
  • 4
    This answer is far better than running `process.memoryUsage`. – nicholaswmin Mar 23 '18 at 08:53
  • 5
    @qibobo 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. – shriek Apr 26 '19 at 19:48
  • 5
    @shriek `/ 1024 / 1024` will give you MB, not GB – J. Schei Jul 29 '20 at 19:27
  • 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) – Sabunkar Tejas Sahailesh Oct 13 '20 at 10:29
  • 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`. – Andrew Feb 24 '23 at 08:42
2

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

tony_k
  • 1,983
  • 2
  • 20
  • 27
  • 6
    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. – Gary Holiday Feb 12 '20 at 06:35
0

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);
  }
Andrew
  • 73
  • 1
  • 6