有什么问题?
首先,我认为我的 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.3
和 node 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
参数运行 node 并检查像这样的内存使用情况:
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');
关于javascript - Node.js:新版本中存在严重的内存泄漏错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134010/