Firefox memory bloat fix

Firefox users complained a lot about the memory situation since the Firefox 4 release. The browser consumes endless memory if it stays idle for a while and after an allocation heavy task it doesn’t trigger a GC to shrink the memory footprint.

One of the reasons why this happened is the landing of the Bug 558451 - Merge JSScope into JSScopeProperty, JSObject“. JSScopes were allocated with malloc and not on our JS heap. These off-heap allocations triggered most of our GCs. Without these allocations, our main GC trigger vanished over night. I did a quick fix in Bug 592007 - TM: New Scope patch changes GC behavior in browser“. The main goal was to imitate our old GC behavior. Our off-heap allocation trigger is pretty simple: Once we hit 128MB of mallocs after the last GC trigger a new GC.

The problem was that the new trigger has to monitor and predict heap growth rather than simply add off-heap allocations. I introduced a heap-growth-factor that allows the JS heap to grow by 300% before we force another GC. So the additional trigger was based on the amount of memory that survives the GC. This number has to allow the heap to grow very fast because we don’t want to trigger a GC during a critical page-load or benchmark. With these changes, the GC behavior was almost the same as before.

Well now we can see that it was not good enough because we only trigger the GC when we allocate objects. This means that we don’t perform a GC even if we have a huge heap because the trigger limit is not reached. Running the V8 benchmark suite is an example for this bad behavior. Right at the end of the suite the splay benchmark allocates a huge splay tree. We have to increase our JS heap and the amount of reachable memory after each GC is around 300MB. Our GC trigger allows the heap to grow 3x before the next GC is performed.

So after the benchmark we end up with a heap size between 300 and 900MB and we don’t perform a GC until the trigger limit (900MB) is reached. This can be forever if you just read a blog or surf on some pages that are not JS allocation heavy. I did most of my testing on my MacBook Pro with 4GB of RAM. So I never realized this bad behavior. Recently I bought a new netbook with 1GB RAM and running the V8 benchmark suite on it was painful because afterwards my browser used up all memory and no GC made my browser useable again.

In order to make FF work on my netbook I implemented Bug 656120 - Increase GC frequency: GC occasionally based on a timer (except when completely idle)“. The idea is now that we perform a GC even if the trigger is not reached after 20 seconds. This should shrink the heap without hurting any allocation heavy pages like benchmarks or animations.

Nick Nethercote found that this patch fixed many other bugs that other users filed. It didn’t make it into the FF 6 release. Maybe not enough people complained about the memory bloat problem or we should just buy all release drivers a low-end netbook :) Well the good thing is that the fix will be in FF7! Everybody should see a reduced memory footprint and it should definitely help users with limited devices like netbooks!

PS: This patch also reduces fragmentation but that’s for the next post!

This entry was posted in Firefox GC. Bookmark the permalink.

