Keeping Firefox Memory Usage Under Control

My laptop has 12 GB of memory and is largely used for web, email, IRC, and ssh to other systems. You might think that would be sufficient. You would be wrong.

In fact, after a week or two of uptime I would find myself running short on memory and things would start to slow down. The primary culprit was Firefox, which uses approximately an order of magnitude more memory than any other single process on my system. To some extent this is understandable since browsers have become essentially nested operating systems and some of the more hefty web apps (looking at you, Google docs) can cause them to eat up a lot of memory.

But this is also the problem. If I have to restart my email or IRC clients it's not a big deal. They're one window applications that easily pick back up where you left off. Restarting my browser is almost as painful as restarting the entire system. As of this writing I have 8 separate browser windows open, with an average of probably 3 tabs per window. When I restart my browser it does restore all those windows, but it does so on one desktop which means I have to move them all back to one of the six virtual desktops that I use. This is time-consuming and annoying.

At one point I discovered the about:memory page and the Minimize memory usage button therein. Brilliant, I thought! I push that button and Firefox's memory usage drops. This does help somewhat, but unfortunately it doesn't take long for the memory usage to go right back where it was.

There's always the possibility of a memory leak in Firefox or one of my extensions, but this has been an ongoing problem for years and I'm not running any particularly exotic extensions. Chances are that if there were a serious memory leak it would have been fixed by now. Also, closing windows/tabs did seem to help, but eventually the memory usage just grew too large for my laptop to handle.

So I started looking for ways to reduce Firefox's long-term memory usage. Since this was something that happened over time and did seem to be related to the number of windows/tabs/sites I opened in Firefox I looked for history-related settings. And I found some! So far these seem to have levelled off my browser memory usage such that if goes up when I have a lot of windows open and goes back down once I close them. No longer is the steady state memory usage of my browser larger than my available RAM.

The settings I tweaked were related to the session restore feature. Here are the settings I overrode in the about:config page:

browser.sessionhistory.max_entries: 25
browser.sessionhistory.max_total_viewers: 3
browser.sessionstore.max_serialize_back: 5
browser.sessionstore.max_serialize_forward: 3
browser.sessionstore.max_tabs_undo: 3
browser.sessionstore.max_windows_undo: 1

You can find details about most of these on the link above. It is important to note that this does not break session restore in any way. I've successfully restored my session multiple times since changing these values and all of my windows and tabs came back. These values only seem to affect live use of the browser to limit how many pages you can go back before having to re-render the page or how many tabs/windows you can Undo Close on. Since I rarely undo more than one tab (and never undo entire windows) these settings have had no negative effect on my browser usage at all (as far as I can tell).

Most importantly, my browser memory usage is now at a sustainable level and has been for nearly a month now. I don't think I've ever been able to leave one browser sesion open that long before. If I open a bunch of new tabs or windows the usage will grow temporarily, but it eventually returns to an acceptable level once I close the extra pages. This is how it should work.

Again, at this point I don't think there is any bug in Firefox. It's just that under heavy usage the default settings cause quite a lot of overhead related to previously closed windows, tabs, and pages. In my case that overhead pushed my memory usage too far and was not providing me any significant benefit. If I had double the memory that I do I probably would never have noticed this because the memory usage would have plateaued at a sustainable level. If I didn't open so many windows and tabs over the course of a day I probably wouldn't have noticed this either. But because I fall in a weird intersection of heavy usage and lightweight hardware some tweaking was required to avoid mandatory periodic restarts.

Hopefully this will help someone else deal with their browser memory issues.