Node.js v8.getHeapStatistics() Method - GeeksforGeeks

28 Jul, 2020
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
// Accessing v8 moduleconst 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
// Accessing v8 moduleconst v8 = require('v8');  // Calling v8.getHeapStatistics() stats = v8.getHeapStatistics();console.log("Heap Stastistics 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 Stastistics 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
Node.js-V8-Module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
Node.js fs.readFileSync() Method
Node.js fs.writeFile() Method
How to update NPM ?
Difference between promise and async await in Node.js
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript