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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With