Memory leaks in addEventListener

When registering an event through the addEventListener element addEventListener then delete this element without deleting the event, and repeating this several times, will there be a memory leak?

+8
javascript dom memory-leaks
source share
2 answers

It should not flow. One browser that infamously infiltrates like hell when the event handler calls the host object <> loop of the JS object is IE (prior to version 7), and IE (prior to version 8) does not support addEventListener .

Leave this in working order and see how the use of browser memory in the long run affects if you want to test it in a specific browser.

 <div id="x"></div> <script type="text/javascript"> function replace() { var x= document.getElementById('x'); if (x.firstChild!==null) x.removeChild(x.firstChild); var el= document.createElement('p'); el.addEventListener('click', click, false); x.appendChild(el); } function click() { alert('click'); }; setInterval(replace, 1); </script> 

(To test it with the reference loop, move the function click definition to the replace body.)

+5
source share

You will lose a memory leak if you remove items from the DOM that connected listeners. But this only happens in IE, Fx, and others in GC.

It often happens if you manipulate DOM elements not through the DOM, but how

 el.innerHTML = ... 

For example, YUI has a custom implementation of setInnerHTML to prevent memory leaks in this case.

0
source share

All Articles