views:

119

answers:

3

Hi all

I'm running an AJAX request from a JavaScript-powered (+jQuery) webpage every 5 seconds for a set of JSON data. I left my application on overnight, and by morning my computer had completely frozen. I narrowed it down to my web browser and now, using Google Chrome's Resource Tracker, I can see that each request contributes a new memory expenditure, and the old JSON lingers.

As the source JSON is constantly changing, I call it with the timestamp as a parameter, to avoid caching... I realise caching would solve this problem, but it would also make my data invalid.

Any ideas? I'm overwriting the previous variable, so I don't see why the previous data should be retained. The memory increases don't happen at the same interval at the AJAX requests, so maybe its something else. I'd be happy to send someone the code privately, if it would help.

Thanks all :-)

Gausie

A: 

First, make sure that it is ajax request that causes the leak. Don't request this ajax every 5 seconds and check if memory still leaks.

If it is the request, maybe you overrite one variable, but you have another variable pointing at this data? Something like this:

var a = json_object;
var b = json_object;
//A lot of other code here
var a = json_object2;

json_object is still in the memory, var b points at it. If there is no var b, maybe you add it to some map or array? In such case map or array points at it.

amorfis
I'm investigating this now, although it doesn't look promising: all uses of the variable seem to be comparative at a first search. Either way, thanks for the idea
Gausie
A: 

It can't be answered cause you didn't provide some code sample. In general check for improper use of closure... Anyway - check this post http://www.crockford.com/javascript/memory/leak.html and use Google or provide some examples. Good luck!

NilColor
I realise this, but I don't know what part of the code to upload. If you'd like, I can send you over a copy privately?
Gausie
Try to remove/subst secret/private parts of code, put it somewhere online (for ex. http://jsbin.com/) and give us a link. This way some of us can check your code and try to help you. Community is a strong source of ideas ;)
NilColor
http://jsbin.com/asavuIs that the right kind of thing? I've never used jsbin before, and I don't know the form in which my submission can be most useful.
Gausie
+1  A: 

What are you doing with the data ?

It is probably not jquery's ajax the culprit.

Is you dom growing ? Do you have forgotten to declare a variable using the var prefix ? do you delete content using innerHTML = '' ?

Olivvv
I was missing the var from a couple of variables, so I'm testing behaviour with the var prefix in place. I usually delete content using .remove() or .empty() from jQuery. How can I tell if the DOM is growing?
Gausie
There is the DOM Monster plugin by Thomas Fuchs, but it is not free. Using the webdeveloper bar, you can do "view generated source", then paste that in some diff tool, and compare it with a later state. If your dom is not too big, firebug will be sufficient.But a missing var prefix is totally a good catch, so I'd advice you to first cleanup your app.
Olivvv
It seems to have fixed after adding the var prefixes. Thanks!
Gausie