ajax memory leak

2019-07-22 23:17发布

问题:

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 : 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?

回答1:

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.



回答2:

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.