M
process.memoryUsage
History
Added in: v0.1.16
v0.1.16
Introduced in: v0.10.0
v0.10.0
Added
arrayBuffers to the returned object.v13.9.0, v12.17.0
Added
external to the returned object.v7.2.0
process.memoryUsage(): Object
Returns an object describing the memory usage of the Node.js process measured in bytes.
import { memoryUsage } from 'node:process'; console.log(memoryUsage()); // Prints: // { // rss: 4935680, // heapTotal: 1826816, // heapUsed: 650472, // external: 49879, // arrayBuffers: 9386 // }
const { memoryUsage } = require('node:process'); console.log(memoryUsage()); // Prints: // { // rss: 4935680, // heapTotal: 1826816, // heapUsed: 650472, // external: 49879, // arrayBuffers: 9386 // }
heapTotalandheapUsedrefer to V8's memory usage.externalrefers to the memory usage of C++ objects bound to JavaScript objects managed by V8.rss, Resident Set Size, is the amount of space occupied in the main memory device (that is a subset of the total allocated memory) for the process, including all C++ and JavaScript objects and code.arrayBuffersrefers to memory allocated forArrayBuffers andSharedArrayBuffers, including all Node.jsBuffers. This is also included in theexternalvalue. When Node.js is used as an embedded library, this value may be0because allocations forArrayBuffers may not be tracked in that case.
When using Worker threads, rss will be a value that is valid for the
entire process, while the other fields will only refer to the current thread.
The process.memoryUsage() method iterates over each page to gather
information about memory usage which might be slow depending on the
program memory allocations.
On Linux or other systems where glibc is commonly used, an application may have sustained
rss growth despite stable heapTotal due to fragmentation caused by the glibc malloc
implementation. See nodejs/node#21973 on how to switch to an alternative malloc
implementation to address the performance issue.