How to monitor the memory usage of Node.js?

前端 未结 6 2180
攒了一身酷
攒了一身酷 2020-12-02 05:25

How can I monitor the memory usage of Node.js?

相关标签:
6条回答
  • 2020-12-02 06:12

    The built-in process module has a method memoryUsage that offers insight in the memory usage of the current Node.js process. Here is an example from in Node v0.12.2 on a 64-bit system:

    $ node --expose-gc
    > process.memoryUsage();  // Initial usage
    { rss: 19853312, heapTotal: 9751808, heapUsed: 4535648 }
    > gc();                   // Force a GC for the baseline.
    undefined
    > process.memoryUsage();  // Baseline memory usage.
    { rss: 22269952, heapTotal: 11803648, heapUsed: 4530208 }
    > var a = new Array(1e7); // Allocate memory for 10m items in an array
    undefined
    > process.memoryUsage();  // Memory after allocating so many items
    { rss: 102535168, heapTotal: 91823104, heapUsed: 85246576 }
    > a = null;               // Allow the array to be garbage-collected
    null
    > gc();                   // Force GC (requires node --expose-gc)
    undefined
    > process.memoryUsage();  // Memory usage after GC
    { rss: 23293952, heapTotal: 11803648, heapUsed: 4528072 }
    > process.memoryUsage();  // Memory usage after idling
    { rss: 23293952, heapTotal: 11803648, heapUsed: 4753376 }
    

    In this simple example, you can see that allocating an array of 10M elements consumers approximately 80MB (take a look at heapUsed).
    If you look at V8's source code (Array::New, Heap::AllocateRawFixedArray, FixedArray::SizeFor), then you'll see that the memory used by an array is a fixed value plus the length multiplied by the size of a pointer. The latter is 8 bytes on a 64-bit system, which confirms that observed memory difference of 8 x 10 = 80MB makes sense.

    0 讨论(0)
  • 2020-12-02 06:19

    You can use node.js memoryUsage

    const formatMemmoryUsage = (data: any) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`
    
    const memoryData = process.memoryUsage()
    
    const memmoryUsage: {
                rss: `${formatMemmoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
                heapTotal: `${formatMemmoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
                heapUsed: `${formatMemmoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
                external: `${formatMemmoryUsage(memoryData.external)} -> V8 external memory`,
            }
    
    console.log(memoryUsage)
    /*
    {
        "rss": "177.54 MB -> Resident Set Size - total memory allocated for the process execution",
        "heapTotal": "102.3 MB -> total size of the allocated heap",
        "heapUsed": "94.3 MB -> actual memory used during the execution",
        "external": "3.03 MB -> V8 external memory"
    }
    */
    
    0 讨论(0)
  • 2020-12-02 06:21

    Also, if you'd like to know global memory rather than node process':

    var os = require('os');
    
    os.freemem();
    os.totalmem();
    

    See documentation

    0 讨论(0)
  • 2020-12-02 06:22

    The original memwatch is essentially dead. Try memwatch-next instead, which seems to be working well on modern versions of Node.

    0 讨论(0)
  • 2020-12-02 06:25

    node-memwatch : detect and find memory leaks in Node.JS code. Check this tutorial Tracking Down Memory Leaks in Node.js

    0 讨论(0)
  • 2020-12-02 06:29

    On Linux/Unix (note: Mac OS is a Unix) use top and press M (Shift+M) to sort processes by memory usage.

    On Windows use the Task Manager.

    0 讨论(0)
提交回复
热议问题