A glibc-based malloc replacement for Linux that learns when to return freed heap pages to the system.
hymalloc targets the high RSS that stays after free(), as an
alternative to calling malloc_trim() on a timer. Load it with
LD_PRELOAD: each arena learns how long freed memory stays unused, and
releases it at a cost that targets a share of the run time, 1% per
arena by default.
In these tests, with the default 1% budget, the program used 85%
less memory than with the system glibc while working after a peak, at
the same speed, and less than with malloc_trim each second; its
slowest free and malloc took 1.6 ms, against 21.7 ms. While idle after
longer work, it kept 2.2% of the freed memory resident, against 33%.
| Allocator | RSS after a peak | Slowest free and malloc after a peak | Speed after a peak | RSS with 8 threads | p99.9 with 8 threads | Freed memory resident while idle after longer work |
|---|---|---|---|---|---|---|
| System glibc 2.39 | 1015 MiB | 1.7 ms | 1144k/s | 1941 MiB | 28.0 µs | 100% |
System glibc, malloc_trim each second |
163 MiB | 21.7 ms | 1146k/s | 348 MiB | 28.3 µs | 33% |
| hymalloc, 1% (default) | 153 MiB | 1.6 ms | 1147k/s | 344 MiB | 29.3 µs | 2.2% (1.3–31% across 12 runs) |
| hymalloc, 5% | 141 MiB | 5.0 ms | 1108k/s | 333 MiB | 42.2 µs | 1.1% |
System glibc, MALLOC_MMAP_THRESHOLD_=65536 |
837 MiB | 1.1 ms | 1150k/s | 1038 MiB | 27.5 µs | 0% |
| jemalloc 5.3 | 453 MiB | 0.7 ms | 942k/s | 914 MiB | 27.9 µs | 100% |
mimalloc 3.7, MIMALLOC_PURGE_DELAY=0 |
385 MiB | 0.8 ms | 531k/s | 714 MiB | 43.8 µs | 0% |
| tcmalloc (gperftools 2.15, minimal) | 1083 MiB | 0.7 ms | 1306k/s | 1908 MiB | 29.0 µs | 100% |
hymalloc's default is in bold. Lower is better in every column except
speed. Apple M5, Docker Linux VM (aarch64), Ubuntu 24.04. All columns
but the last are medians of three runs, two with 8 threads, made
together. In the last column, malloc_trim each second and hymalloc
come from 12 later rounds made together, and the other allocators from
two rounds of the first runs; each value is the median over all 1.5 s
waits, 72 in the 12 rounds, and 1.3–31% is the range of the medians of
the rounds.
BENCHMARKS.md describes the tests and gives more
results.
On Ubuntu 22.04, 24.04, 26.04 and 26.10, for x86_64 and arm64, install it from the PPA:
sudo add-apt-repository ppa:denny0707/hymalloc
sudo apt install libhymalloc0
LD_PRELOAD=/usr/lib/$(uname -m)-linux-gnu/libhymalloc.so.0 ./programOr build it from the source:
make
LD_PRELOAD=$PWD/libhymalloc.so ./programRun this from the top of the repository, whose path must not contain
spaces or colons, as LD_PRELOAD splits on them. After make install,
which installs the library in /usr/local/lib, a program can also be
linked with -lhymalloc.
The default budget is a target of 1% of the run time for each arena.
glibc.malloc.trim_overhead sets
it in tenths of a percent: GLIBC_TUNABLES=glibc.malloc.trim_overhead=50
is 5%, and 0 turns the release off. These glibc tunables and
variables work as in glibc: top_pad, perturb, mmap_threshold,
trim_threshold, mmap_max, arena_max, arena_test, tcache_max,
tcache_count and hugetlb, and the MALLOC_* variables of the
first seven. MALLOC_CHECK_ and glibc.malloc.check are not
supported.
HYMALLOC_VERBOSE=1 prints one line when malloc is set up in a program
that uses the library, and ./libhymalloc.so prints its version.
make check runs the tests.
hymalloc needs Linux and glibc 2.34 or later. The tests pass on x86_64 with Ubuntu 24.04, and on aarch64 with Ubuntu 24.04, Ubuntu 22.04 and Debian 12.
- A free chunk of 64 KiB or more records when it became free.
- When such memory is allocated again, the arena counts its pages and how long they had been free.
- At most once a second, when a large chunk is freed, the arena picks the shortest delay at which the page faults on memory used again would have cost no more than the budget. The cost of a page fault is measured, not assumed.
- The arena earns the budget as time passes and saves what it does not spend for up to 10 s. Releasing pages and the page faults are paid from it, so memory freed after a peak usually goes back within a few seconds.
- When a large chunk is freed, chunks that have been free for longer
than the delay have their whole pages released with
MADV_DONTNEED, asmalloc_trimdoes.
The budget is a target for each arena, not a hard limit: one release can take longer than what is left of it, and page faults are only counted after they have happened.
- Memory is released inside
free. A program that stops freeing memory keeps it until its next largefreeormalloc_trim. - The budget is per arena. It does not cover recording when chunks became free, nor costs to other threads, such as TLB flushes.
- hymalloc does not make allocation faster than glibc. With 5%, the page faults made the program about 3% slower after a peak.
- The arena learns from what the program did before. When memory that
was used again every few hundred milliseconds starts to stay idle for
longer, the first one or two waits still keep most of it: after
longer work, with 1%, 11 of 12 rounds kept less resident than
malloc_trimeach second, and one kept more, 31% against 18%. - With 5%, memory used again a few milliseconds after it became free
is still released, which costs more page faults than
malloc_trimeach second. - Preload the library or link with it; it cannot be loaded with
dlopen. - In a program with several threads,
forkcan rarely hang. The library locks malloc in a fork handler, before glibc takes some locks of its own infork; if another thread holds one of those and allocates at that moment, for instance while it callspopenorpthread_atfork, the two wait for each other. The lock of the stdio stream list, the case of glibc bug 19431, is taken first and does not hang. jemalloc and the tcmalloc of gperftools lock the same way. glibc locks its own malloc last, so the change as part of glibc, on theglibcbranch, does not have this limit.
Questions and bug reports are welcome in GitHub issues.
src/glibc: the malloc of glibc 2.44 with the patches of theglibcbranch, copied byscripts/sync-glibc.sh.src/compat,src/hymalloc.c: what these files need from the rest of glibc, and the setup that glibc does outside malloc.tests,bench,scripts: tests, benchmarks and the scripts that run them.BENCHMARKS.md: the tests behind the table above, and more results.
The glibc branch holds the same change as patches for glibc 2.44.
LGPL-2.1-or-later, the same as glibc. See AUTHORS for who wrote the
malloc of glibc.