0

Possible Duplicate:
jQuery Memory Leak Suspicion

I noticed our website got slower and slower the longer the tab stayed open. Using Chrome's Task Manager, I narrowed down the problem:

Each time the following line of code is called the memory usage increases:

$jquery.post(url, params);

I implemented this code on a 1second timer, and within a few minutes memory usage had gone from 30MB to 250MB. Commenting out just this one line of code fixes the problem. Note that I only comment out this one line; I still let the URL and params be generated. So the problem is definitely to do with this line of code. Each time it runs about 2MB more of memory is consumed.

Clearly there is some sort of memleak. I expect it has something to do with caching or JSON deserialization of the results (the JSON results are potentially rather large).

Can anybody offer a work-around or solution? Even merely forcing some garbage collection would be acceptable. FWIW, the memory usage does occasionally decrease, though not by much (garbage collection?). I just saw it drop from 300MB to 250MB, but is now inching upward again.

More specifically, the code looks like this...

...

    init: function()
    {
        setInterval(function(){ SManager.updateAll(); }, 1000);
    },

    updateAll: function() 
    {
        var url = SManager.SERVER_URL;
        var params = new Object();
        params.version = 1;
        params.platform = 'web';

        $jquery.post(url, params);
    },

Thanks!

Community
  • 1
  • 1
Zane Claes
  • 14,732
  • 15
  • 74
  • 131

2 Answers2

1

Use a profiling tool to find large objects that do not get garbage-collected. E.g. in Chrome F12 / Profiling / Heap trace.

Tgr
  • 27,442
  • 12
  • 81
  • 118
  • Okay, I used the Profiles tab of Chrome to take a heap snapshot. According to the snapshot, the heap size is only ~= 5MB, even though the memory usage in the task manager is at > 100 MB. I'm going to go try to dig up some documentation to understand the relationship between the two, as I presently do not understand how memory could be consumed by the page but not in the heap... – Zane Claes Apr 17 '12 at 17:49
  • Maybe your connections are not terminating properly? You might be able to check that in the net tab, or just shut down the web server after a few requests, and see if the memory is freed. – Tgr Apr 17 '12 at 17:55
  • Also, if the URL is not the same each time (note that jQuery can append a cache breaking parameter to the URL even if your URL parameter is the same), there might be some sort of browser-side caching going on (though that would be unusual for a POST request). – Tgr Apr 17 '12 at 17:57
  • The network tab is showing a 200/OK on each of the requests, with an average response time ~= 500 MS. Browser-side caching sounds like a possibility. I just tried converting the $.post() to a $.ajax() call and setting cache:false, but the problem persists... – Zane Claes Apr 17 '12 at 18:04
  • cache:false means that jQuery will append something to each request to make sure they all use a different URL. That means the result never comes from the browser cache, but does not stop the browser from caching the request. You should set cache:true instead - if it is really the cache that leaks, that way new requests just overwrite the previous cache entry and the memory use should not grow after the first request. – Tgr Apr 17 '12 at 18:23
1

Similar issue with a solution on stackoverflow I believe the solution to your problem can be found here:

jQuery Memory Leak Suspicion

Community
  • 1
  • 1
dennmat
  • 2,618
  • 2
  • 17
  • 20