If a DOM Element is removed, are its listeners also removed from memory?

后端 未结 6 1735
北荒
北荒 2020-11-22 06:18

If a DOM Element is removed, are its listeners removed from memory too?

6条回答
  •  忘了有多久
    2020-11-22 07:01

    Don't hesitate to watch heap to see memory leaks in event handlers keeping a reference to the element with a closure and the element keeping a reference to the event handler.

    Garbage collector do not like circular references.

    Usual memory leak case: admit an object has a ref to an element. That element has a ref to the handler. And the handler has a ref to the object. The object has refs to a lot of other objects. This object was part of a collection you think you have thrown away by unreferencing it from your collection. => the whole object and all it refers will remain in memory till page exit. => you have to think about a complete killing method for your object class or trust a mvc framework for example.

    Moreover, don't hesitate to use the Retaining tree part of Chrome dev tools.

提交回复
热议问题