How do I create a memory leak in JavaScript?

前端 未结 6 1665
南笙
南笙 2021-01-31 03:34

I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory co

6条回答
  •  悲哀的现实
    2021-01-31 04:12

    You're not keeping the element you've created around and referenced anywhere - that's why you're not seeing the memory usage increase. Try attaching the element to the DOM, or store it in an object, or set the onclick to be a different element that sticks around. Then you'll see the memory usage skyrocket. The garbage collector will come through and clean up anything that can no longer be referenced.

    Basically a walkthrough of your code:

    • create element (el)
    • create a new function that references that element
    • set the function to be the onclick of that element
    • overwrite the element with a new element

    Everything is centric around the element existing. Once there isn't a way to access the element, the onclick can't be accessed anymore. So, since the onclick can't be accessed, the function that was created is destroyed.. and the function had the only reference to the element.. so the element is cleaned up as well.

    Someone might have a more technical example, but that's the basis of my understanding of the javascript garbage collector.

    Edit: Here's one of many possibilities for a leaking version of your script:

    
    
    
    
    
    

    So, for #1, you're simply storing a reference to that element somewhere. Doesn't matter that you'll never use it - because that reference is made in the object, the element and its callbacks will never go away (or at least until you delete the element from the object). For possibility #2, you could be storing the events somewhere. Because the event can be accessed (i.e. by doing events[10]();) even though the element is nowhere to be found, it's still referenced by the event.. so the element will stay in memory as well as the event, until it's removed from the array.

提交回复
热议问题