Skip to content

Guide · Open source

How to use mimalloc: install, run as malloc replacement and tips

Updated · written with AI from the project's own documentation and checked against our catalogue data

mimalloc is a memory allocator, the part of a program that hands out and reclaims memory while it runs. It is written in C and is designed as a drop-in replacement for the standard malloc, so it can often be used without changing program code. This guide covers what it is, how to get the source, how to swap it in on Linux and Windows, and which version to pick.

mimallocmicrosoft/mimalloc · ★ 13.5k · MIT

Quick start

  1. 1Clone the repository. Run git clone https://github.com/microsoft/mimalloc to get the source.
  2. 2Check prerequisites. Make sure you have a C compiler and CMake, or download a prebuilt release archive for your platform.
  3. 3Build the library. Build mimalloc for your platform following the repository instructions.
  4. 4Choose a version. Use v3 for new work, as the README recommends it over the legacy v1 and v2 lines.
  5. 5Preload on Linux. Start your program with LD_PRELOAD pointing at libmimalloc.so, without changing the program's code.
  6. 6Override on Windows. Follow the Windows overriding section of the README to replace the default allocator dynamically.

What mimalloc does and who it is for

mimalloc is a general purpose allocator. The README describes it as compact, about 10k lines of C, and designed for good performance and low fragmentation. It runs on many platforms, including Windows, macOS, Linux, WASM, various BSDs, Haiku and MUSL.

It is most useful for developers who want to try a faster or lower-memory malloc replacement in an existing C or C++ program, and for teams running long-lived or multithreaded server software that suffers from memory fragmentation. Runtime implementers who want an embeddable allocator they can adapt will also find it relevant.

Get the mimalloc source code

The README's install step is to clone the repository. You need a C compiler and CMake to build from source, or you can use a prebuilt release archive for your platform. The source does not list other build prerequisites, so check the repository for details.

git clone https://github.com/microsoft/mimalloc

Use mimalloc on Linux with LD_PRELOAD

On dynamically linked ELF-based systems such as Linux and BSD, you can use mimalloc without recompiling your program by preloading the shared library. Replace the path with the location where your built libmimalloc.so is installed.

This works with programs you did not write, because the loader substitutes mimalloc for the default allocator at startup.

LD_PRELOAD=/usr/lib/libmimalloc.so myprogram

Use mimalloc on Windows with dynamic overriding

mimalloc includes a way to dynamically override the default allocator on Windows. The README links to a dedicated section on overriding on Windows, which is the place to follow for the exact steps.

Choose the right mimalloc version

The README lists three maintained versions. It recommends v3 for new work, describes v2 as a stable legacy version, and describes v1 as the initial legacy design. New development happens on v3.

v3 simplifies the lock-free design of earlier versions, improves memory sharing between threads, supports true first-class heaps that can allocate from any thread, and may use much less memory on certain large workloads.

  • v3: recommended, development branch dev3
  • v2: stable legacy, development branch dev2 and main
  • v1: legacy, development branch dev; the README asks that PRs target v1 if possible, otherwise dev3

Enable secure mode only if you need it

mimalloc can be built in secure mode, which adds guard pages, randomized allocation and encrypted free lists to protect against heap vulnerabilities. The README says the performance penalty is usually around 10% on average over its benchmarks, so weigh that cost before enabling it.

What people use it for

  • Trying a drop-in malloc replacement on an existing C or C++ server without code changes
  • Reducing memory fragmentation in long-running multithreaded services
  • Embedding a small allocator in a language runtime or runtime system
  • Organising allocations across multiple heaps and destroying a heap in one call

Frequently asked questions

Is mimalloc free to use?

mimalloc is released under the MIT licence, as stated on its repository. The MIT licence permits use, modification and distribution under its terms; read the LICENSE file in the repository for the full text.

What do I need to install mimalloc?

The README lists a C compiler and CMake as prerequisites for building from source. Alternatively, you can use a prebuilt release archive for your platform. Start by cloning the repository with git clone https://github.com/microsoft/mimalloc.

How do I use mimalloc without changing my code?

On dynamically linked ELF-based systems such as Linux and BSD, you can set LD_PRELOAD to the path of libmimalloc.so when you run your program. On Windows, the README describes a way to dynamically override the default allocator.

Which mimalloc version should I use?

The README recommends v3 for new work. v2 is described as a stable legacy version, and v1 as a legacy version that receives security and bug fixes.

How does mimalloc compare to jemalloc, tcmalloc and Hoard?

The README says that in its benchmarks mimalloc outperforms jemalloc, tcmalloc, Hoard and other leading allocators, and often uses less memory. Benchmarks depend on your workload, so test with your own program before switching.

Should I use mimalloc if the default allocator is fine?

Probably not. The README notes that the benefit may be small if you only need the default system allocator and have no performance or memory concerns. Test it first if you do need better speed or lower memory use.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

mimalloc suits C and C++ developers who want a compact, drop-in malloc replacement for performance or fragmentation problems, especially in long-running multithreaded servers. If the default allocator already meets your needs, or you need secure mode without a performance cost, consider another option, and check the README's comparison with jemalloc, tcmalloc, Hoard and snmalloc.

Install options & downloads