• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2173
  • Last Modified:

Firefox AJAX Memory Leak

I asked a similar question to this yesterday, and by implimenting prototype I thought I had fixed it, but it looks like that was just in IE7.  I tested out my script in Firefox 2.0 and there is still a definite memory leak.

I made a multiplayer AJAX poker game, but I seem to have some problems with browser memory leaks.  I have the AJAX poll the server for data to update the table every 2 seconds and was wondering if anybody knew of some common or maybe not-so common issues in a setup like this that could cause excessive memory build up in the browser, or cause the user to be locked out of the site until they restart their browser.

Common symptoms are that the table just freezes, and then if they try and go to any page on the site it won't load, but any other site loads.  Then if they restart the browser everything works fine again.
0
jls33fsls
Asked:
jls33fsls
  • 7
  • 7
  • 2
  • +2
1 Solution
 
StingRaYCommented:
This is just my idea.

Make sure the process (making a request, taking charge of response, rendering or whatever) will be done within 2 seconds. I don't know how fast FF can render or access DOM response. If it is slower than you think, it may cause the bottleneck.
0
 
mreuringCommented:
Are you polling in sequence or in parallel?
Do you have a connection-pool so that you can protect your client from opening too many threads to the server?
If the above answers are parallel and no connection-pool it could be verry likely that you're polling faster than your resolving the requests and as a result build up too many threads to the server.

That at least would explain the symptoms :)
0
 
jls33fslsAuthor Commented:
I have checked that and they get done in under a second.  What do you mean by polling in sequence or parallel?

If you mean it might cause a bottleneck on the server then that isn't the case because it is just in the browser and doesn't affect other users.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mreuringCommented:
The webserver may have a max. request per user-client setting. If you're polling in parallel and have several request open at the same time you might hit that limit and the server may stop serving requests to the client. It won't affect other users because the whole safe-guard is to prevent other users from harm because one user is abusing the system.

Another option, but I wouldn't know whether this is valid or not, is that the polls might be falling close enough to each other to keep everything within the same thread, once the thread times out on the server the client doesn't open up a new one but actually 'hangs' itsself as towards that particular ip/website.

Both are scenarios that would not create a bottleneck by definition, but would create the symptoms you describe. Again, it's just theories, you would have to test or reason them out to gues their value...
0
 
jls33fslsAuthor Commented:
So how could I fix either of those?
0
 
mreuringCommented:
Well, first you could try and see if they occur by monitoring the connections to your webserver. If it's a timeout issue there oughta be an error logged for it.

For testing purposes upping the timeout between polls to see if that keeps the connection stable.

Implement a logging mechanism by which you count the ammound of polls you open and close, so that you are certain the amound of open requests is really stable and not slowly counting upwards.

Using a monitoring proxy such as charles (http://www.xk72.com/charles/) could also give you some insight into what's going on by being able to see what the last reponse of the server.

Also, I would give it a day orso and maybe even try to get the attention of Zvonko orso, they may have some insights in where else it might go wrong, it is just as likely a common implementation problem in JS...
0
 
jls33fslsAuthor Commented:
Well one thing I am wondering is why it would happen in Firefox and not IE, and also I just raised the max connections and it had no effect.  Don't see any timeout error log though.
0
 
jls33fslsAuthor Commented:
Also, I increased the polls to 10 seconds and had the same result.
0
 
scrathcyboyCommented:
How do you know that AJAX has adequately tested their product with Firefox?   Do you realize that 90% of websites and web devel. companies write for IE only, and turn a stupid blind eye to the better browsers that are available?  It is called burying your head in the sand, and I am endlessly outraged at how stupid almost all major web development companies are in writing to an IE only browser.  Then they wonder why they go out of business.  If you are certain AJAX is the problem, you should be on the phone harassing them to make them wake up that the world is not only coloured black -- there is some white out there too.
0
 
StingRaYCommented:
Hi,
I think it is FireFox issue, not web server. Is it possible to track step-by-step the problem by alert or print out something that what the browser is doing right now in detail with timestamp. This will help you see what happens.
0
 
mreuringCommented:
Problem is, the only known XHR bug that cause memory-leak in FireFox is this:
https://bugzilla.mozilla.org/show_bug.cgi?id=206520

In a nut-shell, the title of th bug says it all: XMLHttpRequest leaks memory if send() not called and event listeners set. Due to the nature of your applications' polling it seems unlikely that this bug is your culprit.

On the side then, maybe this tool can help you out: http://dbaron.org/mozilla/leak-monitor/
0
 
jls33fslsAuthor Commented:
Well I can see the leak happening while the page is open, though the memory does stay up when I close the page as well.  I can watch in the resource manager on Vista and every time the poll goes through the memory usage goes up a little, and it just does that as long as the window is open.

I don't think it is a bug in the browser, I think it is just some flawed coding in my application.  I keep seeing stuff about closures and things like that, but don't really understand what they are.  Could that be something that could be a cause of this?
0
 
mreuringCommented:
Excerpt stolen from http://www.jibbering.com/faq/faq_notes/closures.html:
Closure
    A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).

