How to get the value of max_old_space_size from the code?
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
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
LEQADA
Updated on June 27, 2022Comments
-
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 fieldheap_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
becomes557842432
bytes (557.8
MB) when I setmax_old_space_size
to500
MB -
LEQADA over 6 yearsThank you for the answer. I checked it. Looks like it is not the same. The value of
heapTotal
is the same7159808
for differentmax_old_space_size
values -
Orelsanpls over 6 years@LEQADA oh ok, gonna check if there is something else. Thanks for checking it out and sorry
-
Orelsanpls over 6 years@LEQADA What about v8.getHeapStatistics() ?
-
LEQADA over 6 yearsThank you for the update! Looks like it is
heap_size_limit
ortotal_available_size
. Formax_old_space_size=500
heap_size_limit
returns557842432
,total_available_size
returns548898944
. Formax_old_space_size=900
heap_size_limit
returns977272832
,total_available_size
returns968329344
-
Orelsanpls over 6 yearsGreat 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 over 6 yearsI 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 isheap_size_limit
. Now the question is why 500 MB is 557842432 bytes. -
SandorRacz over 3 yearsold_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.