Live data from Hacker News

The effect of switching to TCMalloc on RocksDB memory use

blog.cloudflare.com

21–30 of 49 posts

Re: The effect of switching to TCMalloc on RocksDB memory use

#21
since each memory request chooses the best fit fragment from an individual arena and not the best fit fragment overall

I vaguely remember when working with the DOS memory allocator many years ago, you could choose between the first/next/best/worst fit strategies, and best fit would tend to produce the worst fragmentation (creating lots of tiny fragments); but as others have noted, it highly depends on the allocation pattern of the application.

Re: The effect of switching to TCMalloc on RocksDB memory use

#22

The blog post sadly doesn't cover what alternatives are out there, and why they settled on their choice. Besides tcmalloc, there are jmalloc, hoard, the more recent mimalloc, a bunch of commercial options, ... Would have been interesting to see a comparison. Edit: background is in this thread: https://news.ycombinator.com/item?id=26013230

Do you think any of these stand a chance at replacing Doug Lea's malloc in glibc, or does dlmalloc provide more desirable behavior for the general case?

The FSF requires copyright assignment for code added to glibc, which creates an impediment to including code from other projects.

Re: The effect of switching to TCMalloc on RocksDB memory use

#23
post #6

Why don’t things like malloc get fixed? I’ve known about these issues for years. Does the core team not know?

The FSF requires copyright assignment for code added to glibc, which makes it difficult to add code from other projects.

Re: The effect of switching to TCMalloc on RocksDB memory use

#24
I remember this hit the Varnish Cache project about 7-8 years ago.

We've spent endless hours trying to identify leaks before we finally tried a few alternative allocators. We switched to jemalloc and the positive effects where huge.

Its a bit weird that this is still an issue.

Re: The effect of switching to TCMalloc on RocksDB memory use

#25
post #13

Earlier quoted context omitted.

Because there's no such thing as a truly general purpose memory allocator. Memory allocation is built to some purpose, and the programmer who uses it should be aware of its problems. There's probably a technical reason why the original glibc malloc does what it does (probably better optimized for low-thread counts)

What you say is technically true but can be used to just hand wave away any issue. Things can almost always be improved. Allocators like TCMalloc are not significantly worse in single threaded mode. The glibc allocator just isn’t very good. I have personally fought with the glibc allocator and seen issues with it that don’t exist on other OSes.

Historically the glibc allocator had a number of hooks as well as support for unexec(). I think unexec() has been killed (thankfully) but the glibc allocator API is still not exactly equal to other allocator APIs.

Re: The effect of switching to TCMalloc on RocksDB memory use

#26
Defragmentation is one benefit of garbage collected environments I feel like you don't see mentioned often. Every collector I can think of is capable of compacting its heap so that you are less likely to suffer from fragmentation overhead, and it happens as part of the regular GC flow (though typically not the 'fast' collections) instead of being something you have to go out of your way to trigger at ideal times.

Obviously there are downsides to GC, but it's interesting to see how in this scenario malloc+free had the same "uses way more memory than strictly necessary" problem that people usually pin on garbage collectors.

Re: The effect of switching to TCMalloc on RocksDB memory use

#27
post #6

Why don’t things like malloc get fixed? I’ve known about these issues for years. Does the core team not know?

Because there's no such thing as a truly general purpose memory allocator. Memory allocation is built to some purpose, and the programmer who uses it should be aware of its problems. There's probably a technical reason why the original glibc malloc does what it does (probably better optimized for low-thread counts)

I'm not aware of any production use cases where tcmalloc would be worse than glibc malloc in a way that end users would actually feel. tcmalloc and jemalloc are used all over the place in all sorts of different applications.

If glibc malloc is so bad at something like this I find it hard to believe it would be great at anything important if jemalloc/tcmalloc are not.

Re: The effect of switching to TCMalloc on RocksDB memory use

#28

This problem hit the Ruby community hard about 5 years ago. Basically everyone uses jemalloc or tcmalloc in production in the Ruby community nowadays. What frustrates me is the malloc community's sort of blase reaction along the lines of "there's no bug, this is expected behavior".

Why try to optimize for every cases possible ? Use the right tool for the right job.

Re: The effect of switching to TCMalloc on RocksDB memory use

#29

I wrote a commercial memory allocator (long long gone) for pre MacOS 10 and tested it by writing a ton of memory bashing applications based on every perverse pattern I could think of in order to ensure it was fast, failure free and limited fragmentation. Of course from that era I didn't need to support multiple threads so that made it somewhat easier, but in building it (and studying others at the time) it was obviou…

It's interesting that I saw this post minutes after watching the talk "What's a Memory Allocator Anyway?" by Benjamin Feng. It's ostensibly about Zig's allocation schemes, but it's more of a general overview of the varied (single-thread) allocation strategies in use today. It made me really want to try out arena allocators

It's a quick watch at 1.5x speed, the last third is an interview.

1. https://youtu.be/vHWiDx_l4V0

Re: The effect of switching to TCMalloc on RocksDB memory use

#30

I've spent some time really studying memory allocation. It seems to me that almost any high-performance application will take advantage of the fine-details of memory allocators to optimize themselves. There's something to be said about garbage collection as an actual strategy (!!). I know people think that garbage collection is slow, but... very smart people have put their minds on the memory problem. A generational…

This sort of thing is why I'm generally not impressed by the perennial "GC vs. manual memory management" debate. Manual memory management usually isn't. Simply using "malloc" isn't manual memory management, there's still a lot of stuff going on behind the scenes that may or may not match your program's use case. Both GC and "manual" memory management aren't single points, they're suites of options, which you can often mix and match beyond that. Statements like "Manual memory management is better than GC" is roughly like saying "Pie tastes better than cake!"... it doesn't even really make sense, there's so many pies and so many cakes that there's no way to universally order them like that, even for a specific person; add in the subjectivity of taste (in this metaphor, the different needs various programs have of memory in the first place) and it's beyond absurd.

(To my mind, "manual memory management" is when you get a big slab of memory from something, doesn't much matter what, and everything else is managed in your application code. If you're not doing that, you've got some sort of "automation" involved, and that automation can mismatch your program's memory needs every bit as much as a "GC" can.)

Post reply on HN