Memory leak involving jQuery Ajax requests

后端 未结 3 2078
攒了一身酷
攒了一身酷 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 04:56

    Please correct me if I'm wrong here but doesn't SetTimeout(fn) prevents the release of the caller's memory space? So that all variables/memory allocated during the resetTable(rows) method would remain allocated until the loop finished?

    If that's the case, pushing string construction and appendTo logic to a different method may be a little better since those objects would get freed up after each calling and only the memory of the returned value (in this case either the string markup or nothing if the new method did the appendTo()) would remain in memory.

    In essence:

    Initial Call to kickoff

    -> Calls resetTable()

    -> -> SetTimeout Calls kickoff again

    -> -> -> Calls resetTable() again

    -> -> -> -> Continue until Infinite

    If the code never truly resolves, the tree would continue to grow.

    An alternative way of freeing up some memory based on this would be like the below code:

    function resetTable(rows) {
        appendRows(rows);
        setTimeout(kickoff, 2000);
    }
    function appendRows(rows)
    {
        var rowMarkup = '';
        var length = rows.length
        var row;
    
        for (i = 0; i < length; i++)
        {
            row = rows[i];
            rowMarkup += ""
                    + "" + row.mpe_name + ""
                    + "" + row.bin + ""
                    + "" + row.request_time + ""
                    + "" + row.filtered_delta + ""
                    + "" + row.failed_delta + ""
                    + "";      
        }
    
        $("#content tbody").html(rowMarkup);
    }
    

    This will append the markup to your tbody and then finish that part of the stack. I am pretty sure that each iteration of "rows" will still remain in memory; however, the markup string and such should free up eventually.

    Again...it's been a while since I've looked at SetTimeout at this low level so I could be completely wrong here. In anycase, this won't remove the leak and only possibly decrease the rate of growth. It depends on how the garbage collector of the JavaScript engines in use deal with SetTimeout loops like you have here.

提交回复
热议问题