【问题标题】:NodeJS : warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limitNodeJS:警告:检测到可能的 EventEmitter 内存泄漏。增加了 11 位听众。使用emitter.setMaxListeners() 增加限制
【发布时间】:2012-12-08 14:59:08
【问题描述】:

我有以下代码:

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)

【问题讨论】:

标签: node.js


【解决方案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

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2012-09-04
    • 2014-01-03
    • 1970-01-01
    • 2016-06-12
    • 2016-10-08
    • 2018-11-15
    • 2016-05-05
    • 2013-03-12
    相关资源
    最近更新 更多