process.memoryUsage()


    返回描述 Node.js 进程的内存使用量(以字节为单位)的对象。

      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
      // }
      • heapTotal and heapUsed refer to V8's memory usage.
      • external refers 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.
      • arrayBuffers refers to memory allocated for ArrayBuffers and SharedArrayBuffers, including all Node.js Buffers. This is also included in the external value. When Node.js is used as an embedded library, this value may be 0 because allocations for ArrayBuffers 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.