Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force Javascript garbage collection in IE? IE is acting very slow after AJAX calls & DOM manipulation

I have a page with chained drop-downs. Choosing an option from the first select populates the second, and choosing an option from the second select returns a table of matching results using the innerHtml function on an empty div on the page.

The problem is, once I've made my selections and a considerable amount of data is brought onto the page, all subsequent Javascript on the page runs exceptionally slowly. It seems as if all the data I pulled back via AJAX to populate the div is still hogging a lot of memory. I tried setting the return object which contains the AJAX results to null after calling innerHtml but with no luck.

Firefox, Safari, Chrome and Opera all show no performance degradation when I use Javascript to insert a lot of data into the DOM, but in IE it is very apparent. To test that it's a Javascript/DOM issue rather than a plain old IE issue, I created a version of the page that returns all the results on the initial load, rather than via AJAX/Javascript, and found IE had no performance problems.

FYI, I'm using jQuery's jQuery.get method to execute the AJAX call.

EDIT This is what I'm doing:

<script type="text/javascript">
function onFinalSelection() {
  var searchParameter = jQuery("#second-select").val();
  jQuery.get("pageReturningAjax.php",
  {SEARCH_PARAMETER: searchParameter},
  function(data) {
    jQuery("#result-div").get(0).innerHtml = data;
   //jQuery("#result-div").html(data); //Tried this, same problem
    data = null;
  },
  "html");
}
</script>

I want to note that this only becomes an issue when the return data is quite large. It is directly related to the size, as I am able to see moderate slowdown for medium size results and only major slowdown when it is a few hundred records + being returned.

like image 219
aw crud Avatar asked May 03 '10 18:05

aw crud


People also ask

How do I force garbage collection in JavaScript?

You can force major garbage collection to be triggered in a Node-based runtime (e.g. Electron or NW. js), by using the --expose-gc flag and running: global. gc();

Is garbage collection automatically in JavaScript?

Some high-level languages, such as JavaScript, utilize a form of automatic memory management known as garbage collection (GC). The purpose of a garbage collector is to monitor memory allocation and determine when a block of allocated memory is no longer needed and reclaim it.

Can users prevent garbage collection in JavaScript?

Garbage collection is performed automatically. We cannot force or prevent it. Objects are retained in memory while they are reachable.

How could you make sure a const value is garbage collected?

With a const variable declaration, you can't assign to the variable something little like "" or null to clear its contents. That's really the only difference in regard to memory management. Automatic garbage collection is not affected at all by whether it is declared const or not.


1 Answers

You can force garbage collection in IE by using the CollectGarbage function, e.g.

if (typeof(CollectGarbage) == "function")
    CollectGarbage();

The JScript garbage collector is described in detail in this blog entry: http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx

As the blog says, the GC is not predictable, so delete data or data = null will not reclaim the memory immediately, but it eventually will reclaim it.


But I doubt that your performance penalty is really caused by the memory usage; I think that it is a problem with DOM rendering.

like image 122
ignaZ Avatar answered Oct 14 '22 03:10

ignaZ