13

As I understand it is possible to set a maximum memory usage limit for an application like this:

node --max_old_space_size=500 app.js

Is it possible to get a value of the max_old_space_size from the code?

UPDATE

With the help of @Grégory NEUT we found out that v8.getHeapStatistics() contains a field heap_size_limit which looks like what I am looking for.

However I'm still not sure about it since I didn't find any official documentation (or some piece of code from Node.js) which will approve this theory.

I also don't understand why heap_size_limit becomes 557842432 bytes (557.8 MB) when I set max_old_space_size to 500 MB

LEQADA
  • 1,913
  • 3
  • 22
  • 41

1 Answers1

12

In the Node.js v9.4.0 Documentation you can find the function v8.getHeapStatistics() that gives you the heap size information.

UPDATE 2019: Node.js v11.x Documentation


LEQADA Example from the comment:

For max_old_space_size=500

heap_size_limit : 557842432

total_available_size : 548898944


For max_old_space_size=900

heap_size_limit : 977272832

total_available_size : 968329344


enter image description here


EDIT : process.memoryUsage() do not give informations about the max size of the heap


In the Node.js v9.4.0 Documentation you can find the function process.memoryUsage() that gives you the heap size information.

UPDATE 2019: Node.js v11.x Documentation

enter image description here

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • 2
    Thank you for the answer. I checked it. Looks like it is not the same. The value of `heapTotal` is the same `7159808` for different `max_old_space_size` values – LEQADA Jan 10 '18 at 16:04
  • 1
    @LEQADA oh ok, gonna check if there is something else. Thanks for checking it out and sorry – Orelsanpls Jan 10 '18 at 16:07
  • @LEQADA What about [v8.getHeapStatistics()](https://nodejs.org/dist/latest-v9.x/docs/api/v8.html#v8_v8_getheapstatistics) ? – Orelsanpls Jan 10 '18 at 16:18
  • 1
    Thank you for the update! Looks like it is `heap_size_limit` or `total_available_size`. For `max_old_space_size=500` `heap_size_limit` returns `557842432`, `total_available_size` returns `548898944`. For `max_old_space_size=900` `heap_size_limit` returns `977272832`, `total_available_size` returns `968329344` – LEQADA Jan 10 '18 at 16:31
  • 1
    Great job! :) Now maybe someone very strong can explain to us why there is such a difference between the limit and the actual value :D I guess something like memory pageSize or something – Orelsanpls Jan 10 '18 at 16:36
  • 1
    I found this answer https://stackoverflow.com/a/47768386/980828. It says that "`heap_size_limit`: The absolute limit the heap cannot exceed (default limit or `--max_old_space_size`)". So looks like the answer is `heap_size_limit`. Now the question is why 500 MB is 557842432 bytes. – LEQADA Jan 10 '18 at 21:29
  • 2
    old_space is just the largest configurable section of the heap, it's not all of the heap. 557842432 is 532MB..that's the total heap managed. the old_space (where objects live before garbage collected) is 500MB. – sanya Dec 14 '20 at 10:33