Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making sure NODE_OPTIONS --max-old-space-size is working

This may sound weird but I am setting --max-old-space-size option and I want to make sure that the configuration is in place so that I dont get runtime errors.

Is there a way to verify memory limit during runtime?

like image 818
Cemre Mengü Avatar asked May 31 '18 20:05

Cemre Mengü


2 Answers

To get heap size in megabytes pls use

const v8 = require('v8')
v8.getHeapStatistics().total_available_size / 1024 / 1024

Example 1

node --max-old-space-size=256

v8.getHeapStatistics().total_available_size / 1024 / 1024

284.47106170654297

Example 2

node --max-old-space-size=512

v8.getHeapStatistics().total_available_size / 1024 / 1024

544.9713745117188

Example 3

export NODE_OPTIONS=--max-old-space-size=1024

node

v8.getHeapStatistics().total_available_size / 1024 / 1024

1065.4709014892578

So, as we can see, both command line arg and env variable NODE_OPTIONS work.

like image 124
Andrey Avatar answered Nov 16 '22 02:11

Andrey


So to verify how much memory you are actually using you can call

process.memoryUsage()

which will return

{
  rss: 4935680,
  heapTotal: 1826816,
  heapUsed: 650472,
  external: 49879
}

heapTotal refers to memory available. heapUsed referred to heap memory used and external refers to the memory usage of C++ objects bound to JavaScript objects managed by V8. rss, Resident Set Size, is the amount of space occupied in the main memory device https://nodejs.org/api/process.html

like image 44
Lucas Hendren Avatar answered Nov 16 '22 04:11

Lucas Hendren