Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs setMaxListeners to avoid memory leak detection

I read some other questions and posts, but I couldn't find where to apply .setMaxListeners(0). I'm using a simple websocket-server which comes up with the errer:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at Socket.EventEmitter.addListener (events.js:160:15)
at Socket.Readable.on (_stream_readable.js:689:33)
at XHRPolling.Transport.setHandlers            (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:116:15)
at XHRPolling.HTTPPolling.setHandlers        (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:53:39)
at XHRPolling.Transport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:70:10)
at XHRPolling.HTTPTransport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:84:39)
at XHRPolling.HTTPPolling.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:73:41)
at XHRPolling.Transport (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:31:8)
at XHRPolling.HTTPTransport (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:29:13)
at XHRPolling.HTTPPolling (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:27:17)

I'm instantiating the router this way:

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app, { log: false })
, fs = require('fs');
app.listen(8070);

Then I'm listening for incomming socket-requests:

io.sockets.on('connection', function (socket) {

       socket.on('createTake', function(data){...}
});

Where can I apply the MaxListeners?

I already tried this:

io.sockets.on(...).setMaxListeners(0);

But it doesn't work.

like image 415
marcel Avatar asked Nov 19 '13 08:11

marcel


People also ask

How do you avoid memory leaks in node js?

Avoid Accidental Globals This could be the result of a typo and could lead to a memory leak. Another way could be when assigning a variable to this within a function in the global scope. To avoid issues like this, always write JavaScript in strict mode using the 'use strict'; annotation at the top of your JS file.

How do you detect node memory leaks?

Finding the leak. Chrome DevTools is a great tool that can be used to diagnose memory leaks in Node. js applications via remote debugging. Other tools exist and they will give you the similar.

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.

What is EventEmitter3 in JavaScript?

EventEmitter3 is a high performance EventEmitter. It has been micro-optimized for various of code paths making this, one of, if not the fastest EventEmitter available for Node. js and browsers. The module is API compatible with the EventEmitter that ships by default with Node.


1 Answers

You are attaching listeners to io.sockets (io.sockets.on(...)). And you should apply setMaxListeners to the same object (to EventEmmiter). So try:

io.sockets.setMaxListeners(0);
like image 125
tomekK Avatar answered Sep 20 '22 19:09

tomekK