1 Jun 2011

(Fix) Memory Leaks: IE and JQuery 1.4 Ajax

Author: Ben Adams | Filed under: Development

While testing Illyriad we found that over time the browsers IE7 and IE8 leak memory on Ajax calls using JQuery 1.4.

If you make a lot of ajax calls without changing page this can build up quite quickly and start causing issues. This is caused by the onreadystatechange event not being detached and IE not garbage-collecting the associated memory.

To fix this, locate these lines in the source JQuery file; already kindly annotated with “Stop memory leaks”:

// Stop memory leaks
if ( s.async ) {
 xhr = null;
}

Add in these extra lines to change them to the following (we detach the abort event also for good measure):

// Stop memory leaks
if ( s.async ) {
 try {
  xhr.onreadystatechange = null;
  xhr.abort = null;
 } catch (ex) { };
 xhr = null;
}

Don’t forget to minify your jquery file before including it. [We prefer using UglifyJS]

Also rename it slightly so users with cached versions pick up the new file

e.g. jquery-1.4.4a-min.js

No related posts.

Tags: , , , , , , , , , ,

One Response to “(Fix) Memory Leaks: IE and JQuery 1.4 Ajax”

  1. (Fix) Memory Leaks: Ajax page replacement | Illyriad - Beneath the Misted Land Says:

    [...] coupled with the Fix for IE and JQuery 1.4 Ajax deal with most of the regular unexpected [...]

Leave a Reply