Detecting memory leaks in nodejs

后端 未结 7 1472
情话喂你
情话喂你 2020-12-23 17:14

Are there some tools for detecting memory leaks in nodejs? And tell me about your experience in testing nodejs applications.

相关标签:
7条回答
  • 2020-12-23 17:41

    I have used the npm package Memwatch:

    Take a look to the Github repository and the NPM source

    Basically this package checks the memory heap usage right after a garbage collection is performed by the V8 engine, and give you a baseline of the actual memory usage.

    Here's how I used it:

    var memwatch = require('memwatch');
    
    memwatch.on('leak', function(info) {
        console.log('Memwatch leak: ');
        console.log(info);
    });
    
    memwatch.on('stats', function(stats) {
        console.log.message('Memwatch stats: ');
        console.log(stats);
    });
    

    From the original documentation:

    The 'stats' event, emitted occasionally, give you the data describing your heap usage and trends over time.

    The 'leak' event, is emitted when it appears your code is leaking memory. It is usually executed when the heap size continously grows in a short period of time.

    Memwatch also provides a "HeapDiff" class to compute the heap state between two snapshots you can take in your functions.

    It might be a good idea to have memwatch running in your stage environment, in order to track events causing the issue.

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