0

I'm experiencing a memory leak with jQuery.ajax(). I've followed the suggestions here and here, but still see memory leaking. I'm essentially trying to replace the Google Visualization API with my own API, because GV API also leaks memory. Here's a snippet of my code:

RiseVision.Common.Visualization.Query = function(url) {
    this.baseURL = url + "&tqx=responseHandler:setResponse";
    this.url = this.baseURL;
}
RiseVision.Common.Visualization.Query.prototype.send = function(callback) {
    var self = this;

    this.callback = callback;
    this.hasResponded = true;
    this.timerID = setInterval(function() {
        if (self.hasResponded) {
            self.hasResponded = false;
            self.sendQuery();
        }
    }, this.refreshInterval);
}
RiseVision.Common.Visualization.Query.prototype.sendQuery = function() {
    var request = $.ajax({ 
        url: this.url,
        context: this,
        jsonpCallback: "setResponse",
        dataType: "jsonp",
        success: function(json) {
            var response = new RiseVision.Common.Visualization.QueryResponse(json);

            if (json.sig != null) {
                this.url = this.baseURL + ";sig:" + json.sig;
            }

            if ((response.getStatus() == "error") && (response.getReasons() == "not_modified")) {
            }
            else {
                this.callback(response);
            }

            response = null;
            this.hasResponded = true;
        }
    });

    //This is supposed to mitigate the memory leak.
    request.abort = null;
    request = null;
}

I'm using jQuery 1.8. The refreshInterval is 1 second because it's pulling real-time data. What I'm seeing in Chrome's Task Manager is a gradual increase in memory. After about 5 minutes, garbage collection seems to kick in and memory drops. The problem is that memory is not dropping to the same level at which it started from. For example, memory starts at 50K, then gradually increases to 60K, then garbage collection kicks in and memory drops to 52K. Now it increases to 62K, then drops to 54K. And so on. Eventually, this is going to crash the browser.

I've tried setTimeout instead of setInterval. That seems worse. I've tried putting the success handler into its own function. Didn't help.

Here's a jsfiddle to illustrate the problem - http://jsfiddle.net/aADXq/7/

Any suggestions at all?

Thx.

Community
  • 1
  • 1
donnapep
  • 500
  • 6
  • 19
  • Not an answer, but you could try this memory leak detection method using chrome developer tool : http://gent.ilcore.com/2011/08/finding-memory-leaks.html – sdespont Sep 19 '12 at 15:41

1 Answers1

0

Change the dataType in your ajax requests to 'text', then parse the resulting string with json_parse.js. This library parses the json recursively without using eval which is what's eating up your memory.

Itamar
  • 81
  • 1
  • 6