Skip to main content

Bug Tracker

Side navigation

#6242 closed bug (fixed)

Opened March 08, 2010 01:58PM UTC

Closed December 06, 2010 10:23PM UTC

Last modified March 09, 2012 01:53PM UTC

Memory leak in IE8 when using ajax

Reported by: jlhm Owned by: snover
Priority: blocker Milestone: 1.5
Component: ajax Version: 1.4.4
Keywords: IE8 java memory leak Cc:
Blocked by: Blocking:
Description

I've an application where I use the ajax function in jquery to query the web server every second for updated data.

In FF the memory utilization remains constant even after running the script for several days.

The same thing in IE8 make the memory utilization to increase from around 60 MB to over 1.5GB in less than 24 hours.

After some research and testing I've found that nullify event handlers abort and onreadystatechange before nullify xhr in jquery 1.4.2 the memory leak stop in IE8 and the memory utilization remains at the initial allocation +/- 5MB.

Current source:

Stop memory leaks

if ( s.async ) {

xhr = null;

}

My modified source:

Stop memory leaks

if ( s.async ) {

xhr.onreadystatechange = null;

xhr.abort = null;

xhr = null;

}

Attachments (0)
Change History (28)

Changed March 25, 2010 12:25PM UTC by Daazku comment:1

I also have the same problem on IE7.

But I found a different solution. Just don't use HttpXmlRequest and use activeX for ie. It don't leak anymore.

I also created a ticket before viewed this one.

http://dev.jquery.com/ticket/6343

Changed March 30, 2010 02:23PM UTC by DNS comment:2

I can confirm this on IE7 & IE8 on every machine I've tried it on. I also found a StackOverflow question (http://stackoverflow.com/questions/2429056/simple-jquery-ajax-call-leaks-memory-in-ie) that describes the same problem.

Changed April 08, 2010 07:05PM UTC by ryleyb comment:3

I experienced the same problem and that fix seems to have done the trick.

I haven't detected any downside to doing this yet, ajaxError events still trigger, future AJAX calls seem to still work fine.

Changed June 29, 2010 07:45AM UTC by domo comment:4

I have the same problem in FF3.6.6. The fix seems to works with IE7 (checked with sIEve), but in FF3.6.6 the problem remains.

Changed July 19, 2010 01:59PM UTC by pascal comment:5

I have the same problem. The fix don't works for me (IE 8). The use of activeX don't resolve the problem too.

I also created a ticket before viewed this one. http://dev.jquery.com/ticket/6802

Changed September 14, 2010 09:23PM UTC by benjamin comment:6

This proposed fix works for me.

I am using long polling to get data from my server app. Users keep my page up all day long. Without this fix, the memory continues to skyrocket. With it, it works as expected.

Changed October 07, 2010 04:18PM UTC by ta comment:7

I'm experiencing the same phenomenon using IE8.

I tried to apply the fix, but IE throws an error at me because of

xhr.onreadystatechange = null;

It's telling me: types incompatible

When i output typeof xhr.onreadystatechange the result is 'unknown'.

So i'm not really able to confirm if this fixes the problem, since i'm not able to apply it.

Any ideas what the solution might be?

Changed October 11, 2010 02:37PM UTC by elija comment:8

We found that the suggested fix will break IE 6.

Try this instead

// Stop memory leaks

if ( s.async ) {

if (typeof(xhr) !== 'undefined') {

if (typeof(xhr.onreadystatechange) !== 'unknown' ) {

xhr.onreadystatechange = null;

}

if (typeof(xhr.abort) !== 'unknown' ) {

xhr.abort = null;

}

xhr = null;

}

}

This should also fix the IE7 / 8 issues

Changed October 15, 2010 10:23PM UTC by snover comment:9

priority: → blocker
status: newopen

Changed October 18, 2010 06:27PM UTC by chris@prettycode.org comment:10

I can confirm that this is still a bug, as elija and snover have found. With msie6, "onreadystatechange" and "abort" of "xhr" have typeof()s of "unknown", which are shown as "void" types in in the Visual Studio 2010 JavaScript debugger.

Changed October 19, 2010 03:48PM UTC by jvenema@gmail.com comment:11

This should work:

try {

x.onreadystatechange = null;

} catch (e3) {

// IE6

x.onreadystatechange = function() { };

}

Changed October 25, 2010 07:42AM UTC by cobexer comment:12

we use this since a few months now and have found no problems so far

// Stop memory leaks
if ( s.async ) {
  try {
    xhr.onreadystatechange = null; xhr.abort = null;
  }
  catch(_) {/* ignore */}
  xhr = null; //this line was there originally
}

Changed October 26, 2010 10:34AM UTC by anonymous comment:13

I tried this fix (IE8), but for me it's not working. I have an intranet app that gets images from server, and puts them to div tag.

First i empty div tag:

$("images_div").empty();

Then place new images:

$("images_div").html(images);

Memory used by IE8 continues to grow. I tryed Firefox 3.6 and Chrome also,

but same problem apears.

Any suggestions?

Changed October 27, 2010 06:57AM UTC by cobexer comment:14

this bug is only related to AJAX requests, your images leak of another reason.

There was a problem once with jQuery UI, jQuery UI widgets and empty that caused widgets to not be destroyed when using empty, are your versions up to date?

Changed October 27, 2010 10:09PM UTC by snover comment:15

milestone: 1.4.31.4.5

