• Node.js Video Tutorials

NodeJS - v8.getHeapStatistics() Method



The NodeJS v8.getHeapStatistics() method is used to retrieve the statistics about the heap that is derived from the v8 version. This method returns statistics about the heap such as total heap size, used heap size, heap size limit, total available size, etc.

The getHeapSpaceStatistics() returns the statistics based on the spaces in a system, whereas the getHeapStatistics() method retrieves the statistics for the whole system.

Syntax

Following is the syntax of the NodeJS v8.getHeapStatistics() method

v8.getHeapStatistics()

Parameters

This method does not accept any parameters.

Return Value

This method returns an object that contains statistics about the heap that is derived from the v8.

Following are the properties that contain in the returned object.

  • total_heap_size − This property specifies the total heap space size.

  • total_heap_size_executable − This property specifies the total heap size that is available for execution.

  • total_physical_size − This property specifies the total physical size available on the disk.

  • total_available_size − This property specifies the total size that is available to the system.

  • used_heap_size − This property specifies the heap size that is used

  • heap_size_limit − This property specifies the limit of the heap size for a user/application.

  • malloced_memory − This property specifies the memory that is allocated to the application.

  • peak_malloced_memory − This property specifies the max limit of the memory that is available for the application.

  • does_zap_garbage − This is a Boolean value 0/1 that tells the system if -- zap_code_space option is enabled or not.

  • number_of_native_contexts − This is the number of top-level contexts that are currently active. An increase in this number tells that there might be a memory leak.

  • number_of_detached_contexts − These are the number of contexts that are detached but not yet collected by the garbage collector. If this number is not zero, then it indicates a potential memory leak.

Example

In the following example, we are trying to get all the statistics of the heap that is derived from v8 using NodeJS getHeapStatistics() method.

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

Output

{
   total_heap_size: 5369856,
   total_heap_size_executable: 524288,
   total_physical_size: 4298984,
   total_available_size: 17226372488,
   used_heap_size: 2855168,
   heap_size_limit: 17230200832,
   malloced_memory: 8192,
   peak_malloced_memory: 418904,
   does_zap_garbage: 0,
   number_of_native_contexts: 1,
   number_of_detached_contexts: 0
}

Example

In the example below, we are trying to get the statistics of the v8 heap such as total heap size, used heap size, and heap size limit.

const v8 = require('v8');
let statistics = v8.getHeapStatistics();
console.log("total_heap_size: " + statistics['total_heap_size']);
console.log("used_heap_size: " + statistics['used_heap_size']);
console.log("heap_size_limit: " + statistics['heap_size_limit']);

Output

total_heap_size: 6086656
used_heap_size: 3769624
heap_size_limit: 17213423616
does_zap_garbage: 0
nodejs_v8_module.htm
Advertisements