Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

I have the below code:

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);
        });

when I run the code I get below error:

(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)
like image 558
Programmer Avatar asked Dec 21 '12 01:12

Programmer


People also ask

What is event emitter memory leak?

The warning possible EventEmitter memory leak detected happens when you have more than 10 listeners (read EventEmitter) attached to an event. First of all, try to understand how this can be the case. Most of the times it's a developer mistake that can be solved easily by cleaning up the code.

How do I add a listener to EventEmitter?

on(event, listener) and eventEmitter. addListener(event, listener) are pretty much similar. It adds the listener at the end of the listener's array for the specified event. Multiple calls to the same event and listener will add the listener multiple times and correspondingly fire multiple times.

What is NodeJS EventEmitter?

The EventEmitter is a module that facilitates communication/interaction between objects in Node. EventEmitter is at the core of Node asynchronous event-driven architecture. Many of Node's built-in modules inherit from EventEmitter including prominent frameworks like Express.

What happens if an error event is emitted through an EventEmitter and nothing listens to it?

Any listeners for the error event should have a callback with one argument to capture the Error object and gracefully handle it. If an EventEmitter emits an error event, but there are no listeners subscribed for error events, the Node. js program would throw the Error that was emitted.


1 Answers

I believe the problem is that you're not removing the listeners when you don't need them anymore. You need to use 'schild.removeListener('exit', function)' or 'schild.removeAllListeners('exit')' when you're done with them.

See: http://nodejs.org/api/events.html#events_emitter_removelistener_event_listener

Of course there are scenarios where you need to have more than 10 listeners in which case you should use 'schild.setMaxListeners(0)' (0 means unlimited)

See: http://nodejs.org/api/events.html#events_emitter_setmaxlisteners_n

Hope it helps!

like image 161
LuisCien Avatar answered Oct 06 '22 02:10

LuisCien