Memory leak using AJAX + jQuery queries

I repeatedly retrieve a JSON object from the server using AJAX calls. Over time, memory usage in the browser increases (used with Chrome, Safari, Firefox). Using Chrome heap snapshots, I found that timestamp strings are left without links. If I take a sequence of shots, I see that the number of lines is constantly increasing.

$(function() {
    var latestTimestamp = 0;

    function fetchData() {
        $.get("/parameter?format=json&since=" + latestTimestamp, gotData)
    }   

    function gotData(data) {
        latestTimestamp = data['timestamp'];
        setTimeout(fetchData, 250);
    }   

    fetchData();
});

Other notes:

  • I am using jQuery 1.7.1. EDIT: just tried with 1.6.2 and 1.4.2, the same problem.
  • timestampin the JSON object is actually an integer, not a string. So cumulative rows can be temporary values?
  • Removing + latestTimestampfrom an AJAX request stops the leak.
  • setTimeout (20 ) . , , 250 , .
+5
3

[], :

function gotData(data) {
    latestTimestamp = data['timestamp'];
    delete data;
    setTimeout(fetchData, 250);
} 
0

javascript cleartimeout? , .

var abc=null;
function gotData(data) {
latestTimestamp = data['timestamp'];
data=null;
clearTimeout(abc);
abc=setTimeout(fetchData, 250);
}
0

All Articles