Node.js – v8.getHeapSpaceStatistics() Method


The v8.getHeapSpaceStatistics() method returns the statistics about the heap spaces. Both the ordering of the heap spaces and its availability can’t be guaranteed as the statistics are provided by V8 getHeapSpaceStatistics. Since it depends upon the V8 version, it may change from one version to another.

Syntax

v8.getHeapSpaceStatistics()

Parameters

Since it returns the space details/statistics, it does not require any special input parameters. However, the value returned contains the following properties −

  • space_name − This field will represent the name of the heap space.

  • space_size − Size of the space.

  • space_used_size − Size that is currently being used by the V8 and the CPU for processing.

  • space_available_size − This is the available space size in heap after deducting the used space.

  • physical_space_size − This field represents the total available heap size.

Example 1

Create a file with the name "spaceStats.js" and copy the following code snippet. After creating the file, use the command "node spaceStats.js" to run this code.

 Live Demo

// v8.getHeapSpaceStatistics() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Printing the stats details
console.log(v8.getHeapSpaceStatistics());

Output

C:\home
ode>> node spaceStats.js [ { space_name: 'read_only_space',    space_size: 524288,    space_used_size: 35200,    space_available_size: 480384,    physical_space_size: 43904 }, { space_name: 'new_space',    space_size: 4194304,    space_used_size: 1088680,    space_available_size: 973656,    physical_space_size: 2165496 }, { space_name: 'old_space',    space_size: 3145728,    space_used_size: 2410200,    space_available_size: 391856,    physical_space_size: 2462424 }, { space_name: 'code_space',    space_size: 1572864,    space_used_size: 1208224,    space_available_size: 448,    physical_space_size: 1257376 }, { space_name: 'map_space',    space_size: 524288,    space_used_size: 422488,    space_available_size: 0,    physical_space_size: 431192 }, { space_name: 'large_object_space',    space_size: 0,    space_used_size: 0,    space_available_size: 1516756480,    physical_space_size: 0 } ]

Example 2

Let’s have a look at one more example

// v8.getHeapSpaceStatistics() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Calling v8.getHeapSpaceStatistics()
stats = v8.getHeapSpaceStatistics();

// Initialiing the list
var myList = []
for (var i = 0; i < stats.length; i++){
var element = stats[i];

// Pushing the space data into table
myList.push({ "Name of the Space": element['space_name'],
"Size of Space": element['space_size'],
"Space Size Used": element['space_used_size'],
"Space Size Available": element['space_available_size'],
"Physical Space Size":element['physical_space_size'] },
);
}

// Printing the data in tabular form
console.table(myList)

Output

C:\home
ode>> node spaceStats.js

Updated on: 18-Aug-2021

102 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements