Node.js Reference ยท process
Node.js process.memoryUsage() method
Reference for process.memoryUsage() in Node.js: what rss, heapTotal, heapUsed, external and arrayBuffers mean, the faster rss() variant, and how to measure growth.
process.memoryUsage() returns an object that describes how much memory the current Node.js process is using, measured in bytes. It is the quickest way to check whether a script, a cache or a request handler is leaking memory.
The numbers come from two places: the operating system (how much memory the process occupies) and the V8 engine (how big the JavaScript heap is and how much of it is in use). Because garbage collection runs at times you do not control, treat single readings as rough and compare trends instead.
const { heapUsed, rss } = process.memoryUsage(); console.log(heapUsed < rss);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
process.memoryUsage() process.memoryUsage.rss()
| Name | Description |
|---|---|
| (none) | Neither form takes parameters. |
Returns: memoryUsage() returns an object with these integer fields (bytes):
| Field | Meaning |
|---|---|
rss |
Resident set size: all memory the process holds in RAM (code, stack, heap, buffers). |
heapTotal |
Memory V8 has reserved for the JavaScript heap. |
heapUsed |
Part of the heap that is occupied by live and not-yet-collected objects. |
external |
Memory used by C++ objects bound to JavaScript objects, including Buffers. |
arrayBuffers |
Memory used by ArrayBuffer and SharedArrayBuffer, including Node Buffers. It is already counted inside external. |
process.memoryUsage.rss() returns only the rss number as a plain integer.
Errors / edge cases: the call does not throw in normal use. If the operating system cannot supply the figure it may report an unusual value, so never divide by one of these fields without checking it.
Usage notes
- Use
rss()for frequent sampling. The fullmemoryUsage()call has to ask V8 for heap statistics, which is comparatively slow.process.memoryUsage.rss()only asks the operating system, so it is the better choice in a timer that fires every second. rsscan be smaller than you expect, or larger. Part of the heap may be swapped out, and shared libraries are counted too. Compare readings from the same machine only.- Buffers live outside the heap. A 100 MB
Bufferbarely movesheapUsed; look atexternalandarrayBuffersinstead. - Numbers are not stable. Garbage collection can drop
heapUsedat any moment. To judge a leak, take readings after the same amount of work, several times, and see whether the low points keep rising. - Convert before printing. Divide by
1024 * 1024for mebibytes. Printing raw bytes is hard to read. process.resourceUsage()hasmaxRSS. It reports the peak resident size in kilobytes. See process.cpuUsage() and resourceUsage().- Worker threads have their own heaps.
memoryUsage()in the main thread does not include the heaps of workers;rssdoes include their memory because it is process-wide. - Do not print it in production loops. Send the figures to your metrics system as numbers instead of logging them as text.