Slashdot Mirror


Reducing Firefox's Memory Use

An anonymous reader writes "Many people have complained about Firefox's memory use. Federico Mena-Quintero has a proposal for reducing the amount of memory used to store images, which, in his proof of concept code, 'reduced the cumulative memory usage... by a factor of 5.5.'."

17 of 110 comments (clear)

  1. Easier solution by plover · · Score: 4, Insightful
    Buy more memory. It seems to be the rest of the industry's answer to resource hogging software. Look at all the bloatware out there: XML, JVMs, .NET, etc. The rest of the world is building for 1GB boxes, so who cares how efficient their code is anymore?

    Coming from me, this is sarcasm, but it's a depressingly prevalent real attitude in the industry.

    --
    John
    1. Re:Easier solution by GigsVT · · Score: 3, Insightful

      With CPU development practically stagnating, but RAM and HD storage still growing fast

      What?

      I could believe HD, but RAM sizes have not kept up at all. You might have gotten a system with 128 megs with a 20 gig hard disk with a CPU running 800 mhz a few years ago. These days you get a system generally with something like 512 megs and 200 gigs storage running 3Ghz. Also RAM prices have not dropped all that much. 512megs of DDR2 is over $200.

      Yes, CPU speeds have stagnated in the last year or so, only growing by 10-20%, but RAM hasn't advanced all that much in the size department.

      --
      I've had enough abrasive sigs. Kittens are cute and fuzzy.
    2. Re:Easier solution by Guspaz · · Score: 5, Informative

      Firefox (on Windows) can and will suck up an infinite amount of memory. This is because under some circumstances (Well, always, at least for me and many other users) it does NOT remove the uncompressed images from memory when a tab is closed.

      If I am opening and closing a lot of image-heavy tabs, after a while, my firefox instance is sucking up 800MB of system memory, and the ONLY way to free it is to restart firefox.

      I don't care about firefox's memory usage with compressed versus uncompressed. If I'll get more speed with 90MB of uncompressed images, go for it. What I do have a problem with is how it doesn't bother to remove raw images that are no longer needed. Essentially, it is a really bad memory leak that they haven't fixed for ages.

      As for reducing actual memory usage, a hybrid solution is best. At the very least all images on other tabs should remain compressed, and then decompressed when switching to that tab, going back to the compressed images from the old tab (Disk cache them, or keep both compressed and uncompressed in memory).

      In addition, you can probably do smart-cacheing on images on the current tab. As the article author mentioned, keep uncompressed copies of only images near the current viewport. Another solution might be to store everything as compressed, even in the current tab, and modify the rendering engine so that images are drawn asynchronously. A 100ms delay while scrolling will cause noticeable hitching, but if you draw the rest of that page and throw in the image 100ms later, the user will have a much smoother experience. They can keep scrolling while the image is loaded in.

    3. Re:Easier solution by plover · · Score: 3, Funny

      You call it a leak. They call it a cache. Semantics, really. :-)

      --
      John
    4. Re:Easier solution by jonadab · · Score: 3, Interesting

      > What I do have a problem with is how it doesn't bother to remove raw images that are no longer
      > needed. Essentially, it is a really bad memory leak that they haven't fixed for ages.

      This is *partly* due to the way memory allocation and freeing work at the system call level. In a nutshell, memory that you free does not actually become free for other programs to use until your process exits. (As bad as this sounds, it's preferable to the situation wherein the system doesn't know what process owns the memory, so if an app has a leak the only way to reclaim it is to restart the whole system.) I am not sure which platforms have this (wait until the app exits) behavior, but I think it might be fairly common, although on some systems the not-really-free memory may be given to the *same* process again, next time it requests more memory, and in that case it shouldn't grow infinitely, as it would be only *other* processes that can't use it. Still, it's annoying that once you've loaded up too much at once and grown the process' memory usage to a large figure, the only way to reclaim that for other apps is to exit the process -- closing the document that caused all the usage won't do it. I *think* this is the behavior I see on Ubuntu, e.g., once I've had a really complex SVG image open in Inkscape (or a big document in OpenOffice with a lot of frames and especially images in it), and I then open a smaller, simpler image (or document) and close the big one, the memory doesn't get freed until I close Inkscape. However, if I've had a big document open, which I've closed, and then I open another big document, the memory usage doesn't go up any worse (unless the second document is bigger or more complex than the first one, of course), so it appears the same process is handed that same memory again that it freed earlier, even though another process can't have it.

      I *suspect* the same is true on Win NT/2K/XP, although it's harder to tell there, due to the lack of facilities such as panel applets and good process management tools. There's the mem command, but I haven't spent a lot of time on tracking memory usage that way. On the other hand, part of the reason I haven't spent time figuring it out is because memory that's not in usage any more on Windows systems usually ends up just growing the swapfile automatically, which is really not such a big deal if you've got plenty of hard drive space. I *mostly* like the way Linux handles virtual memory better than Windows (in particular, it (since circa 2.4, or maybe it was in 2.2) seems to do a quite good job of knowing what to swap out or not to keep thrashing to a minimum), but I do like the auto-allocation that Windows does.

      --
      Cut that out, or I will ship you to Norilsk in a box.
    5. Re:Easier solution by mallie_mcg · · Score: 3, Interesting

      I understand your point, but how about something as simple as a "cleanup memory" button. I too often have several tabs open (4 instances right now with 24 tabs) but most of those tabs will sit idle most of the time, like my PHP/MySQL/M-W.com windows that are mainly used for occasional reference. Being able to force a cleanup would be good. I'd still save the time to reload the entire page, even if it does take a second to redraw when I flip back to the tab.

      Great for people like us, who knows whats going on in the background. Many people never run into the firefox memory issues because they tend to single process with their machines, and switch their machines off regularly. The solution should not be a manual thing, it should be able to be solved with clever programming (perhaps an idle, lack of mouse movement detection?) its hard enough to train users to use computers to do their jobs, without adding extra load to their fragile little minds. M

      --


      Do the following really mean anything? SCSA MCP CCSA CCNA
      --I'm not actually after an answer!
  2. X extension by obi · · Score: 3, Interesting

    He gives three possibilities: storing it uncompressed in the server, storing it uncompressed in the client, and storing it compressed in the client (and uncompressing it on the fly).

    I wonder if it might be interesting and worthwile to have an X extension to store it compressed on the server? That way there's a lot less X traffic, and potentially a lot more applications could make use of it.

    The only condition is that you don't need to decompress in Moz, and recompress it to send to the X server, but just pass along the compressed data (there's some security implications with that though, but I guess they could be dealt with).

    1. Re:X extension by emj · · Score: 3, Interesting

      The problem is that to store images compressed on the server you would have to decompress, resize, compress and then store it on the server, and you would still hav to keep the original in the client. Lots of the pictures on the web are resized before being shown by the browser.

    2. Re:X extension by theCoder · · Score: 3, Interesting

      Well, along with that, why couldn't the X server compress the image itself, independent of the app? There wouldn't need to be any changes to the X protocol, this would be something done internal to the server. I suppose there could be an extension to the protocol that allows XPixmaps to be sent in JPEG compressed format or something as well, to reduce transmission time. If this was done, then apps like Firefox wouldn't have to change to get the benifit the author is describing.

      Besides, I'm not sure that storing the images compressed on the client side is going to work as well as the author hopes. In fact, it would increase the RSS of the firefox app, making people think that FF is even more bloated, even though it reduced memory usage overall. How many people have even heard of xrestop (I hadn't until I read the article)?

      --
      "Save the whales, feed the hungry, free the mallocs" -- author unknown
  3. It's the GDI objects by HughsOnFirst · · Score: 4, Interesting

    Windows has something called GDI objects (graphics device interface objects), and firefox uses too damn many of them.

    Somewhere after 5000 of them are in use windows slows down to a crawl and dies no matter how much memory you have, and with enough tabs and windows open firefox will be using 4000+ of them all by itself.

  4. Synchronicity... by benjamindees · · Score: 4, Interesting
    I'm right now running a copy of Opera on a system that's intentionally limited to 64 megs of RAM. It's working beautifully.

    I'm testing out browsers for use on some old machines as web kiosks. Basically, my choices are:

    • Konqueror - includes all of KDE (ugh)
    • Konqueror embedded - lacks maintenance
    • Firefox - seems to be slow and has issues when run without a window manager
    • Dillo - has website layout problems
    • and, Opera - seems to be the best choice


    These machines (P1), and lots of machines like them, pretty much max out at about 64 megs of RAM. I could probably find more RAM, but it'd be costly, and there are usually hardware compatibility problems.

    Although I'm leaning towards Opera at the moment, I was using Konqueror for a while. Linux does a great job of swapping, and Konqueror is quite snappy, so even with low memory it's a viable option. But, with all the libraries that Konqueror requires, 64 megs is kind of pushing it.

    And there is a decided trend in hardware towards less memory and faster processors. It's not uncommon to find Pentium III's with only 128 megs of RAM. Unfortunately, many open source programs are written without limited memory requirements in mind.

    It's kind of humbling to think that, as few as five years ago, a Pentium I with 64 megs of RAM would run an entire OS and web browser without so much as touching swap space. Today, you have to use apps designed for embedded machines to run in 64 megs of RAM, and you're lucky if you can run more than one app at a time.

    From my testing, Firefox is barely outside the range of viable options for a machine with 64 megs of RAM. But as with any performance tuning, there are probably trade-offs. And having lots of options is usually the best strategy. But I think these improvements suggested for Firefox would be beneficial in almost any scenario. Avoiding I/O seems to be the best strategy on any system newer than, say, a Pentium I, when web browsing. So uncompressing images on the fly in exchange for less memory usage would doubtlessly be a good trade-off.
    --
    "I assumed blithely that there were no elves out there in the darkness"
    1. Re:Synchronicity... by brunes69 · · Score: 3, Informative

      Konqueror - includes all of KDE (ugh)
      Konqueror embedded - lacks maintenance

      These are are both false statements.

      For one, you don't need "all of KDE" to build and run Konqeuror. All you need is the kdecore libraries, all of which put together have a much smaller footprint both in memory and diskspace than Firefox. If you don't beleive me, 'apt-get install konqueror' in Debian or any other distro that segments up KDE packages.

      For the second, Konqeuror embedded is built from the *exact same* cvs tree as Konqueror. Any commits to the rendering engine go to both browsers. So it does not 'lack maitenence', it is very actively developed, just like Konqueror.

  5. Interesting, but doesnt solve the biggest problem by meanfriend · · Score: 4, Interesting

    This was a nifty piece of investigation, but doesnt address the largest cause of firefox memory usage. Namely, memory is not freed when tabs are closed.

    See:

    https://bugzilla.mozilla.org/show_bug.cgi?id=13145 6

    Try a test. Fire up a clean FF and note memory usage. Go to somewhere like fark.com and open 50 links in tabs and note mem usage. Close every tab and see if mem usage goes down. It doesnt. Most people visit dozens of pages a day. Hundreds per week. After a while, the memory footprint of FF can grow to epic proportions (ie hundreds of megs) even with only a few tabs open because FF cannot release memory of closed tabs. I have to restart FF every week or so because I'm tired of it using 200MB for no good reason.

    It doesnt bother me so much that FF stores uncompressed images for tabs which are active (ie. open, even if not visible). The article itself mentions a performance hit when storing compressed images. But why the f*** cant it free the memory when I close that tab? The fact that I explicity closed it should indicate that I dont want it anymore. FF developers have acknowledged the problem but have said that there is no easy fix. Probably a poor design in the underlying architecture, though no one associated with the project would state it that bluntly.

    BTW, this article reminds me of one of the best reasons to use some sort of adblocking software. You save quite a bit of memory when you arent caching a dozen useless images with every new web page you visit. Especially in light of the above bug, you can significantly slow down the expanding memory footprint with adblocking.

  6. Don't try to play Operating System by Clueless+Moron · · Score: 4, Insightful
    While it looks like a nifty idea at first glance, this kind of memory optimization is ultimately pointless when you have a nifty demand-paged vmem kernel like Linux.

    Consider: since my box has 1G of memory, I do want the X server to hang on the all those pixmaps, because that makes firefax run fast. The hack would make it waste CPU time re-uncompressing images, whether it's needed or not.

    With the way Firefox works now, if memory does start to run short, well, that's when the kernel will start paging things out based on its clever working set algorithms. If a given pixmap area in the X server hasn't been accessed in quite a while, it'll get swapped out to disk and the memory reclaimed. If the pixmap is accessed later, it'll automatically page back in.

    I don't know about your box, but mine (Athlon XP2000+) can decompress JPEGs at a rate of around only 3MB per second. My disk drives, OTOH, are a hell of a lot faster than that.

    In other words, letting the OS do its job by tossing the images onto swap when necessary strikes me as a much better strategy than constantly sucking up CPU decompressing every image every time it's used just in case the memory might be needed.

    People worry too much about VMEM, IMHO. If I write a program that allocates 1G of memory, but then spins around using only 10k for the next hour, it'll have basically zero impact on the OS. Only ~10k if real RAM is actually getting used.

  7. Re:Interesting, but doesnt solve the biggest probl by Skapare · · Score: 3, Insightful

    I've switched to opening links in new windows a lot now. In part this is because I want to group a set of tabs together. And it's easier to just close the whole mess by closing that whole window (generally all on one site or about one topic). But it seems FF is not free-ing up memory in these cases, either.

    I don't see why a tab or a new window should be different internally, though. It should only be a matter of associating the state of a loaded page with a given display context. The real issue, though, is the memory management issue. Apparently something fundamentally wrong in the browser architecture is preventing that. I highly suspect it is due to over-abstraction and/or the inability of some tools they are using to properly destruct objects that are no longer needed. It does seem that large complex software projects such as this do tend to suffer a lot of complexity issues that result in basic things like free-ing memory becoming impossible to do. I don't encounter these problems in my programs, but then, I don't do anything nearly as large as Firefox, nor do I use a team of developers, nor do I use all these abstract tools by ignoring their internal operation implementations. I'll be curious as to the actual, real cause.

    --
    now we need to go OSS in diesel cars
  8. Scattered memory allocations by Skapare · · Score: 4, Interesting

    One possible contributor to the memory issue here, and in some other programs, could be the way the memory is allocated. Memory is obtained from the kernel in chunks a multiple of a page size. These pages cannot be returned back to the kernel unless all usage of the entire page is gone. Memory usage for a typical object tends to be small pieces. If the pieces allocated for one page (in one tab) are interleaved with pieces allocated for another page (in another tab), then closing one of those tabs, even if the mainline code destructs all objects which correctly free all their underlying memory allocations, does not necessarily result in pages being released back to the kernel.

    So how can memory allocations get scattered around like that? Consider that many objects need to persist as long as the page exists, but many others can be destructed because they are only needed when the page is being loaded or rendered. During loading and rendering, both sets of objects can be created in a mixture. Then the non-persistent ones would be destructed. Because of the order of allocation of underlying memory, the persistent objects tend to be interleaved with the non-persistent ones. That then means most pages may have some persistent object lying around, preventing it from being returned to the kernel.

    Solutions to this problem would be difficult. But I also think this effort would be valuable for any and all large projects that can face this kind of memory issue. Some means is needed to control the memory alloction, and in particular to allow grouping of memory into contexts. The first kind of context would be a context for each tab or window being opened by the browser user. That way, if a tab is closed, it should substantially destruct objects grouped together. But this can also be wasteful because the non-persistent objects that do get destructed after rendering is done cannot have their memory recycled by other contexts. So another dimenstion of context needs to be on the basis of what is persistent vs. non-persistent, so that all non-persistent memory gets grouped together so it can be returned to the kernel as whole pages, which can then be recycled to other contexts (getting the pages from the kernel again).

    This would require a much more involved memory allocation system. Further, it would also require major changes in many of the abstract programming classes used by such large programs ... in ways that tend to be counter to what the abstraction is all about in the first place. Abstraction is supposed to hide details about the underlying implementation so the programmer can/should concentrate on application logic. But this is not really an optimal way to program when dealing with limited resource issues that need to be managed, such as the memory issues seen here. In particular, the various classes themselves won't know whether they are persistent (in the context of what the browser application needs) or not. Many instances of the very same class may be created for both persistent and non-persistent intents, so the class itself could never be designed to make any such assumptions (e.g. think of hiding the details in reverse ... the class does not see the details of the application, e.g. which instances are to be long lived and which are not).

    A concept that may help with this is one that would have to be applied to the whole of such object oriented programming, or even non-OO functions that also could allocate memory for such variant uses (this isn't fundamentally an OO problem ... OO merely exposes it due to the larger application scale that OO enables to be implemented). This concept is to create instance groups that can span laterally across all classes. It would require that each time an instance is created, that it be associated with a particular instance group. Then instead of destructing each instance individually, the group is destructed, which destructs all instances in the group. The implementation of all thi

    --
    now we need to go OSS in diesel cars
  9. This is not either/or, people by Kris_J · · Score: 3, Interesting

    This is exactly the sort of thing you handle with a slider in the options; "How would you like Firefox to handle images" with High CPU/low RAM on one end and Low CPU/High RAM on the other. The slider would determine how aggressively Firefox caches uncompressed images.