views:

277

answers:

5

I've just restarted my firefox web browser again because it started stuttering and slowing down. This happens every other day due to (my understanding) of excessive memory usage. I've noticed it takes 40M when it starts and then, by the time I notice slow down, it goes to 1G and my machine has nothing more to offer unless I close other applications. I'm trying to understand the technical reasons behind why its such a difficult problem to sol ve.

Mozilla have a page about high memory usage:

http://support.mozilla.com/en-US/kb/High+memory+usage

But I'm looking for a slightly more in depth and satisfying explanation. Not super technical but enough to give the issue more respect and please the crowd here.

Some questions I'm already pondering (they could be silly so take it easy):

  • When I close all tabs, why doesn't the memory usage go all the way down?
  • Why is there no limits on extensions/themes/plugins memory usage?
  • Why does the memory usage increase if it's left open for long periods of time?
  • Why are memory leaks so difficult to find and fix?

App and language agnostic answers also much appreciated.

A: 

Because firefox is a catastrophically badly designed piece of software.

Autopulated
I am sure you can backup this affirmation by writing a better browser.
Andrei Ciobanu
But how do you really feel about it?
David
Browsers are like people - they age, get bloated, and get ditched for younger and leaner models.
Uri
A: 

Wikipedia has a god article on Memory Leaks: http://en.wikipedia.org/wiki/Memory_leak

Macros
+8  A: 

Browsers are like people - they get old, they get bloated, and they get ditched for younger and leaner models.

Firefox is not just a browser, it's an ecosystem. While I feel that recent versions are quite bloated, the core product is generally stable.

However, firefox is an ecosystem/platform for:

1) Badly written plug-ins

2) Badly written JavaScript code that executes within it.

3) Adobe flash as a platform for heavyweight video and for poorly written ad scripts such as 'hit Osama bin Laden with a duck to reduce your mortgage rate and receive a free iPod* (participation required).

4) Quicktime and other media player.

5) Some embedded Java code.

The description of a memory leak suggests a script running amok or a third-party tool requesting more memory. If you ever run Flash on a Mac, that's almost a given along with 90% CPU utilization.

The goal of most programming languages is not to save you but to give you tools to save yourself. You can write bad and bloated code with memory leaks in any language, including ones with garbage collection. Third party tools are usually not as well tested as the platform itself. Web pages that try to do too much are also not uncommon.

If you want to do an experiment to demonstrate this, get a mac with Firefox and go to a well-written site like Stack Overflow and spend an hour. Your memory usage shouldn't grow much. Then spend 5 minutes visiting random pages on Myspace.

Now let me try and answer your questions based on my guesses since I'm not familiar with the source code

  • When I close all tabs, why doesn't the memory usage go all the way down?

Whereas each browser instance is an independent process with its own memory, the tabs in a single window are all within the same process. Firefox used to have some sort of in-memory caching and merely closing a tab doesn't clear the relevant information immediately from the in-memory cache. If you reopened a tab to the same site, you might get better performance. There was some advanced option to allow you to disable it, something like browser.cache.memory.enable. Or just search for disabling the memory cache.

* Why is there no limits on extensions/themes/plugins memory usage?

For the same reason that Windows or Linux doesn't have a vetting process on applications you can run on them. It's an open environment and you assume the risk. If you want an environment where applications and extensions are 'validated', Apple might be the way to go :)

* Why does the memory usage increase if it's left open for long periods of time?

Not all calculations and actions in a script have visual manifestations. A script could be doing some stuff in the background (like requesting extra materials, pre-fetching stuff, just bugs) even if you don't see it.

* Why are memory leaks so difficult to find and fix?

It's about bookkeeping. Think about every item you ever borrowed (even a pen) or that someone borrowed from you in your entire life. Are they all accounted for? Memory leaks are the same way (you borrow memory from the system), except that you pass items around. Then look at the stuff on your desk, did you leave anything lying around because 'you might need it soon' even though you probably won't? same story.

Uri
Nice bookkeeping analogy.
Warren Pena
@Warren: Thank you!
Uri
And I'm on a mac...
zaf
+2  A: 
  • Why are memory leaks so difficult to find and fix?

Because some developers refuse to use tools like Electric Fence.

Donal Fellows
Firefox devs use Valgrind - which is much more capable than Electric Fence.
slacker
Good for them. I've got good memories of using Valgrind to hunt memory problems. (Alas, my code had significant troubles at the time so the overall experience was painful, but that wasn't Valgrind's fault...)
Donal Fellows
+1  A: 

Memory leaks are present in the first place because you want to keep things in memory and not on disk. For example, suppose you have a web page, which have images, CSS, JavaSript, text. If to display the page you will go to the hard disk every time you want to use the JavaScript interpreter or a CSS parser, or a font rendering engine to display a text with, then the browser will be very slow and sometimes won't work at all (because one JavaScript piece might need variables present which are left by another JavaScript piece, for example). Therefore, a browser tries to keep all things necessary for its work in memory, and those things get cross-referenced easily (JavaScript calling into Adobe Flash, Adobe Flash calling into JavaScript and so on). And you have to be very careful with such resource references, because cleaning them prematurely and out of order will break the code (better to keep a resource around then to die suddenly because it isn't there).

ArtemGr