Nodejs v8 getHeapSpaceStatistics method | Heap space statistics in node
In this tutorial, You learn v8.getHeapSpaceStatistics()
method in v8 module of nodejs with examples The getHeapSpaceStatistics
method returns statistics about heap sizes based on spaces. v8 is an opensource javascript engine from google chrome and used by nodejs and MongoDB
What is a space in v8? Space
is a chunk of memory allocated by the v8
engine. V8
has the following different spaces for storing and garbage-cleaning objects in heap memory.
new space
: this is small memory used for new objects for garbage collection of objects quickly.old pointer space
: store long live objects which point to other objects- old data space`: these are objects, plain raw data has no reference to another object
code space
: executable memory for compilation and JITed executionsCell
space,property-cell-space, and map-space
: Storing the same size of cell property and map-related objects.large object space
: storing large objects
The getHeapSpaceStatistics
method return the above object space statistics.
Each object holds information about space available used and physical heap sizes information.
V8 is an engine is used internally by the Nodejs environment. You can check v8 version and 32 bit or 64 bit for Nodejs
Sometimes, To debug node out of memory errors, We need to know the heap size of an environment and application.
To get the above information, Nodejs has an inbuilt module v8
, It has an inbuilt getHeapSpaceStatistics
to get statistical information about heap memory allocated based on spaces.
V8 has another method [getHeapStatistics](/nodejs-v8-getheapstatistics-method)
about the stats of heap memory in the entire system. Syntax:
getHeapSpaceStatistics();
It returns an object with the following properties.
space_name
: Name of the space used, examples are read_only_space,new_space,old_space,code_space,map_space,large_object_space,code_large_object_space,new_large_object_spacespace_size
: Allocated space heap size in bytesspace_used_size
: Used space heap size in bytesspace_available_size
: Available space heap size in bytesphysical_space_size
: Physical space heap size in bytes
It gives a heap of information.
v8 getHeapSpaceStatistics method example
first import the v8 module using the require
keyword in the ES5 syntax
const v8module = require("v8");
Here is a code for v8 getHeapSpaceStatistics example info.js
const v8module = require("v8");
console.log(v8module.getHeapSpaceStatistics());
running info.js in the command line with the node command
node info.js
It outputs an object
[
{
space_name: 'read_only_space',
space_size: 151552,
space_used_size: 150392,
space_available_size: 0,
physical_space_size: 151552
},
{
space_name: 'new_space',
space_size: 2097152,
space_used_size: 881912,
space_available_size: 165512,
physical_space_size: 2097152
},
{
space_name: 'old_space',
space_size: 1458176,
space_used_size: 1349464,
space_available_size: 248,
physical_space_size: 1458176
},
{
space_name: 'code_space',
space_size: 360448,
space_used_size: 87456,
space_available_size: 0,
physical_space_size: 360448
},
{
space_name: 'map_space',
space_size: 528384,
space_used_size: 262152,
space_available_size: 0,
physical_space_size: 528384
},
{
space_name: 'large_object_space',
space_size: 135168,
space_used_size: 131112,
space_available_size: 0,
physical_space_size: 135168
},
{
space_name: 'code_large_object_space',
space_size: 0,
space_used_size: 0,
space_available_size: 0,
physical_space_size: 0
},
{
space_name: 'new_large_object_space',
space_size: 0,
space_used_size: 0,
space_available_size: 1047424,
physical_space_size: 0
}
]
Conclusion
To Sum up, It is helpful for developers to profile a node application by using getHeapSpaceStatistics to improve performance.