• Node.js Video Tutorials

NodeJS v8.getHeapSpaceStatistics() Method



The NodeJS v8.getHeapSpaceStatistics() Method returns the statistics of the v8 heap space. This method returns the statistics as an array of objects such as new space, old space, code space, map space, and large object space. Each object contains data about space name, space size, space used size, space available size, and physical space size.

Syntax

Following is the syntax of the NodeJS v8.getHeapSpaceStatistics() Method

v8.getHeapSpaceStatistics()

Parameters

This method does not accept any parameters.

Return Value

This method returns an array of objects that contains statistics of v8 heap space.

The following are properties present in the returned array of objects −

  • space_name − This is a string that specifies the name of the heap space.

  • space_size − This is a number that specifies the size of the heap space

  • space_used_size − This is a number that specifies the used heap space size.

  • space_available_size − A number that specifies the available heap space size.

  • physical_space_size − A number that specifies the physical heap space size.

Example

In the following example, we are trying to get the statistics of the v8 heap space using NodeJS v8.getHeapSpaceStatistics() method.

const v8 = require('v8');
console.log(v8.getHeapSpaceStatistics());

Output

After executing the above program, the NodeJS getHeapSpaceStatistics() method returned an array of objects containing properties about v8 heap space.

Statistics of v8 heap space: 
[ 
   {
      space_name: 'new_space',
      space_size: 2097152,
      space_used_size: 653080,
      space_available_size: 378088,
      physical_space_size: 2059736 
   },
   { 
      space_name: 'old_space',
      space_size: 2396160,
      space_used_size: 2295248,
      space_available_size: 168,
      physical_space_size: 2338768 
   },
   { 
      space_name: 'code_space',
      space_size: 1048576,
      space_used_size: 572736,
      space_available_size: 0,
      physical_space_size: 605504 
   },
   { 
      space_name: 'map_space',
      space_size: 544768,
      space_used_size: 288160,
      space_available_size: 0,
      physical_space_size: 305568 
   },
   { 
      space_name: 'large_object_space',
      space_size: 0,
      space_used_size: 0,
      space_available_size: 17206803968,
      physical_space_size: 0 
   } 
]

Example

In the following example, we are trying to print the statistics about the heap space in a tabular form.

const v8 = require('v8');
let statistics = v8.getHeapSpaceStatistics();
var EmpArr = []
for (var i = 0; i < statistics.length; i++){    
   var element = statistics[i];  
   EmpArr.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'] },);
}
console.table(EmpArr)

Output

┌─────────┬───────────────────────────┬────────────┬─────────────────┬─────────────────┬─────────────────────┐
│ (index) │        Space Name         │ Space Size │ Used Space Size │ Space Available │ Physical Space Size │
├─────────┼───────────────────────────┼────────────┼─────────────────┼─────────────────┼─────────────────────┤
│    0    │     'read_only_space'     │   262144   │      32808      │        0        │        33088        │
│    1    │        'new_space'        │  2097152   │     649048      │     398408      │       2088064       │
│    2    │        'old_space'        │  1916928   │     1582912     │      88736      │       1585152       │
│    3    │       'code_space'        │   430080   │     155616      │        0        │       172000        │
│    4    │        'map_space'        │   528384   │     308800      │        0        │       309640        │
│    5    │   'large_object_space'    │   135168   │     131112      │        0        │       135168        │
│    6    │ 'code_large_object_space' │     0      │        0        │        0        │          0          │
│    7    │ 'new_large_object_space'  │     0      │        0        │     1047456     │          0          │
└─────────┴───────────────────────────┴────────────┴─────────────────┴─────────────────┴──────────────────────

Note − To get a better understanding, let's execute the above code locally.

After executing the above code, the getHeapSpaceStatistics() method returns an array of 5 objects such as new space, old space, code space, map space, and large object space.

V8 Getheapspacestatistics
nodejs_v8_module.htm
Advertisements