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

本文介绍如何在Node.js应用中引入并检测内存泄漏。通过使用'gc-profiler'和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.

转载于:https://www.cnblogs.com/Answer1215/p/6371559.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值