Nodejs v8 getHeapSpaceStatistics method | Heap space statistics in node

In this tutorial, You learn v8.getHeapSpaceStatistics() method in v8 module of nodejs with examples getHeapSpaceStatistics method returns statistics about heap sizes based on spaces. v8 is an opensource javascript engine from google chrome and used by nodejs and Mongodb

What is a space in v8? Space is an chunk of memory allocated by v8 engine. V8 has a following different spaces for storing and garbage cleaning object in heap memory.

  • new space : this is small memory used for new objects for garbage collection of objects quickly.
  • old pointer space: store long live objects which point to other objects
  • old data space: these are object, plain raw data has no reference to another objects
  • code space : executable memory for compilation and JITed executions
  • Cell space, property-cell-space and map-space: Storing same size of cell property and map related objects.
  • large object space: storing large objects

getHeapSpaceStatistics method return above object space statistics.

Each object holds information about space available used and physical heap sizes information.

V8 is an engine is used internally by Nodejs environment. You can check v8 version and 32 bit or 64 bit for Nodejs

Sometimes, To debug node outofmemory errors , We need to know heapsize of an environment and application.

To get the all above information, Nodejs has a inbuilt in module v8 , It has a inbuilt getHeapSpaceStatistics to get statistics information about heap memory allocated based on spaces.

V8 has another method [getHeapStatistics](/nodejs-v8-getheapstatistics-method) about stats of heap memory in the entire system. Syntax:


It returns an object of following properties

  • space_name: Name of the space used, examples are read_only_space,new_space,old_space,code_space,map_space,large_object_space,code_large_object_space,new_large_object_space
  • space_size: Allocated space heap size in bytes
  • space_used_size: Used space heap size in bytes
  • space_available_size: Available space heap size in bytes
  • physical_space_size: Physical space heap size in bytes

This will give useful heap information

v8 getHeapSpaceStatistics method example.

first import v8 module using require keyword in ES5 syntax

const v8module = require('v8');

Here is an code for v8 getHeapSpaceStatistics example info.js

const v8module = require('v8');  

running info.js in command line with node command

node info.js

It outputs an object

    space_name: 'read_only_space',
    space_size: 151552,
    space_used_size: 150392,
    space_available_size: 0,
    physical_space_size: 151552
    space_name: 'new_space',
    space_size: 2097152,
    space_used_size: 881912,
    space_available_size: 165512,
    physical_space_size: 2097152
    space_name: 'old_space',
    space_size: 1458176,
    space_used_size: 1349464,
    space_available_size: 248,
    physical_space_size: 1458176
    space_name: 'code_space',
    space_size: 360448,
    space_used_size: 87456,
    space_available_size: 0,
    physical_space_size: 360448
    space_name: 'map_space',
    space_size: 528384,
    space_used_size: 262152,
    space_available_size: 0,
    physical_space_size: 528384
    space_name: 'large_object_space',
    space_size: 135168,
    space_used_size: 131112,
    space_available_size: 0,
    physical_space_size: 135168
    space_name: 'code_large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 0,
    physical_space_size: 0
    space_name: 'new_large_object_space',
    space_size: 0,
    space_used_size: 0,
    space_available_size: 1047424,
    physical_space_size: 0
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.