mimalloc: A High-Performance Scalable Memory Allocator for Modern Times
Key point
mimalloc is an open-source allocator that serves as a drop-in replacement for malloc/free, optimized for large-scale multithreading.
Details
mimalloc is an open-source memory allocator designed as a drop-in replacement for malloc/free. It was first created in 2020 at MSR RiSE for Lean and Koka, and is now used across Microsoft's internal services and external projects. It improved response times for services like Bing, and has been open-sourced on GitHub with 12K+ stars. Its goal is to bound worst-case allocation time and space overhead while reducing internal fragmentation and contention.
Its scope of application is wide.
- Used as the allocator for NoGIL CPython 3.13+.
- Integrated into Unreal Engine.
- Also used in the game Death Stranding.
- The Rust wrapper is downloaded over 100,000 times a day.
The codebase is small, at about 12K lines, with clear internal data structures that make integration easy. The core structure is a thread-local heap called theap that each thread has, and within it, a 64 KiB page. Each page divides fixed-size blocks into size classes, and most allocations and frees are handled without synchronization. Small allocations take a fast path that pulls a block directly from the free list, and theap and small_pages are initialized to a special empty structure to reduce unnecessary branching. On x64, only two uncommon branches remain.
For freeing, if it's the same thread, it pushes to local_free; if it's a different thread, it's inserted into thread_free via CAS (compare-and-swap). In the latest mimalloc v3, mi_ptr_page uses an on-demand allocated memory map to more safely filter out invalid pointers that come in when the global free is overwritten, and the used count allows fast determination of whether an entire page can be freed.
The approach of keeping 3 free lists—free, local_free, and thread_free—per page lowers the chance of collisions even as thousands of lists are created. Thanks to this structure, mimalloc scales broadly from small apps to large-scale services exceeding 500 GiB, achieving both cache locality and scalability. It's closer to a randomization-style design that lowers collision probability by using many independent lists instead of complex shared data structures.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.