title: Memory leak involving jQuery Ajax requests
body: <p>I have a webpage that's leaking memory in both IE8 and Firefox; the memory usage displayed in the Windows Process Explorer just keeps growing over time.</p>
  
  <p>The following page requests the "unplanned.json" url, which is a static file that never changes (though I do set my <code>Cache-control</code> HTTP header to <code>no-cache</code> to make sure that the Ajax request always goes through).  When it gets the results, it clears out an HTML table, loops over the json array it got back from the server, and dynamically adds a row to an HTML table for each entry in the array.  Then it waits 2 seconds and repeats this process.</p>
  
  <p>Here's the entire webpage:</p>
  
  <pre><code>&lt;html&gt; &lt;head&gt;
      &lt;title&gt;Test Page&lt;/title&gt;
      &lt;script type="text/javascript"
       src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt; &lt;body&gt;
  &lt;script type="text/javascript"&gt;
      function kickoff() {
          $.getJSON("unplanned.json", resetTable);
      }
      function resetTable(rows) {
          $("#content tbody").empty();
          for(var i=0; i&lt;rows.length; i++) {
              $("&lt;tr&gt;"
                  + "&lt;td&gt;" + rows[i].mpe_name + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].bin + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].request_time + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].filtered_delta + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].failed_delta + "&lt;/td&gt;"
              + "&lt;/tr&gt;").appendTo("#content tbody");
          }
          setTimeout(kickoff, 2000);
      }
      $(kickoff);
  &lt;/script&gt;
  &lt;table id="content" border="1" style="width:100% ; text-align:center"&gt;
  &lt;thead&gt;&lt;tr&gt;
      &lt;th&gt;MPE&lt;/th&gt; &lt;th&gt;Bin&lt;/th&gt; &lt;th&gt;When&lt;/th&gt; &lt;th&gt;Filtered&lt;/th&gt; &lt;th&gt;Failed&lt;/th&gt;
  &lt;/tr&gt;&lt;/thead&gt;
  &lt;tbody&gt;&lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>If it helps, here's an example of the json I'm sending back (it's this exact array wuith thousands of entries instead of just one):</p>
  
  <pre><code>[
      {
          mpe_name: "DBOSS-995",
          request_time: "09/18/2009 11:51:06",
          bin: 4,
          filtered_delta: 1,
          failed_delta: 1
      }
  ]
  </code></pre>
  
  <p>EDIT: I've accepted Toran's extremely helpful answer, but I feel I should post some additional code, since his <code>removefromdom</code> jQuery plugin has some limitations:</p>
  
  <ul>
    <li>It only removes individual elements.  So you can't give it a query like `$("#content tbody tr")` and expect it to remove all of the elements you've specified.</li>
    <li>Any element that you remove with it must have an `id` attribute.  So if I want to remove my `tbody`, then I must assign an `id` to my `tbody` tag or else it will give an error.</li>
    <li>It removes the element itself and all of its descendants, so if you simply want to empty that element then you'll have to re-create it afterwards (or modify the plugin to empty instead of remove).</li>
  </ul>
  
  <p>So here's my page above modified to use Toran's plugin.  For the sake of simplicity I didn't apply any of the general performance advice <a href="https://stackoverflow.com/questions/1455947/memory-leak-involving-jquery-ajax-requests/1456073#1456073">offered by Peter</a>.  Here's the page which now no longer memory leaks:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Test Page&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script type="text/javascript"&gt;
  &lt;!--
      $.fn.removefromdom = function(s) {
          if (!this) return;
  
          var el = document.getElementById(this.attr("id"));
  
          if (!el) return;
  
          var bin = document.getElementById("IELeakGarbageBin");
  
          //before deleting el, recursively delete all of its children.
          while (el.childNodes.length &gt; 0) {
              if (!bin) {
                  bin = document.createElement("DIV");
                  bin.id = "IELeakGarbageBin";
                  document.body.appendChild(bin);
              }
  
              bin.appendChild(el.childNodes[el.childNodes.length - 1]);
              bin.innerHTML = "";
          }
  
          el.parentNode.removeChild(el);
  
          if (!bin) {
              bin = document.createElement("DIV");
              bin.id = "IELeakGarbageBin";
              document.body.appendChild(bin);
          }
  
          bin.appendChild(el);
          bin.innerHTML = "";
      };
  
      var resets = 0;
      function kickoff() {
          $.getJSON("unplanned.json", resetTable);
      }
      function resetTable(rows) {
          $("#content tbody").removefromdom();
          $("#content").append('&lt;tbody id="id_field_required"&gt;&lt;/tbody&gt;');
          for(var i=0; i&lt;rows.length; i++) {
              $("#content tbody").append("&lt;tr&gt;&lt;td&gt;" + rows[i].mpe_name + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].bin + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].request_time + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].filtered_delta + "&lt;/td&gt;"
                  + "&lt;td&gt;" + rows[i].failed_delta + "&lt;/td&gt;&lt;/tr&gt;");
          }
          resets++;
          $("#message").html("Content set this many times: " + resets);
          setTimeout(kickoff, 2000);
      }
      $(kickoff);
  // --&gt;
  &lt;/script&gt;
  &lt;div id="message" style="color:red"&gt;&lt;/div&gt;
  &lt;table id="content" border="1" style="width:100% ; text-align:center"&gt;
  &lt;thead&gt;&lt;tr&gt;
      &lt;th&gt;MPE&lt;/th&gt;
      &lt;th&gt;Bin&lt;/th&gt;
      &lt;th&gt;When&lt;/th&gt;
      &lt;th&gt;Filtered&lt;/th&gt;
      &lt;th&gt;Failed&lt;/th&gt;
  &lt;/tr&gt;&lt;/thead&gt;
  &lt;tbody id="id_field_required"&gt;&lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>FURTHER EDIT: I'll leave my question unchanged, though it's worth noting that this memory leak has nothing to do with Ajax.  In fact, the following code would memory leak just the same and be just as easily solved with Toran's <code>removefromdom</code> jQuery plugin:</p>
  
  <pre><code>function resetTable() {
      $("#content tbody").empty();
      for(var i=0; i&lt;1000; i++) {
          $("#content tbody").append("&lt;tr&gt;&lt;td&gt;" + "DBOSS-095" + "&lt;/td&gt;"
              + "&lt;td&gt;" + 4 + "&lt;/td&gt;"
              + "&lt;td&gt;" + "09/18/2009 11:51:06" + "&lt;/td&gt;"
              + "&lt;td&gt;" + 1 + "&lt;/td&gt;"
              + "&lt;td&gt;" + 1 + "&lt;/td&gt;&lt;/tr&gt;");
      }
      setTimeout(resetTable, 2000);
  }
  $(resetTable);
  </code></pre>