Node.js Reference ยท path, os and url
Node.js os.totalmem() and os.freemem() methods
Reference for os.totalmem() and os.freemem() in Node.js: system memory in bytes, how to format it, and how it differs from process memory.
os.totalmem() returns the total amount of system memory in bytes, and os.freemem() returns the amount the operating system currently considers free. Both return plain numbers and take no arguments.
These are machine-wide figures. They say nothing about how much memory your own Node.js process uses; for that see process.memoryUsage(). For "how much could a program still get", process.availableMemory() is often more useful than "free".
import os from "node:os"; console.log(os.totalmem(), os.freemem());
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
os.totalmem(): number os.freemem(): number
| Name | Description |
|---|---|
| (none) | Neither function takes arguments. |
Returns: a number of bytes. totalmem() is fixed for a running machine, while freemem() changes from call to call.
Edge cases: inside containers these functions can report the host's memory, not the container's limit. Free memory on Linux is often small because the kernel uses spare RAM as a file cache, so a low freemem() does not mean trouble.
Usage notes
- All values are bytes. Convert for display; do not show raw numbers to users.
- "Free" is not "available".
process.availableMemory()counts memory that could be handed to programs, so it is better for deciding whether to allocate a large buffer. - Values for an own-process view come from
process.memoryUsage(),process.constrainedMemory()andv8.getHeapStatistics(). - Do not assert exact values in tests. They change constantly.
- Large machines: the result is a regular number, which is exact up to about 9 petabytes, so there is no need for BigInt.