Friday, July 07, 2006

Firefox Memory Leak Solutions

Some number of weeks ago, Dwight teased me on his blog about how my system was handling Firefox's use of memory with all the extensions I am running.

If you are a Firefox user and not familiar with this issue, Firefox has a way of taking up a chunk of memory. In addition, if you open your Windows Task Manger and click the Processes tab and watch the firefox.exe Mem Usage value you might notice it keeps getting larger the longer you use Firefox.

My system is a XP SP2 Home running on 512MB system RAM, a 128MB AGP video card, and a AMD Athlon XP 2400+ processor on a Shuttle brand mini-pc systemboard. Not top of the line, but it is quite speedy for my home base pc needs. I've never noticed any slowdowns on my system or had any "memory usage" problems. Firefox always seemed peppy and I can run multiple apps without much performance worry.

So while tracking down some suspicious hard-drive activity the other day that was hitting my system performance at boot (more on that in a future post) I left the Windows Task Manager open so I could keep an eye on the running processes and Mem Usage.

Imagine my surprise to find firefox.exe eating up 135,408 K and still slowly-but-steadily creeping upward!

Now let me pause for a moment. If you (like me) aren't noticing any performance issues with Firefox or your system in general, then you probably don't need to worry about anything. Who really cares how much memory it needs and is using if it is working to your satisfaction.

I however, being a curious sysadmin, wanted to see if there were any Firefox tweaks that might cut down the Mem Usage. Even though it really wasn't needed.

After searching the web and finding a wealth of dubious hacks and tricks to deal with Firefox's memory, I went to the source for my best information discovery: Reducing memory usage - Firefox - MozillaZine Knowledge Base

On this page, the documentation lists several about:config level settings that may help:

browser.sessionhistory.max_total_viewers, and

So I started trying them. Here's what worked (and didn't) on my system.

browser.cache.memory.enable: This setting controls using memory to cache "decoded images, chrome (user GUI elements), and https pages. It is set at "true" by default. When I set this to "false" and not cache those elements, my firefox.exe mem usage dropped significantly! CAVEAT: I did notice that a few web-pages will not load under the "false" setting value. I just got a blank page. Swapping the value back to "true" allowed the pages to load. This is a known issue.

browser.cache.memory.capacity: Setting doesn't have an impact unless the one above is set to "true." My system has the default setting value at 65536. I knocked it down to 22528 to match the setting value for 512 MB physical RAM in the page description's table.

browser.sessionhistory.max_total_viewers: Controls how many recently viewed pages to store in memory so they load faster in the Back and Forward buttons. The default value is -1 which lets firefox automatically decide based on system memory (for me it would be 5 pages). I manually set this value to 3 pages to store, a reduction in two pages.

config.trim_on_minimize: This setting is not listed in about:config by default. You have to manually add this key and set the value to "true." I did. According to the documentation, it releases most of the memory in use by Firefox back to the Windows system when Firefox is minimized. This would then mean more memory would be available for use for other programs. I tried it and it didn't reduce any memory in use by Firefox. No change. There are a number of forum comments that suggest this feature is now disabled in Firefox and greater. Some users swear it still works. I just know it didn't do anything for me. So I removed it.

With the changes I made listed above, I was able to successfully get my firefox.exe Mem Usage value to hover about 55,000-60,000 K. It very rarely got much higher than that. The only "bad" thing was that I had to go back and toggle the "browser.cache.memory.enable" value back to "true" to load some web-pages.

So, what was my final configuration?

I ended up leaving the browser.sessionhistory.max_total_viewers value set on "3". I will leave the browser.cache.memory.enable value set to "true" as I'm not the only one using Firefox in the family and it is annoying to have to toggle the value back and forth. I also left my browser.cache.memory.capacity set to 22528 to match my physical RAM value. Not a big change, but I figure even these small tweaks can help a little bit. Your mileage may vary depending on your system and software. Right now the "Mem Usage" is still hovering in the 60,000-75,000 K range. Not bad from my previous 135,000 K range.

More Related Tips

If typing "about:config" in the browser bar is a hassle to get to the preferences page, you can make a bookmark for the about:config and place it on your bookmarks toolbar for quick-launch.

AboutConfig (ff) extension: simply adds an about:config button to your toolbar.

MR Tech About:About 2.2 extension adds a slew of "about:" option links to the Help menu. Handy as quick launch links to these more detailed Firefox configuration option windows

Preferential 0.8.1 extension adds a Tools item that opens the about:config items in a hierarchical "tree" style window as well as light documentation as to what each item does.

Leak Monitor 0.3.4 extension pops up an alert window describe when chrome windows close but leave code pointing at JavaScript objects. I used it for a while but you can't toggle it on/off and after a while, it was too annoying popping up all the time -- pesky bad-code writers. So I uninstalled it.

See you in the skies,


Jim Thompson said...

I'll give this a try to see if it works on Mozilla.

Claus said...

I notice that in my post I forgot to mention the version of Firefox I am using.

It is Firefox

PJ said...

Thanks for all the tips. Did not understand how you got to 22528 to match 512 MB physical RAM in browser.cache.memory.capacity.

Claus said...

You're welcome PJ, thanks for stopping by.

The way I read the Mozilla link posted on that entry (and I'm open to more learned readers' correction on this...)

If the browser.cache.memory.enable (b.c.m.enable) preference is set to "true" then:
a) if the browser.cache.memory.capacity (b.c.m.capacity) = "-1" the program will automatically set the "memory cache value" according to the little chart on that page.
b) if set to "0" then decoded images and chrome does not get cached in memory.
c) if any positive value (in KB) is entered, it uses that as the maximum memory cache limit.

Looking at the table, it appears the developers/programmers have somehow come up with some kind of "optimized" ratio of cached memory for Firefox (FF) to system memory. It makes sense because you probably wouldn't want FF to gobble up as much memory as it wanted, it might take it all. I'm not sure how they figured that "optimized" ratio out.

What that table actually works out to is

System RAM <--> Max FF mem. cache RAM (MB)
32 MB <--> 2 MB
64 MB <--> 4 MB
128 MB <--> 8 MB
256 MB <--> 14 MB
512 MB <--> 22 MB
1 GB <--> 32 MB
2 GB <--> 44 MB
4 GB <--> 58 MB

My "default" value found in that field was 65536 which would be equivalent to 64 MB RAM (maximum).

So the way I am understanding it, my "default" about:config load had the b.c.m.enable key set to "true" and the b.c.m.capacity = 65536 (not the -1 like it should be). Therefore FF was allowed to use up to 64 MB of my RAM for memory caching. YIKES! Again, IF I am understanding this correctly, then FF would keep creeping up in the memory usage until it hit that 64 MB limit. I don't like that idea since I only have 512MB system RAM on this pc.

By setting my b.c.m.capacity (according to the table) to the 22528 value, I'm forcing it to only creep up to 22 MB RAM for this particular type of caching activity. I could actually have set it lower, but it seems like a good balance for my personal system's needs. I should have also just been able to type in "-1" and achieve the same result. But I can relate to 22528, not as well with "-1".

Did that make sense?

Anonymous said...

found while googling. Thank you for writing down your experiences!