19 Responses to Firefox memory bloat fix

  1. Sven Meyer says:

    Great work. Most important achievement in the FF development since years. I was at least complaining about the memory bloat already for a long time. Just because everybody (ok, most) guys have a multi-GHz, zig.GB, many-Core Computer is no reason to waste resources uselessly – smart programmer do care about resources !

    • greg9328 says:

      Thanks for the feedback! I recently bought a new netbook with 1GB RAM and noticed that every MB counts.

  2. Pingback: Speicherverbrauch von Firefox deutlich reduziert | Thomas Joss

  3. Michael says:

    You’re the best…
    Greetings from Germany

  4. Knut says:

    Thank you very much for this great work.

    I noticed this hunger for RAM for a long time and was wondering too, why nobody cares about this.

    I hope that your work fill find a way into the FF source code very soon…

  5. Stephen says:


    I’m heartened to hear you believe you’ve got to the bottom of this issue. I’m sure all loyal firefox users will be very grateful if indeed version 7.0 delivers.

    However I must tell you I really was appalled when you commented : “It didn’t make it into the FF 6 release. Maybe not enough people complained about the memory bloat problem..”

    Did you by any chance read the comments on regarding memory consumption by FF4 anf FF5 ? A lot of angry people made their feelings pretty plainly known very early on. This issue should have been nipped in the bud. RAM may be cheap these days, but any decent programmer treats it with respect.

    • Gregor says:

      Thanks for your comment!
      I was also a little bit disappointed that my changes were not approved for an earlier release. The line about “not enough people complaining” should have been a little bit ironic.

  6. Pingback: Firefox 7 to Use 20-50% Less Memory

  7. I’ve use myPhone TV21duo with 2SD memory card. I don’t know what it’s software and I hope to get support form firefox. thank you

  8. Jason Marshall says:

    “Firefox users complained a lot about the memory situation since the Firefox 4 release. ”

    We have been complaining about the memory footprint for a lot longer than that. And I know a lot of people who are less comfortable with complaining than I am, who have simply voted with their feet.

    It’s a running gag in my circle of friends that Firefox doesn’t have ANY memory leaks… except for the 5 that were fixed in the last release. Oh and the five in the release before that, and so on.

    It’s great to see someone on the team finally owning this fact. Hats off to you.

  9. Pingback: Firefox 7 and Browser Memory Consumption | Salsita Software - Custom development of web browser extensions, plugins and packaged web apps

  10. Ben says:

    Here I am, running FF7 on my testing netbook with 1GB, using a special restart plugin that I have needed since 3 full versions ago and, yes, do still need as much as back then. I like using 20+ tabs, but after closing them all I DO expect something below 350MB of RAM usage.

    Of course, I didn’t pay for this browser and I am grateful, but I am slowly migrating towards Safari which, under the same circumstances, never seems to exceed 200MB of RAM. I wonder how many faithful users have, just like me, just about given up after being fervent and faithful FF users since version 0.7…

    Anyway, I am glad people like you are around – people who at least try to fix the issue and who listen to people talking about it. Maybe the decline in FF popularity that I am seeing among my users (I’m a Geman sysop) can be stopped after all.

  11. Robt says:

    I see a lot of interesting content on your blog.

    You have to spend a lot of time writing, i know how to save you a lot of work,
    there is a tool that creates unique, google friendly articles in couple of
    seconds, just type in google – laranita’s free content source

  12. Brad says:

    Its such as you read my thoughts! You seem to
    grasp so much about this, like you wrote the ebook in it or something.
    I feel that you just can do with some percent to pressure the message house a
    little bit, but other than that, that is wonderful blog.
    A fantastic read. I’ll certainly be back.

  13. My spouse and I stumbled over here coming from a different web address and
    thought I might check things out. I like what I see so now i
    am following you. Look forward to looking over your web page again.

    Feel free to surf to my homepage: blog post immediately

  14. You really make it seem so easy with your presentation but I find this topic to
    be really something that I think I would never understand.
    It seems too complex and extremely broad for me.
    I am looking forward for your next post, I will
    try to get the hang of it!

  15. Niki says:

    I read a lot of interesting content here. Probably you spend a lot of time writing,
    i know how to save you a lot of work, there is an online tool that creates high quality, google friendly articles in seconds, just search in google – k2seotips
    unlimited content

  16. air max 90 says:

    I will immediately snatch your rss feed as I can’t to find your e-mail subscription hyperlink or newsletter
    service. Do you have any? Please let me realize in order that I may just
    subscribe. Thanks.

    Visit my web-site :: air max 90

  17. Jean Markale says:

    Hi Gregor,
    I’m from the future (2014 to be precise).
    I’m here to tell you that the “fix” did nothing. It simply does not work. Firefox is more bloated than ever. The number of tabs or nature of the pages is irrelevant. If one has three tabs open on light pages, it will still bloat up to 300, 400 MB, and keep climbing. The only way out is to force Firefox to close and restart it.
    This explains why Firefox continues to suck; apparently even the contributors believe that their “solutions” work when they clearly do nothing.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>