Node.js:严重的内存泄漏更新版本中的错误?
有什么问题?
首先,我发现node.js模块或代码有问题,因为当我访问我的页面时,每次访问后内存都减less了,并且没有被释放回来。 经过几个小时的debugging,我找不到任何问题,所以我尝试了默认的node.js服务器示例,以查看问题是在我的代码还是在node.js本身。
如何重复该问题:
所以我创build了这样的服务器:
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保留它。
那么这里有什么问题?
环境
我正在testingubuntu 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 ,在那个时候应该减less内存使用量。
为了确保GC进程正常工作,我build议你使用--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');