title: JQuery Garbage Collection - Will This Be Clean?
body: <p>Many articles (e.g. <a href="http://msdn.microsoft.com/en-us/library/dd361842%28VS.85%29.aspx" rel="nofollow noreferrer">msdn</a>) have said that a circular reference cannot be cleaned up in some browsers when it involves a <strong>DOM</strong> object and a <strong>JS</strong> object.</p>
  
  <p>(IE 6 can't do it at all and IE7 can only do it between page requests):</p>
  
  <p>Javascript Native (<strong>Leaks</strong>):</p>
  
  <pre><code>function leak(){
      var elem = document.createElement("DIV");
      document.body.appendChild(elem);
      elem.onclick = function () {
          elem.innerHTML = elem.innerHTML + ".";
          // ...
      };
  }
  </code></pre>
  
  <p>Because the element's onload property refers back to itself through a closure, it creates a <strong>circular reference</strong>:</p>
  
  <p><code>elem [DOM] -&gt; elem.onclick [JS] -&gt; elem [DOM]</code></p>
  
  <p>JQuery Version (<strong>Does NOT Leak</strong>):</p>
  
  <pre><code>function leak(){
      var elem = $('&lt;div&gt;&lt;/div&gt;');
      $(document.body).append(elem);
      elem.click(function () {
          elem.html(elem.html() + ".");
          // ...
      };
  }
  </code></pre>
  
  <p>In this case, jQuery stops the leak from happening in ALL browsers concerned even though there is still a circular reference:</p>
  
  <p><code>elem [JS] -&gt; element [DOM] -&gt; elem.onclick [JS] -&gt; elem [JS]</code></p>
  
  <p><strong>My Question: How does jQuery stop the leak if there is still a circular reference?</strong></p>