Am I correct in assuming that when an element is detached, i.e, its detached() method for Polymer 1.+ or disconnectedCallback() for Polymer 2.+ runs, all references to that element and its childs are removed and no memory leak can be associated with that element anymore?
Asked
Active
Viewed 503 times
1 Answers
0
No, detached
/disconnectedCallback
are invoked when the element is removed from the document, but it does not indicate that there are no references to the element.
For example, a Polymer element could call setInterval()
with a callback that holds a reference to one of its properties. Removing that element from the document (e.g., via Node.removeChild()
) does not automatically stop the timer, so it's possible for the element to remain in memory after no longer being in the document. clearInterval()
should be called in order to avoid this.
Also note JavaScript currently has no garbage collection semantics. [1]

tony19
- 125,647
- 18
- 229
- 307
-
Great explanation thanks. Any suggestion on how to approach finding memory leaks in Polymer elements or perhaps using Polymer doesn't change anything and I should still think pure JavaScript? just asking in case you have any input. – TheBen Oct 31 '17 at 02:10
-
1No problem :) Regarding memory leaks in Polymer elements: That's such a broad topic, since practically any type of JS memory leak is possible in Polymer :) Here's a good reference on how to avoid and troubleshoot them: https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/ – tony19 Oct 31 '17 at 18:05