Introduction
mimalloc is a general-purpose allocator developed by Microsoft Research. It achieves performance on par with or better than jemalloc and tcmalloc while using significantly less code and memory overhead. Its design focuses on free-list sharding per mimalloc page, which reduces contention in multi-threaded workloads and limits fragmentation.
What mimalloc Does
- Replaces the system malloc/free with a faster, more scalable implementation
- Reduces memory fragmentation through size-class segregation and eager page reuse
- Provides thread-local heaps that eliminate atomic operations on the fast path
- Supports secure mode with guard pages, randomized allocation, and double-free detection
- Offers a drop-in replacement via LD_PRELOAD without recompiling applications
Architecture Overview
mimalloc organizes memory into segments (large OS-allocated regions) divided into pages (each serving one size class). Each thread owns local pages, so allocation and deallocation on the hot path require no atomic operations. When a thread frees memory belonging to another thread, the pointer is placed on a thread-safe delayed-free list processed lazily. This design keeps the fast path to just a few instructions while maintaining bounded fragmentation.
Self-Hosting & Configuration
- Build from source:
cmake -B build && cmake --build build && sudo cmake --install build - Use as LD_PRELOAD for transparent replacement of system allocator
- Static or dynamic linking; compatible with C and C++ programs
- Configure via environment variables:
MIMALLOC_VERBOSE=1,MIMALLOC_SHOW_STATS=1 - Enable secure mode with
-DMI_SECURE=ONfor guard pages and allocation randomization
Key Features
- Consistent top-tier performance across allocation-heavy benchmarks (cfrac, espresso, redis)
- Compact codebase (~8K lines of C) making it auditable and portable
- Supports huge/large OS pages (2MB / 1GB) for TLB-sensitive workloads
- Built-in statistics and heap visualization for debugging memory behavior
- MIT licensed and used in production at Microsoft, including .NET and Edge
Comparison with Similar Tools
- jemalloc — strong multi-threaded performance but larger and more complex; mimalloc matches speed with less code
- tcmalloc (Google) — thread-caching allocator; similar design philosophy but heavier memory overhead
- glibc ptmalloc2 — the Linux default; significantly slower under contention
- Hoard — academic allocator focusing on scalability; mimalloc achieves similar scaling with simpler design
- snmalloc (Microsoft) — message-passing allocator; higher throughput in some patterns but less portable
FAQ
Q: How do I use mimalloc without recompiling my application?
A: On Linux, set LD_PRELOAD=/usr/lib/libmimalloc.so before launching your program. On macOS, use DYLD_INSERT_LIBRARIES.
Q: Does mimalloc work with C++ new/delete?
A: Yes. When linked or preloaded, it overrides the global operator new and operator delete automatically.
Q: What is secure mode? A: Secure mode adds guard pages around segments, randomizes allocation order, and detects double-frees. It has a small performance cost (~10%) but hardens against heap exploits.
Q: How does mimalloc compare to jemalloc for Redis? A: Benchmarks show mimalloc and jemalloc perform similarly for Redis workloads, with mimalloc using slightly less RSS due to better page reuse.