I could try and simplify the above mentioned link, but I really think that you'd benefit from this very elaborate article, to tell you the truth, I only read the core paragraphs to see if it wasn't lying and then printed it to give it a read myself, there's a few details in there that I don't fully comprehend or appreciate.

An alternative, which is explaining as much as I know about closures without going into the details (and thus actually it's a little less than I know and far less than the above link) http://blog.morrisjohns.com/javascript_closures_for_dummies which isn't really for dummy's as much as it is for programmer not intimately familiar with javascript yet.

Google on "javascript closure memory leaks firefox" and you get a fair list of articles on how to identify common closure patterns that cause leaks, or how to break some of the more common problems.

Examine the bugs listed on this page: http://plugindoc.mozdev.org/faqs/memusage.html one of which would be interresting to know for an application dealing with huge amound of xml-documents (however small they may be): Bug 241518 - calling addEventListener with a closure holding a content node leaks the document. Not cleaning up a closure such as this one could cause you quite some grief.

I think this is enough material to keep you busy for quite a while :)
0
 
jls33fslsAuthor Commented:
So it looks like they are saying the only way closures happen is if you declare a function inside of another function, which I don't do anywhere.
0
 
mreuringCommented:
Hmmm, you're using protoype for your XHR objects, they might (quite likely will) use a closure.

But, since you had this problem before you started using prototype, that doesn't seem to be a valid cause. If you don't have any closures you should start examining how you 'store' your game-data on the client. And more to the points, how do you clean up data that is no longer relevant?

Do you read xml-data from your server? Do you clean up all references to the resultion DOM-trees? Do you clean up references to nodes that you remove from your document? Have you run the application in an entirally clean FireFox install, without any extentions or custom skin.
0
 
jls33fslsAuthor Commented:
The only thing I do to 'clean up' is set things to null that are being changed with innerHTML.  Is there more that I should be doing?  And if it has to do with cleaning up stuff, why doesn't it affect IE?
0
 
mreuringCommented:
I know that innerHTML is actually one of the more risky methods available in JS-land. Might be that your problem lies in the use of innerHTML and has nothing to do with the XHR's at all.

I'd suggest running your application in some way without actually refreshing the content of the page and see if it then still leaks memory, then switch it around, refresh the innerHTML with pre-generated data, not sending and XHR's and see if that stops the leak.

You seem to be at a point where you really have to pick apart your app piece by piece till you find the bit that causes the problem. If you are certain about not using closures, we won't have to worry about that, the biggest two risks left are XHR and innerHTML, find a way to eliminate one as the source of your problem.

I must admit, I'm running out of rocks to turn over though!
0
 
Computer101Commented:
PAQed with no points refunded (of 500)

Computer101
EE Admin
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 7
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now