2012-12-21 79 views
7

我有下面的代碼:NodeJS:警告:可能檢測到EventEmitter內存泄漏。添加了11位聽衆。使用emitter.setMaxListeners()來增加限制

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) 
+1

這只是一個警告,不是一個錯誤。請參閱:http://stackoverflow.com/questions/9768444/possible-eventemitter-memory-leak-detected –

+0

可能重複的[可能EventEmitter內存泄漏檢測](https://stackoverflow.com/questions/9768444/possible-eventemitter -Memory泄漏檢測的) – OrangeDog

回答

4

我相信問題是,你當你不再需要他們時,不要刪除聽衆。當你完成它們時,你需要使用'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

希望它有幫助!

相關問題