Will this lead to a browser memory leak?

I create a pair of divs with an onclick event on them as children of the 'parentDiv' element. In the future, I set parentDiv.innerHTML = 'xyz' , where xyz are more divs with onclick events that replace the old ones.

My question is: will there be any memory leak browsers because I did not delete the onclick events of the old div before deleting them from dom?

+4
source share
4 answers

I would suggest reading a memory leak , as Douglas Crockford explained. The article provides detailed examples of memory leaks and even a good cleaning function that allows you to overcome the problems associated with them. Enjoy it!

+1
source

If you remove them from the DOM, then any event handlers should also be removed from them, and not cause a leak.

Douglas Crockford has an excellent JavaScript memory leak record. Well worth a read.

0
source

No, it depends on the browser. As objects are deleted, the browser will (or at least should) free up associated memory. I would not charge for it.

In any case, if you do not do this a thousand times, I would not expect this to be a problem.

0
source

Usually not.

However, if your onclick handler turns out to be closing, which refers to the HTML element to which it is attached, then it may leak in some browsers.

Take a look at this guide that I wrote for more details on JavaScript memory leaks and how to find and fix them: http://www.vladalexandruionescu.com/2012/08/javascript-memory-leaks.html . That should cleanse you.

0
source

Source: https://habr.com/ru/post/1337507/


All Articles