Node.js:新版本中存在严重的内存泄漏错误

Node.js: Serious Memory Leak Bug in newer versions?

本文关键字:内存 泄漏 错误 存在 js 新版本 Node      更新时间:2023-09-26

有什么问题?

首先,我认为我的node.js模块或代码有问题,因为当我访问我的页面时,每次访问后内存减少,并且没有释放回来。经过几个小时的调试,我找不到任何问题,所以我尝试了默认的node.js服务器示例,看看问题是在我的代码中还是在node.js本身。

如何重复问题:

所以我创建了这样的服务器:
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World'n');
}).listen(80);
console.log('Server running at port 80');

我访问mydomain.com并多次点击刷新,空闲内存一直在下降,即使在我释放刷新后,内存仍然保持在同一水平,所以node.js保留了它。

那么这里有什么问题呢?

我正在测试ubuntu 12, max os x 10.8.3node v0.9.0, node v0.10.0, v0.10.2, v0.10.4, v0.11.1问题存在的地方,以及node v0.8.21正常工作的地方,这就是为什么我说这可能是新版本中的错误。

V8将在需要时调用GC,此时内存使用量应该会减少。

为了确保GC进程工作正常,我建议您使用--expose-gc参数运行节点,并使用如下命令检查内存使用情况:

var http = require('http'),
    util = require('util');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World'n');
    global.gc();
    console.log('Memory Usage:');
    console.log(util.inspect(process.memoryUsage()));
}).listen(8080); // changed the port to 8080 because I didn't want to run the server as root
console.log('Server running at port 8080');