zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    游记&退役记
    Nothing to say
    学习知识点的比较好的blog
    计划做题列表
    后缀自动机小专题
    复数
    FFT学习
    P2900 [USACO08MAR]土地征用Land Acquisition
    # 数位DP入坑
    Hdu 4035 Maze(概率DP)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6371559.html
Copyright © 2011-2022 走看看