在我的Node.js应用程序中查找内存泄漏

时间:2019-04-17 06:38:47

标签: node.js express memory-leaks node-inspector

监视我的node.js应用程序显示我对RAM的消耗越来越大。

因此,我开始研究代码并进行多次测试,但我不知道如何纠正这些泄漏以及泄漏的确切来源。

要测试并查看内存泄漏,我正在使用 node --expose-gc --inspect index.js

在我的index.js中:

setInterval(() => {
  // 1. Force garbage collection every time this function is called
  try {
    global.gc();
  } catch (e) {
    console.log("You must run program with 'node --expose-gc index.js' or 'npm start'");
    process.exit();
  }
  const { rss, heapUsed, heapTotal } = process.memoryUsage();
  console.log(
    'rss',
    numeral(rss).format('0.0 ib'),
    'heapUsed',
    numeral(heapUsed).format('0.0 ib'),
    'heapTotal',
    numeral(heapTotal).format('0.0 ib'),
  );
}, 5000);

在我的应用程序上执行任何操作后,我可以清楚地看到堆在增长 terminal heap screenshot

我使用节点检查器拍摄了多个屏幕截图并进行了比较,但是没有看到与自己的代码(memory leak which seems to come from moment.js (I don't have anything called "guesses" in my code))相关的任何内容。就像我的所有泄漏都来自依赖项一样(我做了yarn upgrade --latest

如果有人可以查看我的3个堆快照以帮助我找出内存泄漏的来源,那将对我有很大帮助

0 个答案:

没有答案