‹ BackHN Continuity

Thread

Jemalloc 5.4.0

339 points · 94 comments · gkfasdfasdf

  1. ZenoArrow · · focus · HN ↗
    Why is this on the HN front page? Is there something particularly noteworthy about this release?
    1. baq · · focus · HN ↗
      jemalloc is something you should be aware of if you do software for a living
      1. rfgplk · · focus · HN ↗
        Why? Writing a memory allocator is quite simple, and I'd argue that _everyone_ should write one from scratch for any kind of high performance application. It's also trivial to outperform general purpose allocators that have to satisfy countless constraints. I've written numerous special purpose mallocs that are a) both provably (formally) safer than the standard armada and b) significantly faster (>10x throughput).
        1. groomlake · · focus · HN ↗
          Your experience writing memory allocators is irrelevant. The point is that jemalloc is widely used and that’s why it makes sense to be aware of it.
        2. HackerThemAll · · focus · HN ↗
          I'd happily see performance, latency and stability of your allocators in massively multithreaded, long-living programs with workloads where hundreds or thousands of parallel threads continuously create and destroy short-lived small and medium objects.

          Writing allocators for domain-specific access patterns is easy. Writing a general-purpose high performing, stable allocator with bounded P99 latency is hard.

          Give your friend, Dunning–Kruger, some better pills to keep him from speaking through you.

          1. Pannoniae · · focus · HN ↗
            You're correct, but his point is that you don't need to solve the generic problem. Solving the generic problem is very hard. Grug doesn't like solving hard problem. What does grug do? Solve five easy problems. Make an arena for the short-lived objects, reuse the objects, use generic multithreaded malloc for the rest. Grug happy.
          2. matheusmoreira · · focus · HN ↗
            Not everything needs to be general purpose. Allocation can be as easy as bumping a pointer, and it's hard to beat that.
          3. cv5005 · · focus · HN ↗
            >where hundreds or thousands of parallel threads continuously create and destroy short-lived small and medium objects.

            Should one even want a global, general purpose heap allocator for that? Seems like a crazy idea to even consider.

          4. Mikhail_Edoshin · · focus · HN ↗
            I guess the point was that before you consider using a different allocator you should rule out a custom one.

            And that's rather hard, because a general purpose allocator makes all decisions based only on the requested size. This is a very simple interface and such a tool is worth having. But a custom allocator can both bake in a specific scenario and provide more nuanced interaction.

          5. jonkerz · · focus · HN ↗
            >massively multithreaded, long-living programs with workloads where hundreds or thousands of parallel threads continuously create and destroy short-lived small and medium objects.

            My first thought would be to use per thread pool allocators.

        3. stackghost · · focus · HN ↗
          Writing an allocator is simple, you’re correct, but writing an allocator that doesn’t suck is not simple.
      2. stackghost · · focus · HN ↗
        In the Before Times, the vast majority of software was written in garbage collected language where a working knowledge of the relative merits of C memory allocators is not useful or particularly relevant.

        Why would the scads of people writing JavaScript, Java, python, go, rails, etc need to be aware of jemalloc?

        1. iam-da-author · · focus · HN ↗
          TL;DR: Because the runtime of most GC:d languages uses malloc for its internal data structures.

          I work for the runtime team of JPG @ Oracle. We use malloc in Hotspot, quite a lot actually! Providing your JVM with a good malloc can improve the performance of the runtime, both in terms of CPU and memory, by quite a bit.

          I don't think you need the details, but it's good to be aware that some mallocs are better than others, and there are multiple of them. Being aware of jemalloc is a good way of being aware of the facts I just mentioned :-).

          1. stackghost · · focus · HN ↗
            The vast majority of professional developers are not in a position where they can just swap out allocators willy-nilly. They take what they get, and write the code they're assigned to write on the platform the CTO or their product lead or whoever has decided upon.
            1. iam-da-author · · focus · HN ↗
              Okay, well, I guess all I can say is that if you strive to be one of the developers who do get the chance to care about this stuff, then you should know this stuff :-).
        2. xxs · · focus · HN ↗
          >...the vast majority of software was written in garbage collected language

          and even then recently it costed (us) quite a few months to blame JVM and later the default glibc memory allocator for running out native (not java heap memory) - had to exclude all possible native libs (zlib, zstd via jna), direct buffers, sockets, thread stacks and so on. Changing the malloc to jemalloc solved the issue, even though initially it was done for its debugging capabilities.

          It's just a great memory allocator.

          1. fc417fc802 · · focus · HN ↗
            I'm never sure if I should be upset or happy when I've been debugging a problem for long enough that I finally decide to switch something out in order to improve visibility and that immediately solves the problem for entirely unexpected reasons. Particularly all the times when I couldn't readily discern why.
          2. javier2 · · focus · HN ↗
            We changed to jemalloc first on a jvm service which we could never get to run in its kube memory limit. with jemalloc its been dead stable for years, and we made it the default for all jvm services
        3. nh2 · · focus · HN ↗
          Memory allocation behaviour has visible impact also for users of managed languages, and the behaviour of software for end users.

          In our Python program, a bit of numpy processing of large pictures led to 100 GB not being returned to the OS by glibc's default allocator and the machine running out of memory shortly after. With jemalloc's reliable memory return settings, those problems disappear.

          1. majora2007 · · focus · HN ↗
            I'm in the same boat, I just switched to using it for Kavita, which only does some basic open Image -> Thumbnail to smaller size -> write to disk when importing new comics/books and on linux, memory could swell to 10GB and never get released. Switched to jemalloc and instantly memory stayed well below 1GB.
            1. nh2 · · focus · HN ↗
              Generally yes, but the wording of "instantly" begs for the following pedantic remark:

              This is controlled by jemalloc settings `dirty_decay_ms`, `muzzy_decay_ms`, and their interaction with `background_thread`.

              `dirty_decay_ms` currently defaults to 10 seconds, so it's not that instant.

              That is important e.g. for single-threaded programs that start other programs, such as my Python example: If it starts a subprocess before the 10 seconds elapse after `free()`, Python (and jemalloc) do not run, and get no chance to return memory to the OS.

              In such cases, either enable `background_thread`, or set the `_decay_` values to `0` to ensure immediate return to the OS upon `free()`. (This costs some performance.)

              See e.g. <a href="https:&#x2F;&#x2F;github.com&#x2F;jemalloc&#x2F;jemalloc&#x2F;issues&#x2F;2688" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jemalloc&#x2F;jemalloc&#x2F;issues&#x2F;2688

          2. stackghost · · focus · HN ↗
            Okay but do you think the hordes of JavaScript developers at FANG can just change the browser’s allocator?

            The number of programmers who are in positions to care about jemalloc vs other malloc is minuscule

        4. yxhuvud · · focus · HN ↗
          Because unfortunately, the runtimes belonging to all or at least most of those languages perform a lot better with jemalloc than with the system default.

          I wish that wasn&#x27;t the case, but it is.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.