Memory leak involving jQuery Ajax requests

后端 未结 3 2070
攒了一身酷
攒了一身酷 2020-12-18 04:21

I have a webpage that\'s leaking memory in both IE8 and Firefox; the memory usage displayed in the Windows Process Explorer just keeps growing over time.

The followi

3条回答
  •  轮回少年
    2020-12-18 05:00

    I'm not sure why firefox isn't happy w/ this but I can say from experience that in IE6/7/8 you must set innerHTML = ""; on the object you want removed from the DOM. (if you created this DOM element dynamically that is)

    $("#content tbody").empty(); might not be releasing these dynamically generated DOM elements.

    Instead try something like the below (this is a jQuery plugin I wrote to solve the issue).

    jQuery.fn.removefromdom = function(s) {
        if (!this) return;
    
        var bin = $("#IELeakGarbageBin");
    
        if (!bin.get(0)) {
            bin = $("
    "); $("body").append(bin); } $(this).children().each( function() { bin.append(this); document.getElementById("IELeakGarbageBin").innerHTML = ""; } ); this.remove(); bin.append(this); document.getElementById("IELeakGarbageBin").innerHTML = ""; };

    You would call this like so: $("#content").removefromdom();

    The only issue here is that you need to re-create your table each time you want to build it.

    Also, if this does solve your issue in IE you can read more about this in a blog post that I wrote earlier this year when I came across the same problem.

    Edit I updated the plugin above to be 95% JavaScript free now so it's using more jQuery than the previous version. You will still notice that I have to use innerHTML because the jQuery function html(""); doesn't act the same for IE6/7/8

提交回复
热议问题