polymer icon indicating copy to clipboard operation
polymer copied to clipboard

Memory leak from _handleNative with ShadyDOM

Open Legioth opened this issue 6 years ago • 5 comments

Description

The Shady DOM polyfill overrides addEventListener and among other things stores some metadata as an extra property on the event handler function or object. This causes memory leaks when the same event listener is used for multiple elements, unless you're careful to explicitly remove all listeners when the element is no longer used.

It could be argued that this is a bug in Shady DOM, but a comment in the code implies that changing it to avoid leaks would have undesirable effects on performance: https://github.com/webcomponents/shadydom/blob/f7591f7e72a6536b5d7752c493df108a5f47f4bb/src/patch-events.js#L429-L433

This causes problems with _handleNative in gestures.js since the same function instance is used for all elements for which some specific listeners are registered, thus preventing all such elements from being garbage collected. Furthermore, adding _handleNative as a listener becomes slower and slower as the array of metadata instances keeps growing.

To work around this particular case, Polymer could create a per-node function that delegates to _handleNative and use that function instead of directly adding _handleNative as the event listener.

The problem can be observed with a variety of existing elements such as <paper-button> or <vaadin-grid>.

Live Demo

https://jsbin.com/domimaxidi/1/edit?html,console,output

Steps to Reproduce

  1. Open the live demo in Edge (also reproduces in IE 11, but my example won't load properly there)
  2. Click the element repeatedly to create more and more leaking instances

Expected Results

Taking regular random fluctuations into account, the displayed time information should remain constant.

Actual Results

In Edge, the time taken to perform the action starts creeping upwards with repeated clicks. On my machine, it starts out at around 30 ms and then keeps increasing so that the 10th click takes around 70 ms and the 20th is around 110 ms.

Browsers Affected

  • [ ] Chrome
  • [ ] Firefox
  • [x] Edge
  • [x] IE 11

Versions

  • Polymer: v2.7.2
  • webcomponents: v1.3.3

Legioth avatar May 21 '19 10:05 Legioth

Sorry for the delay on this. It's a very good finding and something we definitely need to fix. We're going to address in the ShadyDOM polyfill itself, see https://github.com/webcomponents/polyfills/issues/175.

sorvell avatar Jul 15 '19 18:07 sorvell

@sorvell thanks for triaging this! I will keep an eye on that issue.

web-padawan avatar Jul 15 '19 18:07 web-padawan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 14 '20 18:07 stale[bot]

No news about this issue? In a SPA this issue is a disaster!

Eymerich01 avatar Nov 24 '20 11:11 Eymerich01

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 02 '22 10:03 stale[bot]