码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-02-06 21:00:49      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:profiler   using   oar   send   increase   tor   push   bsp   error   

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

标签:profiler   using   oar   send   increase   tor   push   bsp   error   

原文地址:http://www.cnblogs.com/Answer1215/p/6371559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!