Open In App

Node.js process.memoryUsage() Method

Last Updated : 20 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The process.memoryUsage() method is an inbuilt method of the process module that provides information about the current processes or runtime of a Node.js program. The memory usage method returns an object describing the memory usage in bytes of the Node.js process.

Syntax:

process.memoryUsage()   

Parameters: This method does not accept any parameter:

Return Value: This method returns an object with the description of the memory usage. 

Below examples illustrate the use of process.memoryUsage() method in Node.js.

Example 1:

index.js




// Requiring module
var process = require('process')
  
// Prints the output as an object
console.log(process.memoryUsage())


Run the index.js file using the following command:

node index.js

Output:

{
  rss: 23851008,     
  heapTotal: 4907008,
  heapUsed: 2905912, 
  external: 951886,  
  arrayBuffers: 17574
}

Example 2: 

index.js




// Requiring module
var process = require('process')
  
// An example displaying the respective memory
// usages in megabytes(MB)
for (const [key,value] of Object.entries(process.memoryUsage())){
    console.log(`Memory usage by ${key}, ${value/1000000}MB `)
}


Run the index.js file using the following command:

node index.js

Output:

Memory usage by rss, 23.87968MB 
Memory usage by heapTotal, 4.907008MB
Memory usage by heapUsed, 2.907088MB
Memory usage by external, 0.951886MB
Memory usage by arrayBuffers, 0.017574MB

Reference: https://nodejs.org/api/process.html#process_process_memoryusage


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads