Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Basically a walkthrough of your code:</p> <ul> <li>create element (el)</li> <li>create a new function that references that element</li> <li>set the function to be the onclick of that element</li> <li>overwrite the element with a new element</li> </ul> <p>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.</p> <p>Someone might have a more technical example, but that's the basis of my understanding of the javascript garbage collector.</p> <p>Edit: Here's one of many possibilities for a leaking version of your script:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;/body&gt; &lt;script&gt; var i, el; var createdElements = {}; var events = []; function attachAlert(element) { element.onclick = function() { alert(element.innerHTML); }; } function reallyBadAttachAlert(element) { return function() { alert(element.innerHTML); }; } for (i = 0; i &lt; 1000000; i++) { el = document.createElement('div'); el.innerHTML = i; /** posibility one: you're storing the element somewhere **/ attachAlert(el); createdElements['div' + i] = el; /** posibility two: you're storing the callbacks somewhere **/ event = reallyBadAttachAlert(el); events.push(event); el.onclick = event; } &lt;/script&gt; &lt;/html&gt; </code></pre> <p>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 <code>events[10]();</code>) 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload