How to get the value of max_old_space_size from the code?

12,882

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

Share:
12,882
LEQADA
Author by

LEQADA

Updated on June 27, 2022

Comments

  • LEQADA
    LEQADA almost 2 years

    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
    LEQADA over 6 years
    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
  • Orelsanpls
    Orelsanpls over 6 years
    @LEQADA oh ok, gonna check if there is something else. Thanks for checking it out and sorry
  • Orelsanpls
    Orelsanpls over 6 years
    @LEQADA What about v8.getHeapStatistics() ?
  • LEQADA
    LEQADA over 6 years
    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
  • Orelsanpls
    Orelsanpls over 6 years
    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
  • LEQADA
    LEQADA over 6 years
    I found this answer 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.
  • SandorRacz
    SandorRacz over 3 years
    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.