Avoid memory leak with objects that create and remove DOM with jquery
<p>Suppose you have a component whose job is, among other things, to create some DOM node (eg with jquery) : </p>

<pre><code>function PageFiller() {
}
PageFiller.prototype.fillPage = function () {
  this.dom = $("&lt;div/&gt;", {
    "class" : "hello",
    text : "Hello world"
  });
  $("body").append(this.dom);
}
</code></pre>

<p>Suppose that another component uses this to fill the DOM, without keeping a reference to the  "PageFiller" </p>

<pre><code>function Main() {
}
Main.prototype.start = function () {
   var filler = new PageFiller();
   filler.fillPage();
};
var main = new Main()
main.start();
</code></pre>

<p>No suppose that right after that (in the same scope), someone brutally clears the DOM : </p>

<pre><code>$(".hello").remove();
</code></pre>

<p>What is the memory layout in such a situation ? Have I leaked memory with the PageFiller instance ?</p>

<p>I'm a bit unsure at which point the PageFiller instance would get garbage collected (if it ever happen), since it holds a reference to a jquery object representing part of the DOM that does not exists anymore ? 
Or does it have nothing to do with the DOM, since from what I understand the <em>actual</em> DOM objects live in a different heap than the regular JS variables (unless I am mistaken here..)</p>

<p>UPDATE : A variation of the thing would be if the reference to the PageFiller was kept by the Main object : </p>

<pre><code>Main.prototype.start = function () {
    this.filler = new PageFiller();
    filler.fillPage();
}
</code></pre>

<p>In this situation, the Main object has a reference to the PageFiller object, which itself holds a reference to a JQ object ; after the 'remove', the JQ object still exists in memory, but points to non-existent DOM nodes, right ? </p>

<p>Am I correct to say that in the original example, as soon as main.start() is finished : </p>

<ul>
<li>no object holds a reference to the PageFiller object, so it gets garbage collected, and the pageFiller.dom object is not referenced by anyone, so it gets garbage collected too ? </li>
<li>the actual DOM elements obviously still exists in memory, but in a separate heap that I don't really have to worry about</li>
</ul>

<p>OTHER CASE : </p>

<p>This time a small variation, with event handlers to complicate matters : </p>

<pre><code>PageFiller.prototype.fillPage = function () {
  var self = this;
  this.dom = ... // as usual 
  this.dom.click(function () {
    alert("Just clicked on dom generated by " + self);
  });
}
</code></pre>

<p>This time, just letting the PageFiller instance run out of scope could not be enough to have its memory reclaimed, since it is referenced by the closure used in the click handler. Have I leaked memory here ? Or can I trust the $("").remove() call to kill the reference to the closure, hence killing the last reference to the PageFiller ? And would things behave differently if I removed the element without jquery (with the native DOM API ?)</p>

<p>This probably all just work as I hope, but I'm just trying to convince myself that no memory can leak from this pattern.   </p>

<p>Thanks.</p>
