Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox specific memory increases in heavy AJAX app

I'm puzzled at why my app's memory increases so much faster in Firefox than other browsers.

Basicially, the app uses a decent amount of AJAX, with the basic action being to load new sets of posts that have an average level of HTML, and generally have large images. The total avg data amount per post (incl loaded images) is under 1MB, maybe 900k. Using jQuery 1.7.1.

In Chrome memory seems to be stable, but in Firefox, every post load results in about 20MB of new memory use. Since a lot of posts are loaded, you quickly get to over 1GB and even 1.4GB in memory, and things quickly grind to a halt.

Digging in on Firefox, I tried to eliminate closures and any extraneous variables using 'delete'. No big improvement. I then started removing functionality, and it seemed like EVERYTHING makes a contribution.

Removing ToolTips, some excessive re-loading of FB widgets (one comment widget per post), I made a big improvement, down to 10MB new memory per post.

But beyond that, I can't get much lower! Basically, if I just load the new html + images (again about 900k ) via $.post() each post adds ~8mb of new memory, even if the new images have "display:none". (also tried disabling firebug).

This is my first attempt at memory management, but this just seems like a lot of overhead, and odd since I don't think memory really increases like that in Chrome at all. Seems like I should get memory increases more in line with the amount of data loaded, not 10X! (or none at all like in Chrome would be nice...)

Is this really reasonable? Any ideas on where to look for problems or what I can do to further minimize this problem?

Thanks!

Update:

As Boris aptly observed, the memory increase is almost entirely due to images (at least 80%). But again the memory increase is much more (10x) the size of the images loaded. One other thing I learned using about:memory - if I simply open up a new empty tab, the memory drops quickly and almost all the added image related memory disappears. I'm guessing that's GC kicking in, and thus as Boris guesses, it seems it's likely a GC issue?

If that is the case, How might I investigate why it's not being triggered naturally only in FF? are there ways to trigger it in JS? As I mentioned, I tried to go through and remove closures...

One other thought, could binding events (via jQuery) to image elements instead of divs be bad? I thought jQuery dealt with all that stuff though.

like image 248
Phil_Ken_Sebben Avatar asked Feb 20 '12 18:02

Phil_Ken_Sebben


People also ask

Why does Firefox memory usage keep increasing?

Firefox may use more system resources if it's left open for long periods of time. A workaround for this is to periodically restart Firefox. You can configure Firefox to save your tabs and windows so that when you start it again, you can start where you left off.

How do I stop Firefox from hogging memory?

In Firefox Preferences, uncheck Use hardware acceleration when available. Go to about:memory and select Minimize memory usage.

Does Firefox have a memory leak?

Firefox memory leak issues can be caused by certain plugins or browser preferences. To troubleshoot the Firefox memory problem, you should check the browser's preferences and installed extensions. The browser also has a handy memory usage option that you can use.

Does Firefox use more RAM than Chrome?

Coming in at number 3 is Mozilla's popular and privacy-minded browser, Firefox. And no, despite the browser's reputation for efficiency, it can take up almost just as much RAM as Chrome. When tested with 10 tabs open, Firefox occupied about 960MBs of memory, which is only slightly less than Chrome.


3 Answers

I have seen mine hit 2gb of ram

I have a widget in my dock which triggers this command which is saved in a file:

killall -c firefox
sleep 1
open -a Firefox
sleep 1
exit

Quick way to reset FF and get all your tabs back. Need to do this most days :) If it weren't for Firebug I'd be on Chrome by now

like image 152
BobB Avatar answered Oct 18 '22 08:10

BobB


If you're really using a lot of ajax, you can try setting $.ajax() option "cache" to false. It would have to be large amounts of data, though, to make an impact on browser memory.

For performance reasons, you usually want to do as much manipulation as possible in memory, and write to the DOM as few times as possible.

It could just be that the version of FF you're using isn't reclaiming memory as well as other browsers you're comparing it to.

It doesn't seem to me like closures would be the culprit. That seems more like an old IE problem.

If it's really an issue, you can try explicitly destroying large objects when you know they're not being used anymore.

like image 22
Ringo Avatar answered Oct 18 '22 08:10

Ringo


not necessarily an answer, but are you having these issues with the latest version of Firefox (13)? There have been a lot of memory and performance related issues with this release and mozilla have said that they are working to repair them.

Also, if you have a lot of items loading on page load and aren't displaying them all initially you could use jquery's waypoints plugin to dynamically load these images. It would relieve a lot of the weight the browser feels on page load.

Hope any of this was helpful. here is some reading material that may help you further:

http://support.mozilla.org/en-US/kb/firefox-slow-how-make-it-faster http://support.mozilla.org/en-US/kb/firefox-uses-too-much-memory-ram

and http://imakewebthings.com/jquery-waypoints/

like image 39
nate63 Avatar answered Oct 18 '22 08:10

nate63