Tuesday, March 19, 2013

When you browse the web for long periods of time without restarting Firefox, or launching a fresh instance of the browser, the memory usage can go through the roof, bringing Firefox to a crawl. This is not specifically a Firefox problem. It can affect all browsers including Chrome and Opera. To prevent this, it is recommended to periodically restart the browser before memory usage becomes too high. There is even a handy add-on that lets you automatically restart the browser keeping all open tabs intact. But you can avoid having to restart if you adhere to the following best practices.

Go easy on extensions

Keep the number of add-ons installed to a minimum. Periodically review the add-ons you have installed, say every 3 month or every month, depending on how frequently you install new extensions. Justify the presence of each extension and remove anything that you haven't used in a while, or those whose usefulness is over. Prune down the list as much as possible.

Determine which extension is causing Firefox to use too much memory and replace it. Using the about:addons-memory extension to observe their memory usage and remove the ones that use more memory than it should. Then try to find an alternative, if you really need their function.

Use restartless add-ons instead of regular ones, because restartless add-ons can be enabled and disabled as needed, on-the-fly, without restarting the browser. This helps to keep memory usage down. There is no setting on Firefox’s website that allows you to filter add-ons by restartless, so finding them may be bit difficult.

Another way is to use bookmarklets instead of add-ons. For instance, you can use the Readability bookmarklet rather than the extension to make pages readable.

Don’t forget plug-ins

Firefox displays add-ons and plug-ins on two separate pages. While you are reviewing your list of add-ons, don’t forget to checkout the plug-ins page. If you have never opened this page before, you might be on for a surprise.

Dozens of plug-ins, many of which you have never installed on purpose. Click the disable button next to each of these plug-ins. Don’t give a second thought. You don’t need any of these on 99% of websites, except Flash maybe.

Enable hardware acceleration

For certain plugins that play videos such as Flash, the content rendering in full screen can be accelerated by hardware. It eases memory usage in case you have a dedicated graphic card memory.

To check whether hardware acceleration is enabled in your browser, type about:config in Firefox’s address bar and press enter. Search for gfx.direct2d.disabled. When the value of this parameter is false, hardware acceleration is enabled.

Go easy on tabs

The surest way to bring down memory consumption is to open as few tabs as possible. To limit yourself to a specific number of tabs per session, you can use the Max Tabs add-on. It allows you to set a maximum number of tabs that can be opened in a window. The extension keeps count of the number of opened tabs on the lower-right on the add-on bar. If you try to go over the limit, Max Tabs displays a message “You’ve decided not to open more than x number of tabs” and promptly closes the new tab.

Max Tabs is a restartless add-on, so you can quickly disable it should you need to open more tabs than you promised.

Reduce memory cache

When you open a webpage with images, Firefox stores the images for a time in uncompressed form. This results in an improvement in speed at the expense of greater memory use. If too many large, uncompressed images are in memory simultaneously, it can lead to increased memory use. Web sites with a lot of large images can require very large amounts of memory. This problem can be mitigated by putting a cap to the amount of memory used by decoded images.

To do so, open the about:config page and search for image.mem.max_decoded_image_kb. By default, this is set to a very high value – 256000 or ~256 MB. Reduce this cache to something smaller, such as 50MB.

Similarly, memory assigned to Javascript is way too high. In webpages where there are lots of scripts doing lots of fancy things, memory footprint can increase quickly. To reduce memory consumption by Javascript, locate the parameter javascript.options.mem.max and change its value from -1 (automatic) to 51200 (50 MB).

Also find javascript.options.mem.high_water_mark, and set it to 30 from the default 128. This parameter tells the garbage collector to start running when javascript is using 30 MB of memory. Garbage collection releases memory back to the system.

Feel free to experiment with other values. Your needs may vary.

Reduce session history

Firefox caches each tab you have open in such a way that they don't have to be re-parsed when you use the back and forward buttons to flip through history. This cache is different from the memory cache. The default is set to 50, meaning Firefox will remember the last 50 pages opened by each tab. If you have the habit of reusing tabs (as opposed to opening new tabs), the browser session history can grow to fantastic sizes.

So what you should do? Close the tab when you are done with a page and open a fresh tab for a new page. Alternatively, you can reduce the number of pages Firefox keeps in cache. To do this, open about:config and set the desired value in browser.sessionhistory.max_entries. A value of 10 should do.

Firefox tweaks that don’t work

You’ll find plenty of guides on the web recommending various tweaks to reduce memory use, but not all of them are effective. Some offers little to no improvement on memory consumption, but can have a negative impact on Firefox’s performance. Others are no longer relevant.

browser.cache.memory.enable and browser.cache.memory.capacity are two about:config parameters that are now obsolete. The first parameter tells Firefox whether to use a memory cache for pages and the second parameter tells the browser how much memory should it set for the cache. Current version of Firefox no longer use these parameters.

Instead, image.mem.max_decoded_image_kb is used (see previous section). Creating this two parameters will have no effect on how Firefox handles memory.