[Node.js] Identify memory leaks with nodejs-dashboard

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Node.js] Identify memory leaks with nodejs-dashboard相关的知识,希望对你有一定的参考价值。

In this lesson, I introduce a memory leak into our node.js application and show you how to identify it using the Formidable nodejs-dashboard. Once identified, we will add garbage collection stats to the error console allowing us to correlate garbage collection with the increased memory usage.

 

Install:

npm i -D gc-profiler nodejs-dashboard

 

let express = require(express);
let router = express.Router();
let faker = require(faker);
let words = [];
let profiler = require(gc-profiler);

profiler.on(gc, (info) => {
    console.error(info);
});

router.get(/, function(req, res, next) {
    let num = Math.floor(Math.random() * 1000) + 1;
    let searchterm = faker.lorem.words(num);
    let arr = searchterm.split( );
    arr.forEach(word => {
        words.push(word); // cause memory leak
    });
    console.log(`Generating ${num} words`);
    res.send(searchterm);
});

module.exports = router;

In the code, we can use ‘gc-profiler‘ to monitor the gb collection in order to see the memory leak in dashboard.

以上是关于[Node.js] Identify memory leaks with nodejs-dashboard的主要内容,如果未能解决你的问题,请参考以下文章

错误:ENOENT:没有这样的文件或目录,uv_resident_set_memory

node.js的异步编程

如何定位 Node.js 的内存泄漏

如何使用 Node.js 检查 ram 的当前使用情况? (Discord.js 机器人)

WASM 和 Node.js 不能在模块外使用“import.meta”

identity 和 identify (我分不清)