Skip to content
Related Articles

Related Articles

Node.js v8.getHeapStatistics() Method

Improve Article
Save Article
  • Last Updated : 01 Nov, 2022
Improve Article
Save Article

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

Syntax:

v8.getHeapStatistics();

Parameters: This method does not have any parameters.

Return Value: This method returns an object that contains statistics about version 8 heap. The returned object usually contains an array, that consists of following fields:

  • total_heap_size: A number, signifies total heap size.
  • total_heap_size_executable: A number, signifies total executable heap size.
  • total_physical_size: A number, signifies total physical size.
  • total_available_size: A number, signifies the total available size.
  • used_heap_size: A number, signifies used heap size.
  • heap_size_limit: A number, signifies heap size limit.
  • malloced_memory: A number, signifies malloced memory.
  • peak_malloced_memory: A number, signifies maximum malloced memory.
  • does_zap_garbage: A number, specifically a boolean, signifies whether the –zap_code_space option is enabled or not.
  • number_of_native_contexts: A number, signifies a number of native contexts or the top-level contexts currently active. Memory leakage might be indicated by measuring the increment of this number over time.
  • number_of_detached_contexts: A number, signifies a number of detached contexts or contexts that were detached and not yet garbage collected. Memory leakage might be indicated if it has non zero value.

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

Example 1: Filename: index.js 

javascript




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


Run index.js file using the following command:

node index.js

Output:

{ total_heap_size: 6537216,
  total_heap_size_executable: 1048576,
  total_physical_size: 6537216,
  total_available_size: 1520717240,
  used_heap_size: 4199600,
  heap_size_limit: 1526909922,
  malloced_memory: 8192,
  peak_malloced_memory: 406408,
  does_zap_garbage: 0 }

Example 2: Filename: index.js 

javascript




// Accessing v8 module
const v8 = require('v8');
 
// Calling v8.getHeapStatistics()
stats = v8.getHeapStatistics();
console.log("Heap Statistics are :");
 
console.log("total_heap_size:"+stats['total_heap_size']);
console.log("used_heap_size:"+stats['used_heap_size']);
console.log("heap_size_limit:"+stats['heap_size_limit']);
console.log("does_zap_garbage:"+stats['does_zap_garbage']);


Run index.js file using the following command:

node index.js

Output:

Heap Statistics are :
total_heap_size:6537216
used_heap_size:4200640
heap_size_limit:1526909922
does_zap_garbage:0

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!