views:

251

answers:

2

I am experiencing a slow memory leak in both IE and Firefox using a combination of ASP.NET AJAX and jQuery. My scenario is very similar to the one described here : http://stackoverflow.com/questions/276087/preventing-ajax-memory-leaks except using jquery and asp.net AJAX, not protyotype: I have a webpage displaying data in an UpdatePanel that is refreshed every 60 seconds using a timer. in the AJAX javascript pageLoad function that is called on every "partial postback", I re-bind events because they are lost in the asp.net partial postback:

function pageLoad(sender, args) {
    $("#item").unbind();
    $("#item").hover(
        function() {
            // do something
        },
        function() {
            // do something
        });
}

so this is called every 60 seconds. Could this alone be the cause of a memory leak?

+2  A: 

Do this instead:

$(function() { //.ready shortcut...
  $("#item").live("hover",
    function() {
        // do something
    },
    function() {
        // do something
    });
 });

Note, this requires jQuery 1.4.1, but acts entirely different in terms of memory. It attaches to the entire DOM watching for the event to bubble instead of attaching a new event to every object your're inserting every 60 seconds.

Nick Craver
i had to use mouseover, mouseout instead of hover - there's only 1 function parameter. are there limitations on which events work? I am having trouble with .scroll and .resize (resize on the window object).
@unknown: Only certain events work, see here for a complete list: http://api.jquery.com/live/ Note that jQuery 1.4 added support for many more event types to .live(), it's one of the big 1.4 features.
Nick Craver
Nick - I am able to replace mouseover type events, but I haven't found a way around recreating controls that "go away" on asp.net partial postback. a progressbar in particular, i need to re-create this every partial postback so I unbind, then instantiate (every 60 seconds) and I believe this is where my memory leak is. any way around this?
@unknown (yahoo) - You don't have to unbind with live...it actually listens up at the document level and will work for newly created elements as well...it's not tied to the elements your wiping out like `$("#item").click(` would be. Just don't unbind and you should be set.
Nick Craver
i am asking about how to handle this for items that live doesn't support. for example, I have to ("#myPB").undbind(); ("#myPB).progressbar(); to re-create progressbars every partial postback
A: 

Yes, it could be.

The first thing to try would be to take the two functions defined there (if possible) and place them in a higher level so that they are only defined once.

Kyle Butt