Node.js V8
Node.js V8 (What is V8)
When we say about the V8, it is an open source JavaScript engine developed by the chromium project for the Google Chrome web browser. And it is written in C++. And now a days, this is being used by many projects such as MongoDB, Node.js and Couchbase.
V8 in Node.js
The Node.js V8 module represents interfaces and event specific to the version of V8. And this will provides methods to get information about heap memory through v8.getHeapStatistics() and v8.getHeapSpaceStatistics() methods.
And to use this module, you need to use require('v8').
const v8 = require('v8');
Node.js v8.getHeapStatistics() Example
The Node.js v8.getHeapStatistics() method return statistics about the heap such as total heap size, heap size limit, used heap size, total available size etc.
File: v8-example1.js
const v8 = require('v8');
console.log(v8.getHeapStatistics());
Node.js v8.getHeapSpaceStatistics() Example
The Node.js v8.getHeapSpaceStatistics() method returns statistics about the heap space. And this will returns an array of 5 objects:-
(1)New space
(2)Old space
(3)Code space
(4)Map space
(5)large object space
Here each object contains information about the space name, space size, space available size, physical space size and space used size.
File: v8-example2.js
const v8 = require('v8');
console.log(v8.getHeapSpaceStatistics());
Memory limit of V8 in Node.js
Currently, by default v8 has a memory limit of 512mb on 32-bit and 1gb on 64-bit systems. And we can also raise the limit by setting --max-old-space-size to a maximum of ~1gb for 32-bit and ~1.7gb for 64-bit systems. But it is recommended to split your single process into several workers if you are hitting memory limits. | |