Retargeting for next release.

Changed November 11, 2010 05:44PM UTC by anonymous comment:16

This didn't fix it for me in IE8.0

I also used a wrapper function on top of editing the jQuery 1.4.3 and still no luck.

I am starting to feel that there's something else, more fundamental, with IE8.0 and those annoying memory leaks!

Any idea?

Changed November 15, 2010 07:24PM UTC by anonymous comment:17

It worries me that after SO LONG this bug is still present, even though the fix is posted in the forum... what's up guys, this is making systems crash all over... think of all the wasted bytes!!! :)

Changed November 22, 2010 11:44PM UTC by shayla comment:18

I am also seeing this issue, largely in IE8. I haven't yet tested FireFox, but I do not see the same issue in Chrome.

I have applied a suggested change (wrapping .ajax to nullify onreadystatechange and abort), and while the leaks do not appear to grow at such a large rate, the memory used is still increasing (perhaps due to a separate leak, I am not yet certain).

Just want to show another voice that this is quite an issue, and (non-workaround) fix would be greatly appreciated!

Changed December 03, 2010 07:17AM UTC by snover comment:19

owner: → snover
status: openassigned

Changed December 06, 2010 11:50AM UTC by anonymous comment:20

Here's a thread that documents this bug and a possible workaround.

http://forum.jquery.com/topic/memory-leaks-with-ajax-calls#14737000001547290

Changed December 06, 2010 10:23PM UTC by john comment:21

resolution: → fixed
status: assignedclosed
version: 1.4.21.4.4

Landed.

Changed December 07, 2010 01:12AM UTC by anonymous comment:22

I will try the fix but I'm not sure it will fix it as the two function calls will still contain code from noop that occupy some bytes that will not be released by IE.

Setting them to null does the release that IE doesn't seems to do.

I will let you know how my testing is going.

Changed December 07, 2010 01:17AM UTC by snover comment:23

_comment0: References to `jQuery.noop` didn’t appear to matter in my testing, so it seems like it was an issue of IE leaking the attached function rather than the XHR object itself. Since `jQuery.noop` is always in scope, it would never be eligible for garbage collection anyway. \ \ In my testing, after several tens of thousands of XHR calls, memory usage remained flat in IE6-8 with this fix, whereas it increased by several MB/s when the leak was occurring. \ \ The only problem that still exists is that memory leaks in IE7 if a request is aborted; I’m not sure why, but figured that some fixes were better than no fixes. If you can figure out how to stop this, I’m happy to revise the patch.1291685128482006
_comment1: The leak happened because the attached function references the original object via a closure. This causes a circular reference in IE. `jQuery.noop` does not go outside its own private scope and thus is not susceptible to this issue. \ \ In my testing, after several tens of thousands of XHR calls, memory usage remained flat in IE6-8 with this fix, whereas it increased by several MB/s when the leak was occurring. \ \ The only problem that still exists is that memory leaks in IE7 if a request is aborted; I’m not sure why, but figured that some fixes were better than no fixes. If you can figure out how to stop this, I’m happy to revise the patch.1291685162553953
_comment2: The leak happened because the attached function references the original object via a closure. This causes a circular reference that IE’s garbage collector does not know how to handle. `jQuery.noop` does not go outside its own private scope and thus is not susceptible to this issue. \ \ In my testing, after several tens of thousands of XHR calls, memory usage remained flat in IE6-8 with this fix, whereas it increased by several MB/s when the leak was occurring. \ \ The only problem that still exists is that memory leaks in IE7 if a request is aborted; I’m not sure why, but figured that some fixes were better than no fixes. If you can figure out how to stop this, I’m happy to revise the patch.1291685301226050

The leak happened because the attached function references the original object via a closure. This causes a circular reference that IE’s garbage collector does not know how to handle. Dereferencing the function that generated the closure resolves the circular reference and allows GC to occur.

In my testing, after several tens of thousands of XHR calls, memory usage remained flat in IE6-8 with this fix, whereas it increased by several MB/s when the leak was occurring.

The only problem that still exists is that memory leaks in IE7 if a request is aborted; I’m not sure why, but figured that some fixes were better than no fixes. If you can figure out how to stop this, I’m happy to revise the patch.

Changed December 21, 2010 01:14PM UTC by anonymous comment:24

This neat solution worked for me:

$.ajax($.extend({}, settings, {

complete: function (xhr, status) {

Avoid IE memory leak

xhr.onreadystatechange = null;

xhr.abort = null;

}

));

Based on the suggestion to use a wrapper function here:

http:spin.atomicobject.com/2010/10/08/jquery-ajax-memory-leak-in-ie8#fn0

Changed January 14, 2011 10:20PM UTC by jitter comment:25

milestone: 1.4.51.5

Move fixed tickets to appropriate milestone

Changed January 26, 2011 02:26AM UTC by saito.yusuke@best-s.net comment:26

if ( s.async ) {

xhr.onreadystatechange = null; xhr.abort = null; xhr = null;

}

I have a question...

Why it's only "async"? The memory also leaks in sync ajax, I think.

Is it OK to remove "if ( s.async )"??

Changed January 26, 2011 05:40PM UTC by snover comment:27

This code does not exist anymore and this ticket is totally superfluous and irrelevant. Please do not comment any more on it.

Changed March 29, 2011 06:10AM UTC by zubairuss@gmail.com comment:28

Hello Sir,

Where should i put that peace of code?