【问题标题】:Node.js Request memory leakingNode.js 请求内存泄漏
【发布时间】:2013-08-25 09:22:37
【问题描述】:

给定以下代码sn-p:

var request = require('request');
function getGoogle() {

  request('http://www.google.com', function (error, response, body) {
      if (!error && response.statusCode == 200) {
      //console.log(body) // Print the google web page.
      }
  });
}
setInterval(getGoogle, 1000);

使用 Node 版本 0.10.17,此代码泄漏。任何想法为什么?

【问题讨论】:

  • 您认为应用程序泄漏的原因是什么?
  • 分配的内存大小一直在增长......或者你是什么意思?
  • 因为 request() 函数是异步的,所以稍后会在匿名函数被调用时释放内存。如果应用程序运行一段时间并且内存保持在某个稳定范围内。这并不意味着存在内存泄漏。

标签: node.js memory-leaks


【解决方案1】:

我看到很多问题都是“哎呀我的内存泄漏!”基于应用程序运行的前几分钟,无需等待进程是否达到上限或实际内存不足。 Node 只使用尽可能多的内存。

请参阅以下粗略基准。我运行了您的代码(改为超时 100),内存使用量迅速增加到约 70 MB,但随后停止了。第一列是内存使用情况。

27368  0:00.36 node test.js
40644  0:00.82 node test.js
47468  0:01.21 node test.js
48192  0:01.40 node test.js
67952  0:02.39 node test.js
69448  0:03.29 node test.js # Increasing fast til around here
70016  0:04.46 node test.js
70624  0:07.43 node test.js
70944  0:10.59 node test.js
71612  0:13.63 node test.js
73120  0:16.83 node test.js
70864  0:18.17 node test.js # Look it's decreasing
67780  0:42.27 node test.js # Considerably later it's even lower!

我猜测为什么会这样是因为垃圾收集很昂贵,但我不确定,如果有人有真正的解释和参考,我会很高兴。

【讨论】:

  • 听起来不错...我尝试使用 global.gc() 强制 GC,这样内存使用量根本不会增长.. 看来,我只需要耐心等待 GC发生。
【解决方案2】:

在这个页面 (http://www.joyent.com/blog/walmart-node-js-memory-leak) 他们实际上发现了 Node.JS 的 http 堆栈中的内存泄漏

好消息是它已在 Node.JS 版本 v0.10.22 中修复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2016-01-01
    • 2013-08-05
    • 2014-01-20
    相关资源
    最近更新 更多