Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#10824 closed bug (invalid)

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.

Change History (11)

comment:1 Changed 8 years ago by dmethvin

Owner: set to 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?

comment:2 Changed 8 years ago by cbr@…

I can confirm this. Its also in 1.7.

comment:3 Changed 8 years ago by dmethvin

Can you provide a test case?

comment:4 Changed 8 years ago by cbr@…

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>

comment:5 Changed 8 years ago by timmywil

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.

comment:6 Changed 8 years ago by fabbios

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;
        });
    
}

comment:7 Changed 8 years ago by sindresorhus

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.

comment:8 Changed 8 years ago by timmywil

Status: newpending

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

comment:9 Changed 8 years ago by Jonas L

"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

comment:10 Changed 8 years ago by Kei Oikawa

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;        
}

comment:11 Changed 8 years ago by trac-o-bot

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!

Note: See TracTickets for help on using tickets.