Are event listeners weak references?

1,381 views
Skip to first unread message

Tom

unread,
Jul 4, 2010, 2:06:36 PM7/4/10
to nod...@googlegroups.com
There's not too much info about event listeners and the garbage collector in the node.js documentation, which is why I want to ask if event listeners are by default weak references. This is considered bad practice by some people, I believe.

Basically, when do I need to remove event listeners and when do I not have to remove them?

Regards,
Tom

Rick Olson

unread,
Jul 6, 2010, 1:18:06 PM7/6/10
to nod...@googlegroups.com
I don't think so. It just adds the function to an _events hash on the
EventEmitter:

http://github.com/ry/node/blob/master/lib/events.js#L66-69

> --
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en.
>

--
Rick Olson
http://github.com

Theturtle32

unread,
Jul 7, 2010, 5:16:24 AM7/7/10
to nodejs
There's no such thing as a weak reference in JavaScript.

If the event emitter is a very long-lived object, and you keep adding
new listeners to it, they would accumulate over time.

But when an event emitter is garbage collected, the listener functions
would also be garbage collected as long as there wasn't anything else
referencing them, as far as I'm aware. I believe that would normally
be the case with an anonymous function. Just don't hold another
reference to those functions and you should be fine.

Brian

Tom

unread,
Jul 7, 2010, 7:39:42 AM7/7/10
to nod...@googlegroups.com
"But when an event emitter is garbage collected, the listener functions
would also be garbage collected as long as there wasn't anything else
referencing them, as far as I'm aware."

Could anyone confirm this? It is surprising howmuch doubt there is about this subject, since (almost) any project with Node has to use these event listeners. Not having them garbage collected would result in a memory leak.

- Tom

2010/7/7 Theturtle32 <thetu...@gmail.com>

r...@tinyclouds.org

unread,
Jul 7, 2010, 9:11:09 AM7/7/10
to nod...@googlegroups.com
On Wed, Jul 7, 2010 at 1:39 PM, Tom <tomm...@gmail.com> wrote:
> "But when an event emitter is garbage collected, the listener functions
> would also be garbage collected as long as there wasn't anything else
> referencing them, as far as I'm aware."
> Could anyone confirm this? It is surprising howmuch doubt there is about
> this subject, since (almost) any project with Node has to use these event
> listeners. Not having them garbage collected would result in a memory leak.

Event emitters get collected like all other objects.

Tom

unread,
Jul 7, 2010, 9:22:07 AM7/7/10
to nod...@googlegroups.com
Yes. The issue is the collection of the event listeners though. Is this done whenever an event emitter is qualified for collection? TheTurtle did say so, but he was not 100% sure either.

- Tom


--

Marco Rogers

unread,
Jul 7, 2010, 9:44:17 AM7/7/10
to nodejs
Tom the event listeners are just functions that are held in the
_events hash in the emitter. When the emitter gets collected the hash
gets collected and there's nothing else referencing the listener
functions. So eventually they get collected too. It's just
javascript. Nothing fancy happening.

I think there is some confusion because v8 has the concept of weak
references on the C++ side. But there is nothing there in the event
emitter code that would keep listeners from being collected. Does
that help?

:Marco
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .

r...@tinyclouds.org

unread,
Jul 7, 2010, 10:13:11 AM7/7/10
to nod...@googlegroups.com
On Wed, Jul 7, 2010 at 3:22 PM, Tom <tomm...@gmail.com> wrote:
> Yes. The issue is the collection of the event listeners though. Is this done
> whenever an event emitter is qualified for collection? TheTurtle did say so,
> but he was not 100% sure either.

Yes, Event Listeners get garbage collected like all other objects.

Tom

unread,
Jul 7, 2010, 10:15:19 AM7/7/10
to nod...@googlegroups.com
Yes, thank you.

Reply all
Reply to author
Forward
0 new messages