Open In App

Node.js v8.getHeapSpaceStatistics() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The v8.getHeapSpaceStatistics() method is an inbuilt application programming interface of the v8 module which is used to get statistics about heap space derived from the v8 version.

Syntax:

v8.getHeapSpaceStatistics();

Parameters: This method does not have any parameters.

Return Value: This method returns an object that contains statistics about version 8 heap space. The returned object usually contains an array of multiple elements, where each element consists of the following fields:

  • space_name: A string represents the name of the heap space.
  • space_size: A number represents the heap space size.
  • space_used_size: A number represents the used heap space size.
  • space_available_size: A number, represents available heap space size.
  • physical_space_size: A number, specifying physical heap space size.

Example 1: The below example illustrates the use of the v8.getHeapSpaceStatistics() method in Node.js.

Filename: index.js 

javascript




// Accessing v8 module
const v8 = require('v8');
 
// Calling v8.getHeapSpaceStatistics()
console.log(v8.getHeapSpaceStatistics());


Run the index.js file using the following command:

node index.js

Output:

[ { space_name: 'read_only_space',
    space_size: 524288,
    space_used_size: 35208,
    space_available_size: 480376,
    physical_space_size: 524288 },
  { space_name: 'new_space',
    space_size: 2097152,
    space_used_size: 975376,
    space_available_size: 55792,
    physical_space_size: 2097152 },
  { space_name: 'old_space',
    space_size: 2330624,
    space_used_size: 2272448,
    space_available_size: 184,
    physical_space_size: 2330624 },
  { space_name: 'code_space',
    space_size: 1048576,
    space_used_size: 571968,
    space_available_size: 0,
    physical_space_size: 1048576 },
  { space_name: 'map_space',
    space_size: 536576,
    space_used_size: 344784,
    space_available_size: 0,
    physical_space_size: 536576 },
  { space_name: 'large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 1520180736,
    physical_space_size: 0 } ]

Example 2: The below example illustrates the use of the v8.getHeapSpaceStatistics() method in Node.js.

Filename: index.js 

javascript




// Accessing v8 module
const v8 = require('v8');
 
// Calling v8.getHeapSpaceStatistics()
stats = v8.getHeapSpaceStatistics();
 
let myList = []
for (let i = 0; i < stats.length; i++) {
    let element = stats[i];
 
    myList.push({
        "Space Name": element['space_name'],
        "Space Size": element['space_size'],
        "Used Space Size": element['space_used_size'],
        "Space Available": element['space_available_size'],
        "Physical Space Size": element['physical_space_size']
    },
    );
}
 
// Printing in tabular form
console.table(myList)


Run the index.js file using the following command:

node index.js

Output: Reference: https://nodejs.org/api/v8.html#v8_v8_getheapspacestatistics



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads