How to avoid memory leaks?

1,021 views
Skip to first unread message

Felix E. Klee

unread,
Nov 15, 2012, 4:32:53 PM11/15/12
to sock...@googlegroups.com
Excerpt from my app's server code:

io.sockets.on('connection', function (socket) {
boards.listen(socket);
});

`boards.listen(socket)` then sets up listeners for events received via
`socket`.

In a nutshell: Every time a client (browser) connects, a new socket is
registered, and new event listeners are set up. This seems to a
typical setup for a Socket.IO application.

Now the problem: The event listeners persist, even when a client
(browser) disconnects.

So: How to avoid memory leaks? Or am I missing something?

Duy Nguyen

unread,
Nov 15, 2012, 9:27:22 PM11/15/12
to sock...@googlegroups.com
Assume you have: var sio = io.connect(host, port);

You should clear all listeners when disconnecting:
var _self = this;
sio.socket.on('disconnect', function() {
  if (this.sio.$events) {
       this.sio.$events = {};
    }
});
--
Nguyen Hai Duy
Mobile : 0914 72 1900
Yahoo: nguyenhd_lucky

Chad Engler

unread,
Nov 16, 2012, 9:37:37 AM11/16/12
to sock...@googlegroups.com
Remove your listeners when they disconnect.

-Chad

Felix E. Klee

unread,
Nov 18, 2012, 5:27:52 AM11/18/12
to sock...@googlegroups.com
On Fri, Nov 16, 2012 at 3:37 PM, Chad Engler <Chad....@patlive.com>
wrote:
> Remove your listeners when they disconnect.

Er, yes! But somehow it didn't occur to me to simply do sth. like:

socket.once('disconnect', function () {
// clean up and remove listeners
}

Instead I tried listening to `disconnect` as emitted by `io.sockets`,
but that doesn't make much sense.

One thing to be aware of (as a note to myself): A client may disconnect
because the network connection is interrupted. If the network connection
comes back on, the client will connect again, and then server-side event
listeners have to be set up again.

By the way, I am wondering whether I have to manually take care of
removing the listeners to events emitted by `socket`
(set up with `socket.on(listener)`). Clarification would be helpful.

Chad Engler

unread,
Nov 19, 2012, 8:38:08 AM11/19/12
to sock...@googlegroups.com
Your `socket.on` listeners should only be setup once when a socket
connects, and later when the socket is destroyed internally I think
socket.io will clean those up.

You just need to worry about your own listeners.

-Chad

-----Original Message-----
From: sock...@googlegroups.com [mailto:sock...@googlegroups.com] On
Behalf Of Felix E. Klee

Felix E. Klee

unread,
Nov 20, 2012, 5:34:08 PM11/20/12
to sock...@googlegroups.com
> Your `socket.on` listeners should only be setup once when a socket
> connects, and later when the socket is destroyed internally I think
> socket.io will clean those up.

I had a brief look at the Socket.IO code the other day and didn't find
cleanup of `socket.on` listeners. And, in fact I don't think anymore
that such a cleanup is necessary. If a `socket.on` listener function is
not referenced externally, then there is no reason why it shouldn't get
garbage collected when the socket is garbage collected.

> You just need to worry about your own listeners.

Absolutely: On every socket connect, I add an event listener on a
database connection (`redisClient.on`). If I don't remove that listener
on socket disconnect, then there is a leak.

Background: My `redisClient.on` listener takes care of pushing data via
Socket.IO to the client (browser) every time the server reconnects to
the database (e.g. after an outage).

Chad Engler

unread,
Nov 26, 2012, 8:51:18 AM11/26/12
to sock...@googlegroups.com
That is what I meant, once the socket is gone; those events will be
cleaned up by the gc. I didn't mean the library itself removes them or
anything like that.

-Chad

-----Original Message-----
From: sock...@googlegroups.com [mailto:sock...@googlegroups.com] On
Behalf Of Felix E. Klee
Sent: Tuesday, November 20, 2012 5:34 PM
To: sock...@googlegroups.com
Subject: Re: How to avoid memory leaks?

Reply all
Reply to author
Forward
0 new messages