AnApprentice
AnApprentice

Reputation: 110940

jQuery $.ajax memory leak?

Why does jQuery ajax cause a horrible memory leak on Firefox & other browsers?

jsfiddle: http://jsfiddle.net/Rqfz7/

If you run this in a browser like Firefox, this causes the memory to climb. Has anyone else seen this? Is there a way to stop the memory leak with jQuery ajax? Thanks

$.ajax({
    url: "http://ws.geonames.org/searchJSON",
    dataType: "jsonp",
    data: {
        featureClass: "P",
        style: "full",
        maxRows: 12,
        name_startsWith: "M"
    },
    success: function( data ) {
        setInterval(function() {
            get_some_stuff();
        }, 1000);
        load_count = load_count + 1;
        $('body h1').text('load_count: ' + load_count);             
    }
})

Upvotes: 0

Views: 2291

Answers (4)

Itamar
Itamar

Reputation: 81

I recently had a similar issue, repeated ajax calls to a json web service resulted in heavy memory leaks even without any processing of the json object itself.

Turns out that there's a bug in jQuery I guess. Change your dataType to 'text', and then parse the resulting response with a library that doesn't use eval() which is what's causing the leaks I believe.

I found this library here json_parse.js. So far it's working well, although it uses a bit more CPU.

Upvotes: 0

Zbigniew
Zbigniew

Reputation: 27584

It is because you are calling setInterval() from within get_some_stuff() function. setInterval is used to run funciton over and over again at a specified time interval. Your issue is because all those registered functions calls (via setInterval) are executing every second and there are lots of them.

Upvotes: 4

ek_ny
ek_ny

Reputation: 10233

I think you want to do SetTimeout instead of SetInterval.

Try this..

http://jsfiddle.net/Rqfz7/2/

Upvotes: 1

Jeff
Jeff

Reputation: 309

You are recursively calling get_some_stuff() upon success of the ajax request. Additionally, every time the ajax request completes, you are setting a loop that calls the function after 1 second. Every time the ajax gets called you call another loop. If this went on for 1000 cycles, you'd have 1000 loops all calling the get_some_stuff() function.

Upvotes: 0

Related Questions