我得到以下警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:385:29)
    at Server.<anonymous> (server.js:20:17)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)
    at TCP.onread (net.js:354:27)

我在server.js中写了这样的代码:

http.createServer(
    function (req, res) { ... }).listen(3013);

如何解决这个问题?


当前回答

正如其他人所指出的那样,提高限额并不是最好的答案。我也面临着同样的问题,但在我的代码中,我没有使用任何事件侦听器。当我仔细研究代码时,我有时会创建很多承诺。每个承诺都有一些抓取所提供URL的代码(使用一些第三方库)。如果你正在做类似的事情,那么它可能是原因。

在使用ES6的Promise.all()时,限制并发性的最佳方法是什么?

其他回答

将.on()替换为once()。使用once()在同一函数处理事件时删除事件监听器。

如果这不能解决问题,那么在package.json中重新安装restler :“restler git: / / github.com/danwrong/restler.git # 9 d455ff14c57ddbe263dbbcd0289d76413bfe07d”

这与restler 0.10对node的错误行为有关。你可以在这里看到git关闭的问题:https://github.com/danwrong/restler/issues/112 然而,npm还没有更新这个,所以这就是为什么你必须引用git头。

我想在这里指出,这个警告是有原因的,正确的解决方法很可能不是增加限制,而是弄清楚为什么要向同一个事件添加如此多的侦听器。只有当您知道为什么要添加这么多侦听器并且确信这是您真正想要的时候,才增加限制。

我找到了这个页面,因为我得到了这个警告,在我的情况下,我正在使用的一些代码中有一个错误,将全局对象转换为EventEmitter!我当然不建议在全球范围内增加限制,因为你不想让这些事情被忽视。

有时,这些警告发生在我们没有做过的事情,而是我们忘记做的事情!

当我用npm安装dotenv包时遇到了这个警告,但在我在应用程序开始添加require('dotenv').load()语句之前就被打断了。当我回到项目时,我开始得到“可能的EventEmitter内存泄漏检测到”警告。

我以为问题出在我做了什么,而不是我没做什么!

一旦我发现了我的疏忽并添加了require语句,内存泄漏警告就清除了。

You said you are using process.on('uncaughtException', callback); Where are you executing this statement? Is it within the callback passed to http.createServer?If yes, different copy of the same callback will get attached to the uncaughtException event upon each new request, because the function (req, res) { ... } gets executed everytime a new request comes in and so will the statement process.on('uncaughtException', callback);Note that the process object is global to all your requests and adding listeners to its event everytime a new request comes in will not make any sense. You might not want such kind of behaviour. In case you want to attach a new listener for each new request, you should remove all previous listeners attached to the event as they no longer would be required using: process.removeAllListeners('uncaughtException');

我们团队对此的解决方案是从.npmrc中删除注册表路径。rc文件中有两个路径别名,其中一个指向已弃用的Artifactory实例。

这个错误与我们的应用程序的实际代码无关,而是与我们的开发环境有关。