Garbage collecting in IE with ajax heavy applicaitons

Written by Alex Wolkov

We have a very heavy Ajax application on one of our sites. It reloads block of html on the site every 5 seconds or every user click, so obviously it’s intense.

I was approached by our QA and he told me that he’s computer freezes after some time the site is running.

And I started to investigate. Turns out we haven’t been collecting our garbage ourselves, and the “good” browsers took care of that for us.

Not IE though. IE was stuck after some 5-6 requests, and would just come to a halt on page.unload()

Manual garbage collecting to the rescue.


_temp = $.ajax(/* get loads of html data from ajax here*/);

_temp = _temp.replace(/*apply needed transformations before outputing code to the page*/)

$('#updated_div').html(_temp);

//garbage collecting - very important

delete _temp;

As you can see, one small line of code can help so much to your heavy Ajax application in browsers like IE.

  • confiq

    I wander if chaining would help…

  • Altryne

    what kind of chaining?

  • confiq

    chaining in jquery… you know! when you chain your methods into one line.

    http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx#tip10

  • And how is chaining helping here?

  • Altryne

    Yeah bro, I know about chaining, but as mike said, this has actually nothing to do with jQuery, it’s a javascript issue.
    In addition, chaining doesn’t really speed things up, of you write your code correctly, and cache all that needs caching chaining can even make your actions slower a bit.
    Certainly won’t help in this case.

  • Serge Krul

    I’m not sure writing delete _temp; will garbage collect it. I think delete is only valid for object properties or arrays. In fact JSLINT even says to remove delete expressions for local variables.
    Perhaps in this case writing _temp=null; or even just declaring it as a local will garbage collect it after the scope of the function (if its not in a closure of-course).

  • Altryne

    Yeah but there are load of stuff that JSLINK advices and aren’t really up to par with the real word.
    But I agree you can set it to null, and it’ll also tell IE that this is to be collected.
    Thing is, only delete worked in our case, I should really check why…