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?
Asked
Active
Viewed 1.8k times
15
-
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
2 Answers
27
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
- 3,866
- 1
- 15
- 33
-
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
-
-
3
-
1
-
3@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
-
3
-
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
3
you can use the process.memoryUsage https://nodejs.org/api/process.html#process_process_memoryusage
tony_k
- 1,905
- 2
- 20
- 27
-
4This 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