Friday, October 12, 2018

Re: Passing method references to HTMLElement.removeEventHandler

Is it still good practice to remove listeners when you don't need the html element anymore ? or do modern browser garbage collectors cope with circular references between DOM and JS objects ? It was one of the features of Widgets that they avoided the leaks automatically.

All recent browsers use garbage collection implementations that can deal with cyclic references. So you don't have to remove event listeners when you remove an element from DOM and do not reference it otherwise. 

If you hold a strong reference to an element that has been removed from DOM (e.g. you have an element cache, or some element lookup table somewhere) then you might want to remove the handler to make sure all code reachable from that handler can be garbage collected if you do not need/want to reuse that explicit handler anymore.

Also keep in mind that DOM is a tree and that child nodes have a reference to their parent. So if you hold a reference to a DOM node and then you remove a parent of that DOM node, thus removing a whole sub tree containing your referenced DOM node, then the whole subtree starting from the removed parent will be kept in memory (and not just your referenced DOM node). Typical example would be you hold a reference to a TD element and later remove the whole corresponding TABLE element. Then the TABLE still exists in memory as long as you hold a reference to the TD element and do not append that TD element to a different element.

-- J.

You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

No comments:

Post a Comment