Skip to main content

Bug Tracker

Side navigation

#10824 closed bug (invalid)

Opened November 18, 2011 11:46AM UTC

Closed December 20, 2011 07:50AM UTC

Last modified March 14, 2012 10:30AM UTC

jQuery.ajax ($.ajax) and its memory leaking

Reported by: fabbios Owned by: fabbios
Priority: low Milestone: None
Component: ajax Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

I faced a serius problem with an application suffering of memory leaking. After almost get crazy I could isolate the problem and I figured out the problem was the jQuery.ajax.

All my pain I posted in here (http://stackoverflow.com/questions/8158739/ie-memory-leak-and-eval-with-jquery/8176724#8176724).

I'm using in my application jQuery.ajax, which is called in a regular basis (like every 10 seconds), but after each iteration some memory wasn't being released, it reaches until 1GB of memory usage, to solve it I did this:

var request = $.ajax({ .... });

When finished the request:

request.onreadystatechange = null;

request.abort = null;

request = null;

jQuery.ajax doesn’t do that and the memory never releases. It solves my problem and I found a lot of people with the same issue.

Attachments (0)
Change History (11)

Changed November 18, 2011 02:51PM UTC by dmethvin comment:1

owner: → fabbios
status: newpending

Version 1.4.2 is nearing its second birthday. We only maintain the most recent release. Can you reproduce this in jQuery 1.7?

Changed November 19, 2011 07:41AM UTC by cbr@nicelooking.de comment:2

I can confirm this. Its also in 1.7.

Changed November 19, 2011 01:56PM UTC by dmethvin comment:3

Can you provide a test case?

Changed November 19, 2011 04:09PM UTC by cbr@nicelooking.de comment:4

I got following scenario:

I had to deliver preloaded pages, so i build three frames.

1. Frame: controlscript with the ajax request

2. Frame: to preload the page of next request

3. Frame: to display the cached page from Frame 2

The controlscript at frame1 asks the masterscript about the next page-url to display and the duration of showing. It loads the page into frame2 because of caching. If the page at frame2 is loaded (jquery event document.ready), it will change the document.href of frame 3 to the document.href of frame 2.

Following code i've done and rises the memory usage at every request:

<script>

f_preload = parent.preload.document;

f_preload.location.href = 'URI_OF_FIRST_PAGE';

var dauer = 10;

var intID;

$(document).ready(function(){

intID = setInterval(Seitenwechsel, 1000);

});

var Seitenwechsel = function() {

var f_content = window.parent.content.document;

var f_preload = window.parent.preload.document;

if (f_content.location.href == f_preload.location.href)

{

dauer--;

if (dauer < 0)

{

clearInterval(intID);

$.get("/URL_TO_MASTERSCRIPT.php", { someValues: "value"}, function(data) {

check if answer is okay

nextparams = data.split(" ");

if (nextparams[2] != "OK") {dauer=2; intID = setInterval(Seitenwechsel, 1000);}

dauer = nextparams[1];

f_preload = parent.preload.document;

f_preload.location.href = nextparams[0] + "?" + Math.round(Math.random() * 999999999999);

intID = setInterval(Seitenwechsel, 1000);

})

.error(function() { dauer=2; intID = setInterval(Seitenwechsel, 1000); }) ran into error, check frequently now to get a good answer

}

}

}

</script>

Changed November 19, 2011 05:53PM UTC by timmywil comment:5

component: unfiledajax
priority: undecidedlow

Thanks for taking the time to contribute to the jQuery project! Please provide a working reduced test case to help us assess your ticket.

Additionally, be sure to test against the jQuery Git version to ensure the issue still exists.

Changed November 22, 2011 06:03PM UTC by fabbios comment:6

status: pendingnew

Also I'm confirming it's leaking in 1.7

I've noticed that it leaks less memory than in 1.4.2, but it's just feeling I didn't perform any deep test.

This is exactly I what have in my code:

var interval;
function setupdaterate(rate) {
   interval = setInterval(updateitems, rate * 1000);
}


function updateitems() {
    
        $('.updatable').each(function () {
            var data = 'varTest=12';

            var request = $.ajax({
                async: true,
                url: '/url.ashx',
                data: data,
                type: 'POST',
                timeout: 10000,
                success: function (data) {
                    //update some IU component here.
                }
            });

            data = null;

            //HERE IS THE HACK! :)
            request.onreadystatechange = null;
            request.abort = null;
            request = null;
        });
    
}

Changed November 28, 2011 09:01PM UTC by sindresorhus comment:7

Please read the text in the red box:

Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Changed November 28, 2011 09:06PM UTC by timmywil comment:8

status: newpending

It would help us to have a small test case that can reproduce the issue.

Changed November 29, 2011 01:09PM UTC by Jonas L comment:9

"request.onreadystatechange = null;

request.abort = null;

request = null;"

This is the same fix I reported for 1.4.2 over a year ago. It looks like as long IE has anything assigned to a callback it will not release the memory for the ajax object.

/Jonas

Changed December 06, 2011 06:36AM UTC by Kei Oikawa comment:10

I noticed that $.ajax() involves memory leaks in IE6(SP3) and IE8 when it is used from other frame.

This thread really helps me. The solution in this thread works perfectly. I really appreciate it.

Situation is like this:

1) We have two frames.

2) jQuery(v1.6.2) is loaded in the parent frame.

3) Our application run in one of the frames and uses parent jQuery.

4) When the application uses $.ajax() to get JSON file, it leaks!

Apparently, using jQuery $.ajax function from different frame seems to be a bad habit for IE

because when I tried to remove jQuery in the parent frame and put it in the application frame(child frame, in other word) instead,

the problem was gone completely. But in terms of saving memory consumption, I wanted like to load

jQuery library in the parent frame and share it in the child frames.

When this situation happens, nullifying the return value of $.ajax() just stated above works perfectly.

In addition to that, it seems sometimes IE6 does not release memory even if the returned XHR object is set to be null.

So, I suggest to set "noop" function(empty function) instead of using setting null. I hope this information helps for all users in the same trouble.

var noop = function(){}; // function doing nothing

var $req = $.ajax( ... ); // ajax processing

// Clean XHR object up
if( $req != null ){
    $req.onreadystatechange = $req.abort = noop;
        $req = null;        
}

Changed December 20, 2011 07:50AM UTC by trac-o-bot comment:11

resolution: → invalid
status: pendingclosed

Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!