Well Stuart is careful to say that jemalloc did well for Firefox specifically rather than just
saying that it's better for everyone. He doesn't actually give any GNU libc numbers for
comparison (unlike for Windows). Google and many others have provided alternative allocators,
it wouldn't be fair to characterise any of their differences as "drawbacks" necessarily, but
this is definitely a case where there are tradeoffs to be made.
Some allocators concentrate on the tiny allocations needed for all those variable length HTML
elements, filenames, email addresses, etc. they may be very efficient but in doing so use a
few more CPU cycles when you free() an item, or slowly fragment your address space over time
so that they're not acceptable in a program which runs for days or weeks at a time.
In some applications the allocator must absolutely be fast, above almost anything. Google, for
example, offer a really fast allocator but it will never return heap memory to the OS.
Allocate some memory once, and you're stuck with it. You can re-use it, split it up, maybe
merge it with another allocation, but the Google allocator won't ever give it back for use by
other applications. If you've just visited myhugeimages.example.com and Firefox won't give
back the 600MB of memory it used rendering the site, this would be unacceptable.
In some applications, as in this Firefox example, fragmentation is determined to be a problem,
and the application is long-lived, it's OK to be a little slower, so long as you don't waste
memory or fragment the address space too much and so long as large allocations are returned
when no longer needed (avoiding fragmentation actually makes that easier).
Thread safety is another variable. Of course your allocator will run faster if it doesn't
worry about this at all, but some applications must have a thread safe allocator. Some will
even want to allocate memory in one thread and then later free it from a different thread.
The system / libc provided malloc is intended only to be a good default. If your application
is important enough to warrant writing or maintaining your own allocator, as has effectively
happened with jemalloc in Firefox, then you can stop using the C library's allocator and it's
smiles all round. GNU lib intentionally emits weak symbols for the allocator, making it easy
to choose your own instead.
OK, on a decent modern OS, not until the program exits. If your OS doesn't track heap
allocations made by userspace, and so leaks RAM when apps crash, well - here's a nickel, get
yourself a real operating system kid.
Posted Mar 13, 2008 3:05 UTC (Thu) by pr1268 (subscriber, #24648)
[Link]
Fascinating discussion - I never knew Firefox used a different allocator - in fact, I was unaware that any userspace application could use any allocator other than what was provided by GNU libc (on GNU/Linux systems in particular).
When you mention "Google", what exactly are you talking about? I thought Google was a Web site/portal with online applications, not a userspace app running on someone's PC. Do you mean Google Earth? Thanks!
Googles TCMalloc
Posted Mar 13, 2008 3:50 UTC (Thu) by pflugstad (subscriber, #224)
[Link]
Posted Mar 14, 2008 14:27 UTC (Fri) by Los__D (guest, #15263)
[Link]
Google has a ton of libraries, frameworks, general tools and other stuff , either developed as
an improvement to some in-house project and then released afterwards, or just created as a
service to other developers.
jemalloc
Posted Mar 13, 2008 7:02 UTC (Thu) by rsidd (subscriber, #2582)
[Link]
If your application is important enough to warrant writing or maintaining your own allocator, as has effectively happened with jemalloc in Firefox
No that's not what happened: jemalloc was written for FreeBSD (where it is the default in 7.0 and up), and ported to firefox with the help of the author, Jason Evans.
jemalloc
Posted Mar 13, 2008 14:31 UTC (Thu) by tialaramex (subscriber, #21167)
[Link]
To quote the article, It was a huge effort resulting in Jason doubling the number of lines of
code in jemalloc over a 2 month period
If Stuart is misdescribing the situation, argue with him, not me.
jemalloc
Posted Mar 13, 2008 14:52 UTC (Thu) by tialaramex (subscriber, #21167)
[Link]
... and for the avoidance of doubt, I didn't intend to hide Jason's credit or imply that
Mozilla.org people are doing all the hard work, but only that this sort of thing doesn't come
free. Firefox developers decided that it was worth it, but for most Free Software projects,
it's going to make sense to use the system allocator unless it's absolutely dire.