Node.js – v8.getHeapStatistics() Method


The v8.getHeapStatistics() method returns the statistics about a heap. The getHeapStatistics() method is a part of the v8 module that returns the details about the heap and size of the v8. The getHeapSpaceStatistics() gives stats based upon the spaces in a system, whereas the getHeapStatistics() method gives stats about the whole system.

Syntax

v8.getHeapStatistics()

Parameters

Since it returns the space details/statistics about V8, it does not require any special input parameters. However, the value returned contains the following properties −

  • total_heap_size − This field will return the total heap space size.

  • total_heap_size_executable − This field will return the total heap size that is available for execution.

  • total_physical_size − This field will return the total physical size available on the disk.

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

  • used_heap_size − This is the heap size that is used.

  • heap_size_limit − Limit of the heap size for a user/application.

  • malloced_memory − Memory that is allocated to the application.

  • peak_malloced_memory − 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 thatare currently active. 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 garbage collector. If this number is not zero, then it indicates a potential memory-leak.

Example

Create a file "heapStats.js" and copy the following code snippet. After creating the file, use the command "node heapStats.js" to run this code.

 Live Demo

// v8.getHeapStatistics() Demo Example

// Importing the v8 module
const v8 = require('v8');

// Calling v8.getHeapStatistics() method
console.log(v8.getHeapStatistics());

Output

C:\home
ode>> node heapStats.js {    total_heap_size: 4468736,    total_heap_size_executable: 524288,    total_physical_size: 4468736,    total_available_size: 2194547400,    used_heap_size: 2853432,    heap_size_limit: 2197815296,    malloced_memory: 8192,    peak_malloced_memory: 123168,    does_zap_garbage: 0,    number_of_native_contexts: 1,    number_of_detached_contexts: 0 }

Updated on: 18-Aug-2021

676 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements