Tuesday, October 6, 2009

A Simple Firefox 3 Hack to Fix Its Memory Leak

Firefox Logo
Firefox is likely the best browser out there. And in my humble opinion, it pretty much is. But that does not mean that it doesn't have its own share of issues. A quick search reveals how the open source browser is plagued with memory leak issues. Now, Firefox 2 may have been inundated with such memory issues, but the latest and greatest (FF 3.5.x) has resolved many of those issues.

Per Mozilla:

Of course, since Firefox is very extensible (with wide array of extensions, plug-ins, add-ons, etc.), it comes as no surprise that the application would run heavy at times. For most users, Firefox doesn't use an abnormally large amount of memory. For others, however, Firefox's memory consumption is a major problem.

There are many different memory problems, and ways to "patch" them helping you restore some of that much needed memory.

First, though, let's make sure we're all on the same page and define what are memory leaks. Whatis.com defines it as:
A memory leak is the gradual loss of available computer memory when a program (an application or part of the operating system) repeatedly fails to return memory that it has obtained for temporary use. As a result, the available memory for that application or that part of the operating system becomes exhausted and the program can no longer function. For a program that is frequently opened or called or that runs continuously, even a very small memory leak can eventually cause the program or the system to terminate. A memory leak is the result of a program bug.

Some operating systems provide memory leak detection so that a problem can be detected before an application or the operating system crashes. Some program development tools also provide automatic "housekeeping" for the developer. It is always the best programming practice to return memory and any temporary file to the operating system after the program no longer needs it.

Firefox Memory Leak

How do you know how much memory Firefox (or any other application) is specifically using?

Hit Ctrl+Alt+Delete in Windows 2000/XP (or Ctrl+Shift+Esc in Vista/Windows 7) to pop up the built-in Task Manager, which offers a pretty decent look at what programs you're running, as well as current CPU and memory usage for each and a basic description. I personally go straight to the "Processes" tab, and then click on "Image Name" to sort that column alphabetically. I scroll down to the F's and there's firefox.exe. The "Mem Usage" column, matching it to the row of the application you're seeking (Firefox in this case), shows you exactly how much memory that application is consuming in Kilobytes (KBs). If the number is in the thousands, as it will likely be, then simply divide it by 1,024 to know how much it's consuming in megabytes (MBs).

Understanding the columns in Vista/Window 7 can be confusing, so I urge you read "What do the Task Manager memory columns mean?" by Microsoft. How to optimize your system's memory, optimizing the task manager along with a few other tricks along the same line is the subject, I'm hoping, of a future Tip Tuesday on ahmadism.com

Getting back to the browser itself, it's worth mentioning that Firefox caches objects for future use. In addition, memory becomes fragmented as memory is repeatedly allocated and de-allocated. This general increase is normal.

Normal Firefox memory usage reported by Windows might be as high as 100-150 MBs. These numbers will vary because Firefox is configured by default to use more memory on systems that have more memory available and less on systems with less. If you experience substantially more usage than this, there may be a problem, or you may just be viewing pages with large amounts of data.

So could Firefox extensions, themes and plug-ins be causing this?

For extensions & themes (not plug-ins), observing memory usage while Firefox is in safe mode may help tell you whether one of your extensions or themes is causing memory problems. There is a list of extension-specific problems, which may be of benefit.

Firefox Addons
Plugins can cause Firefox to use more memory when they're used and may not release their memory until Firefox is closed. It's a good idea to make sure you're using the most recent version of every plugin. To save the maximum amount of memory, uninstall any unnecessary plugins. Or you can block/control specific instances of memory usage, such as for Flash Player, via extensions or configuration options. Flash images in particular are pervasive in modern web pages (as ads, videos, etc) and so are a common source of memory usage. Here's some plugin-specific information.

Reducing Adobe Reader memory usage

Aside from extensions, themes and plug-ins, what else could be consuming memory when using Firefox?

In addition to the settings you can make/modify to reduce memory usage (below), there's download history.
Memory usage can increase and Firefox can slow down or hang if the download history is allowed to accumulate. Clear the download history (you may need to exit Firefox and delete the file "downloads.rdf" from the profile folder in some very rare cases) and change this setting to circumvent the problem:

"Tools -> Options -> Privacy -> Download History -> Remove Files from the Download Manager", select: "Upon successful download".

So what settings can you change to reduce Firefox's memory usage?

Aaah, the trade secrets at last. Before I move on, it is essential that you understand that many (if not all) the changes suggested below require a change to the browser's configuration; which is like messing with Windows' registry and could render your browser useless in the worst case scenario. Changing the about:config preferences below may have an effect on memory consumption but may also affect performance or reduce functionality. Use at your own risk.

How to access the about:config preferences in Firefox 3

In Firefox, type about:config in the Location Bar (address bar) and press Enter to access the hidden configuration options and display the list of preferences, as shown below in Firefox 3. (If you see a page with the warning message, This might void your warranty!, click the button labeled "I'll be careful, I promise!", to continue.)

Firefox about:config

Firefox Config
To add a new preference, context-click (right-click) anywhere in the list of preferences. In the context menu, select New then select the type of preference you are adding.
  • String is any sequence of text
  • Integer is a number
  • Boolean is true-false

To modify an existing preference, context-click (right-click) on the preference, select Modify and type in the new value. In some cases, Toggle will be the selection offered for boolean (true-false) preferences.

To reset a preference to its default value or to remove an added preference, context-click (right-click) on the preference and select Reset. If you added the entry via about:config, the preference will no longer be listed after restarting the program.


Controls how much memory is used to cache pages. More accurately (and geeky), when images are loaded, they can be cached so they don't need to be decoded or uncompressed to be redisplayed. This preference controls the maximum amount of memory to use for caching decoded images and chrome (application user interface elements).

Right click on the page and choose New then Integer and create an integer called browser.cache.memory.capacity and set it to a low number. This controls how many kilobytes of memory are used for the browser’s cache.

The possible value of "-1" (without the quotes) means Firefox will automatically decide the maximum memory to use to cache decoded images and chrome based on the total amount of RAM.

"0" (the number zero, again without the quotes) means do not cache decoded images and chrome in memory. Any positive integer means the maximum amount of memory in KB to use to cache decoded images and chrome (1 MBs = 1024 KBs).

Caveats: browser.cache.memory.enable (see below) must be true for this preference to take effect. Lowering this value causes less memory to be used but also increases the load time of previously visited pages and dialogs. Raising it has the opposite effect.
To view current memory cache usage, put about:cache?device=memory in the Location Bar
This preference does not exist by default in applications other than Thunderbird.

What numbers do I recommend? Well, if you have 1 GBs of RAM installed in your machine, then try 32768 KBs as the memory cache integer. For 2 GBs, try 45056 KBs. And for 4 GBs, try 59392. If you have more than 4 GBs then you shouldn't be complaining about memory leaks ;)


This boolean preference (true or false) looks at when a page is loaded, it can be cached so it doesn't need to be rerendered to be redisplayed. This preference controls whether to use memory to cache decoded images, chrome (application user interface elements), and secure (https) pages. browser.cache.memory.capacity (above) controls the maximum amount of memory to use.

A value of "true" (without the quotes), which is the default value in Firefox, allows decoded images, chrome, and secure pages to be cached in memory. Whereas a value of "false" (without the quotes) means don't cache decoded images, chrome, and secure pages in memory.

Caveats: Setting this preference to false causes less memory to be used but also increases the load time of previously visited pages and dialogs, especially those of secure sites.
If browser.cache.disk_cache_ssl is set to true, secure pages will be stored in disk cache, not memory cache.


Pages that were recently visited are stored in memory in such a way that they don't have to be re-parsed (this is different from the memory cache). This improves performance when pressing Back and Forward.

This preference limits the maximum number of pages stored in memory per tab.

Like browser.cache.memory.capacity, a "-1" (without the quotes) will automatically determine the maximum amount of pages to store in memory based on the total amount of RAM. "-1" is the default value. "0" (the number zero without quotes) tells Firefox to not store any pages in memory. Whereas any positive integer up to 8 will indicate the number of pages to store in memory.

Lowering this value causes less memory to be used but also causes more previously visited pages to have to be re-parsed (thus increasing their load time). Raising it has the opposite effect. One page will on average take 4MB of memory. I personally recommend the value to be between 5 and 8, but if need be, it can go down to 2 or even 1.


Configure Firefox (Windows only) to swap memory to disk when minimized. This will make Firefox less responsive if you minimize/maximize a lot, but it is good if you need to free memory while multi-tasking to other applications.

Firefox Config Preferences

Right click on the page and choose New then Boolean and create a boolean called config.trim_on_minimize and set it to true. Now when you minimize the window it will free up memory.


Network link prefetch will download all pages with the rel=”prefetch” tag. This uses up more memory than you would otherwise think. Simply set network.prefetch-next to false. However, for sites that use prefetching, disabling this preference will decrease the amount of bandwidth used, but increase the time required to navigate sites.

Firefox Config Preference Change


When a page is loaded, it is cached so it doesn't need to be downloaded to be redisplayed. This preference controls the maximum amount of hard drive space to use for caching purposes. A positive integer means a maximum amount of hard drive space in KB to use for cache. Firefox's default is 50000 KBs. A value of "0" (the number zero without any quotes) means do not cache files on the hard drive. Note: browser.cache.disk.enable must be true for this preference to take effect.

Firefox Config Preferences

I know it must be odd that I'm even suggesting this, but an there are extensions out there that could help. Amongst such extensions is RAMBack, which allows the user to manually free memory, such as caches, that is usually used to increase performance. This gives the advantage of small cache sizes without having to always suffer the performance hit.

And finally the obvious ... If you find that Firefox's memory usage continues to grow after long periods of being open, you may want to consider periodically restarting Firefox to bring the memory usage back to reasonable levels. Close Firefox and select the Save and Quit option to reopen your tabs when Firefox restarts.

I'm curious how all of this helps. Share it with the rest of us in the comments.  ▣

Click here to see Tuesday-only posts.

No comments: