NodeJS:警告:检测到可能的 EventEmitter 内存泄漏。添加了 11 位听众。使用 emitter.setMaxListeners() 增加限制

NodeJS : warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit

提问人:Programmer 提问时间:12/21/2012 更新时间:2/14/2013 访问量:20784

问:

我有以下代码:

var schild = spawn('script.sh', ["process1", "process2"]);
        schild.stderr.on('data', function (data) {
                        logger.info('stderr: ' + data);
        });

        schild.on('exit', function (code) {
          logger.info('child process exited with code ' + code);
        });

        schild.stdout.on('data', function (data) {
           logger.info('Data ' + data);
        });

当我运行代码时,出现以下错误:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:175:15)
    at EventEmitter.once (events.js:196:8)
    at Transport.logException (/x/home/prakash/src/node_modules/winston/lib/winston/transports/transport.js:118:8)
    at logAndWait (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:613:15)
    at async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:86:13)
    at Array.forEach (native)
    at _forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:26:24)
    at Object.async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:85:9)
    at Logger._uncaughtException (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:636:9)
    at process.EventEmitter.emit (events.js:126:20)
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:175:15)
    at EventEmitter.once (events.js:196:8)
    at Transport.logException (/x/home/prakash/src/node_modules/winston/lib/winston/transports/transport.js:117:8)
    at logAndWait (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:613:15)
    at async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:86:13)
    at Array.forEach (native)
    at _forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:26:24)
    at Object.async.forEach (/x/home/prakash/src/node_modules/winston/node_modules/async/lib/async.js:85:9)
    at Logger._uncaughtException (/x/home/prakash/src/node_modules/winston/lib/winston/logger.js:636:9)
    at process.EventEmitter.emit (events.js:126:20)
节点.js

评论

1赞 Amir T 12/24/2012
这只是一个警告,而不是一个错误。请参阅:stackoverflow.com/questions/9768444/...
0赞 OrangeDog 8/31/2017
检测到可能的 EventEmitter 内存泄漏的可能重复项

答:

5赞 LuisCien 2/14/2013 #1

我相信问题在于,当你不再需要它们时,你不会删除它们。完成操作后,您需要使用 'schild.removeListener('exit', function)' 或 'schild.removeAllListeners('exit')'

请参见:http://nodejs.org/api/events.html#events_emitter_removelistener_event_listener

当然,在某些情况下,您需要拥有 10 个以上的侦听器,在这种情况下,您应该使用 'schild.setMaxListeners(0)'(0 表示无限制)

请参见:http://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n

希望对您有所帮助!