A PSA about using EventEmitters between windows

2 views
Skip to first unread message

Michael Morrison

unread,
Apr 10, 2013, 2:55:58 PM4/10/13
to node-...@googlegroups.com
I've been looking into some relatively large memory leaks in my applications recently, and a heap dump showed that I had a ton of events hanging around in EventEmitters.

It made sense once I thought about it -- I had been attaching to events cross window, expecting them to be cleaned up when the window closed. However, this certainly isn't the case. If a window hooks to an EventEmitter in another window and then closes -- the hooks remain, leaking memory from the dom of the closed window.

I was thinking that we should probably do one of two things:

1) Include a proxy class in node-webkit which overrides EventEmitter's on() function with an implementation that cleans up hooks when a window is closed (with an extra parameter to optionally leave the hooks in place).

or 2) Mention this in the documentation somewhere so people don't fall into the same trap.

Regards,
-- Michael

Michael Morrison

unread,
Apr 12, 2013, 12:27:37 AM4/12/13
to node-...@googlegroups.com
After dealing with this for many hours, I have a couple other notes and tips:

1) If you accidentally leave a cross window event handler active after a window is closed, the entire window DOM will be kept in memory forever, because it's part of the context for the closure of the event handler. Long story short, MAKE SURE you clean up event handlers when your windows closes!

2) Rather than using the .on() method of EventEmitters directly, consider using a proxy method in your window's scope which keeps track of all it's connections, then cleans them up with removeListener when the window closes.

/rant

-- Michael

Roger

unread,
Apr 12, 2013, 12:42:08 AM4/12/13
to node-...@googlegroups.com
Thanks.

Any suggestions I can improve this from node-webkit?

Roger


--
You received this message because you are subscribed to the Google Groups "node-webkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-webkit...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Morrison

unread,
Apr 12, 2013, 12:50:11 PM4/12/13
to node-...@googlegroups.com
If it was possible, I would suggest having node-webkit automatically remove any EventEmitter hooks within a window's context when the window is closed. Unfortunately, I don't know much about how accessible this data would be to node-webkit without doing a million hacky proxy tricks.

Ultimately, it might just be a good idea to mention it in the docs. Leaking the entire DOM for a window isn't good, and I was still falling for it four months after getting into node-webkit development. Advising devs to check a heap dump periodically for leaking Window objects would also be nice.

-- Michael
Reply all
Reply to author
Forward
0 new messages