Skip to content
Related Articles
Open in App
Not now

Related Articles

Node.js v8.getHeapSpaceStatistics() Method

Improve Article
Save Article
  • Last Updated : 28 Jul, 2020
Improve Article
Save Article

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 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 used heap space size.
  • space_available_size: A number, represents available heap space size.
  • physical_space_size: A number, specifying physical heap space size.

Below examples illustrate the use of v8.getHeapSpaceStatistics() method in Node.js.

Example 1: Filename: index.js




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


Run 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: Filename: index.js




// Accessing v8 module
const v8 = require('v8');
   
// Calling v8.getHeapSpaceStatistics() 
stats = v8.getHeapSpaceStatistics();
   
var myList = []
for (var i = 0; i < stats.length; i++){    
  var 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 index.js file using the following command:

node index.js

Output:

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!