‹ BackHN Continuity

Thread

Comparison of Malloc() Algorithms

142 points · 51 comments · egberts1

Loading the complete thread in the background. This saved snapshot is available now. Refresh

  1. benjojo12 · · focus · HN ↗
    ERR_SSL_VERSION_OR_CIPHER_MISMATCH on my phone it seems?
    1. j4k0bfr · · focus · HN ↗
      Same, from Chrome on Android
    2. Rygian · · focus · HN ↗
      TLS_CHACHA20_POLY1305_SHA256 (256 bit keys, TLS 1.3) successfully negotiated here (Firefox esr 140.14.0).
  2. Nnnes · · focus · HN ↗
    <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20260915165314&#x2F;https:&#x2F;&#x2F;egbert.net&#x2F;blog&#x2F;articles&#x2F;comparison-of-arena-architecture-in-malloc.html" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20260915165314&#x2F;https:&#x2F;&#x2F;egbert.ne...

    Funny SSL setup. Explanation from here <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49133598">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=49133598

    &gt; Oh, certain browser will not work with this blog if it cannot negotiate ONLY for Cha-Cha&#x2F;Poly. It&#x27;s by design as a showcase of why that particular web browser refuses to do that.

    I assume the &quot;particular web browser&quot; is Chromium, which won&#x27;t load it on any OS I&#x27;ve tried. On Windows, Firefox and the built-in curl.exe also refuse to connect.

    1. bom-d-van · · focus · HN ↗
      legend.
    2. [deleted] · · focus · HN ↗

      [deleted]

    3. ncruces · · focus · HN ↗
      Thanks for the link.

      Article makes it look like nothing happened in the embed&#x2F;low-memory&#x2F;single-threaded malloc space in decades since Doug Lea&#x27;s malloc.

      I just implemented TLSF for my minimal Wasm libc: fragmentation is just as good, performance is a lot more consistent and better of average, for a significant reduction in code size.

      <a href="http:&#x2F;&#x2F;www.gii.upv.es&#x2F;tlsf&#x2F;index.html" rel="nofollow">http:&#x2F;&#x2F;www.gii.upv.es&#x2F;tlsf&#x2F;index.html

      <a href="https:&#x2F;&#x2F;github.com&#x2F;ncruces&#x2F;wasm2go&#x2F;blob&#x2F;main&#x2F;libc-gen&#x2F;c&#x2F;malloc_tlsf.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ncruces&#x2F;wasm2go&#x2F;blob&#x2F;main&#x2F;libc-gen&#x2F;c&#x2F;mall...

      1. egberts1 · · focus · HN ↗
        [delayed]
      2. thomasmg · · focus · HN ↗
        Interesting! I have also implemented a versionn of TLSF for my (currently single-threaded) programming language. I also found it to be good for my use case (embedded &#x2F; minimal code size). I&#x27;ll compare it against yours. One improvement I did is some kind of preallocation for small blocks. <a href="https:&#x2F;&#x2F;github.com&#x2F;thomasmueller&#x2F;bau-lang&#x2F;blob&#x2F;main&#x2F;src&#x2F;main&#x2F;java&#x2F;org&#x2F;bau&#x2F;parser&#x2F;StandardLib.java#L9" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;thomasmueller&#x2F;bau-lang&#x2F;blob&#x2F;main&#x2F;src&#x2F;main...
    4. egberts1 · · focus · HN ↗
      [delayed]
    5. quietbritishjim · · focus · HN ↗
      Firefox on Windows user here, and it loads the blog fine. I don&#x27;t have Chrome installed but Edge (which is Chromium engine based) doesn&#x27;t load it.
    6. dark-star · · focus · HN ↗
      I don&#x27;t get what this is supposed to prove. That my browser doesn&#x27;t allow websites to declare what encryption they use? I am pretty sure there&#x27;s a point to all of this, but please let me (and my browser) choose which encryptions I want to trust.

      Is this a spec violation or something?

      1. gspr · · focus · HN ↗
        Isn&#x27;t the idea that the server and client should agree on a common subset? Here, the server&#x27;s subset is very small, but not esoteric – so then surely it&#x27;s the client that&#x27;s lacking in features?
        1. egberts1 · · focus · HN ↗
          [delayed]
        2. dark-star · · focus · HN ↗
          Yeah but I don&#x27;t want Chachacha, I prefer AES. If you can&#x27;t do AES, I&#x27;m not interested in the website :)
  3. ligarota · · focus · HN ↗
    Please write a &quot;how to setup SSL&quot; article
    1. eqvinox · · focus · HN ↗
      It seems to be intentional &amp; if somebody wants to make a statement about TLS with their personal website that&#x27;s their choice to make and execute.
      1. entrope · · focus · HN ↗
        On the bright side, lots of people will be saved from reading bad prose like &quot;Malloc (libc) is the worst memory allocation API to use&quot; and &quot;Programs should avoid, if possible, allocating&#x2F;deallocating memory too often&quot;. (By definition, &quot;too often&quot; means it can possibly be avoided, and usually that it can practically be avoided.)
        1. 0c3ca83 · · focus · HN ↗
          Beats the hell out of most of the LLM-generated &quot;honest assesments&quot; of things on this site.
        2. dezsiszabi · · focus · HN ↗
          I might be stupid, but what&#x27;s wrong with those sentences?
          1. entrope · · focus · HN ↗
            The first one was offered with no explanation and no claimed better alternative. malloc() is probably the simplest interface for generic runtime allocation, and simplicity has a lot in its favor. malloc() does not provide type safety, is susceptible to external fragmentation, and makes it harder to meet the performance goals outlined in the rest of the blog post. So malloc() reflects trade-offs, but saying that the standard API &quot;is the worst memory allocation API to use&quot; should be supported, even if briefly, instead of simply asserted: meeting the goals I listed inflicts other drawbacks, like needing to create and manage separate heaps.

            I thought my explanation for the second one was already clear: &quot;Programs should avoid, if possible, [doing X] too often&quot; is a truism because &quot;too often&quot; implies that some reduction is possible. One should leave out the &quot;, if possible,&quot; -- although deciding what is &quot;too often&quot; can be challenging and sometimes a matter of taste. (Is reducing allocation frequency by 5% worth doubling the CPU usage or memory usage or code complexity? Maybe in some cases, but often not.)

      2. egberts1 · · focus · HN ↗
        Yes
    2. egberts1 · · focus · HN ↗
      No
  4. Someone · · focus · HN ↗
    Not a good article, IMO.

    FTA: “When multiple threads simultaneously allocate or deallocate memory from the allocator, the allocator will serialize them. Programs making intensive use of the allocator actually slow down as the number of processors increases.”

    The article does later retract on that, but that’s no reason to lead with such a blatantly false (with current allocators) statement.

    Also FTA “In 2006, a third pool was introduced (after operating system memory pool and library-based memory pool) called the “arena”. Arena is a jemalloc-term”

    Jemalloc is from around 2005 (<a href="http:&#x2F;&#x2F;jemalloc.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;jemalloc.net&#x2F;), the idea of arenas is from the 1960s, and Wikipedia claims the term was coined in 1990 (<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Region-based_memory_management#History_and_concepts" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Region-based_memory_management...), and the linked paper (<a href="https:&#x2F;&#x2F;www.cs.princeton.edu&#x2F;techreports&#x2F;1988&#x2F;191.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.princeton.edu&#x2F;techreports&#x2F;1988&#x2F;191.pdf) is from 1988.

    Then, a typo: “as well as memory tied to specific to each of the multiple CPU core or even CPU infinity.”

    “Infinity” should be “affinity” there.

    1. eqvinox · · focus · HN ↗
      The tables look mostly correct, and that&#x27;s what I&#x27;ll be bookmarking this for… I don&#x27;t think I&#x27;ve seen any elsewhere that are this extensive (in both axis).
      1. egberts1 · · focus · HN ↗
        [delayed]
    2. skavi · · focus · HN ↗
      yup and the characterization of each allocator is so fuzzy, with zero methodology provided.

      allocators are so simple to just swap into your program. if you can put together a few representative workloads, you should just try out a few allocators and profile whatever metrics you care about.

      1. imp0cat · · focus · HN ↗
        And finally end up with either jemalloc or possibly mimalloc. ;)
        1. egberts1 · · focus · HN ↗
          Invariably so but I&#x27;m mulling over malloc()s on embedded topic now.
        2. skavi · · focus · HN ↗
          we actually ended up with (new) tcmalloc.

          for us, tc was among the fastest in runtime while being very space efficient [0]. large rust application using far too many threads.

          we’ve since also had great success with tc’s built in profiling tools.

          [0]: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=47403847">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=47403847

          1. imp0cat · · focus · HN ↗
            Interesting, is there an accompanying article?

            We&#x27;ve tried tcmalloc, too. I don&#x27;t remember the exact details, but we basically ended using jemalloc because it was using way less memory.

            Same story with mimalloc - it usually provided a tiny bit more speed, but required more cpu and memory.

            1. skavi · · focus · HN ↗
              no article, sorry, grabbed the numbers from an old PR.

              to confirm, you were using tcmalloc from <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;tcmalloc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;tcmalloc and not from <a href="https:&#x2F;&#x2F;github.com&#x2F;gperftools&#x2F;gperftools" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gperftools&#x2F;gperftools, right?

              the latter is a lot worse iiuc.

              1. imp0cat · · focus · HN ↗
                Good point. It was from the <a href="https:&#x2F;&#x2F;packages.debian.org&#x2F;trixie&#x2F;google-perftools" rel="nofollow">https:&#x2F;&#x2F;packages.debian.org&#x2F;trixie&#x2F;google-perftools Debian package, which points to the gperftools project - so it was the worse one I guess.
                1. skavi · · focus · HN ↗
                  yeah that’s a common mistake when evaluating tcmalloc. gperftools tcmalloc diverged quite a while ago. doesn’t have a lot of the fancier features of modern tcmalloc [0].

                  [0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;tcmalloc&#x2F;blob&#x2F;master&#x2F;docs&#x2F;gperftools.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;tcmalloc&#x2F;blob&#x2F;master&#x2F;docs&#x2F;gperftoo...

      2. zX41ZdbW · · focus · HN ↗
        ClickHouse has been tested with jemalloc, mimalloc, tcmalloc (both variants), rpmalloc, lfalloc, hualloc, and ended up using jemalloc after a few patches and bug fixes.
        1. egberts1 · · focus · HN ↗
          [delayed]
          1. zX41ZdbW · · focus · HN ↗
            lfalloc: <a href="https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;62826" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;62826 hualloc: <a href="https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;31376" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;31376
            1. egberts1 · · focus · HN ↗
              [delayed]
        2. skavi · · focus · HN ↗
          a while back, our tests led us to tcmalloc (new).

          <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=47403847">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=47403847

    3. egberts1 · · focus · HN ↗
      [delayed]
    4. adrian_b · · focus · HN ↗
      Yes, I do not know what this paragraph wants to say:

      &gt; &quot;The first memory allocation scheme started with a stack-based memory allocation. Next came the dynamic-based memory allocation scheme where linked-list and bucket-heap mechanism are used to divide the private-heap using size class approach. Soon, garbage collection algorithm introduced the initial backend of the memory allocation scheme.&quot;

      Since no specific operating system is mentioned, these sentences appear to refer to the general history of dynamic memory allocation, in which case they are wrong.

      &quot;malloc&quot; is a late comer in this history. It has appeared as the statement &quot;ALLOCATE&quot;, together with the statement &quot;FREE&quot;, in the programming language PL&#x2F;I of IBM, by the end of 1964.

      At that time many other techniques of managing memory had already been used for a few years.

      Dynamic allocation of memory has started with allocation without ever freeing the allocated memory before the termination of the process.

      Then, in 1960, 3 methods of handling dynamic memory allocation were published: the use of garbage collectors in April (John McCarthy), the use of stacks in May (E. W. Dijkstra), and the use of reference counts in December (George E. Collins @ IBM).

      So the use of garbage collectors is actually the oldest published method for handling dynamic memory allocation, not a newer method.

      1. egberts1 · · focus · HN ↗
        [delayed]
        1. adrian_b · · focus · HN ↗
          To help you with the citations:

          Garbage collectors: &quot;Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I&quot;, John McCarthy, Communications of the ACM, pp. 184-195, 1960-04.

          Stacks and stack pointers: &quot;Recursive Programming&quot;, Edsger Wybe Dijkstra, 1960-05-11.

          Reference counts for memory allocation were introduced in &quot;A Method for Overlapping and Erasure of Lists&quot;, George E. Collins (IBM), Communications of the ACM, Volume 3, Issue 12, 1960-12, pp. 655–657.

          1. egberts1 · · focus · HN ↗
            [delayed]
  5. AnimalMuppet · · focus · HN ↗
    Couldn&#x27;t read the article (ERR_SSL_VERSION_OR_CIPHER_MISMATCH, Chrome on Windows). But I&#x27;m remembering something a coworker told me some time around... 1991 to 1993, maybe? Forgive me if I repeat some of what the article says - I did try to read it!

    While he was at the university, they were experimenting with different kind of mallocs. One was called the &quot;buddy&quot; malloc. It kept a list of free blocks of various sizes, and when you asked for a block and it didn&#x27;t have one, it asked the OS for twice as much as you asked for. From the rest, it made another block (identical to yours, called the &quot;buddy&quot; block), and put it on the free list of that size.

    Well, they experimented with a similar algorithm, but the idea was that most requests were small. So it took the buddy block and broke it into smaller pieces, one half the size of the request, one a quarter the size, and so on, and put those on their respective free lists. They called this the &quot;donner&quot; malloc, because you carved up your buddy.

    From the way my coworker smiled, I think he thought it was amusing, but I don&#x27;t think he was making it up.

    1. egberts1 · · focus · HN ↗
      D. S. Hirschberg, A Class of Dynamic Memory Allocation, CACM 16(10) 1973 covered this variants of halving Buddy Allocator.
  6. brcmthrowaway · · focus · HN ↗
    What malloc does macOS use in userspace? How about kernel?
    1. egberts1 · · focus · HN ↗
      [delayed]
      1. ddcc7 · · focus · HN ↗
        NanoV2 was only used for smaller size classes. The current allocator is xzone malloc: <a href="https:&#x2F;&#x2F;github.com&#x2F;apple-oss-distributions&#x2F;libmalloc&#x2F;blob&#x2F;main&#x2F;doc&#x2F;xzone_malloc.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;apple-oss-distributions&#x2F;libmalloc&#x2F;blob&#x2F;ma...
        1. egberts1 · · focus · HN ↗
          [delayed]
  7. D2OQZG8l5BI1S06 · · focus · HN ↗
    Every time I tried to replace the allocator in an app, the speedup was negligible.
    1. egberts1 · · focus · HN ↗
      [delayed]
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.