Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of max_old_space_size from the code?

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

like image 917
LEQADA Avatar asked Jan 10 '18 15:01

LEQADA


People also ask

What is Max_old_space_size?

--max_old_space_size=... limits the total size of all objects on the heap.

How do I determine the correct max old space size for Node JS?

By default, the memory limit in Node. js is 512 MB. To increase this amount, you need to set the memory limit argument —-max-old-space-size . It will help avoid a memory limit issue.

What is NODE_OPTIONS?

The NODE_OPTIONS --max-old-space-size environment variable allows to increase Node's max heap size. Setting an environmental variable allows Node to read this value from your environment and so we don't need to pass this value as an argument every time we run Node command.

What is Max old space size NPM?

This tool will append --max-old-space-size=4096 in all node calls inside your node_modules/.


1 Answers

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

like image 78
Orelsanpls Avatar answered Oct 20 '22 15:10

Orelsanpls