mimalloc C++ Guide: High-Performance Memory Allocator

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

mimalloc for C++: A High-Performance Memory Allocator

In modern C++ development, memory management remains a key factor affecting application performance. The standard allocator (malloc from glibc or jemalloc) often struggles under heavy loads in multithreaded environments or during intensive allocation/deallocation of small blocks. mimalloc (short for "microsoft mimalloc") is a compact yet extremely fast general-purpose allocator developed by Microsoft Research. It delivers impressive results: up to 2-3x faster than malloc in multithreaded scenarios with significantly lower memory consumption.

Why use mimalloc? First, it's optimized for modern multi-core processors: it uses thread-local caches and minimizes contention for global locks. Second, it effectively combats memory fragmentation through a "free list sharding" algorithm — splitting free block lists by size and thread. Third, mimalloc has built-in protection against certain types of vulnerabilities (double-free, use-after-free) and supports "guarded allocations" for debugging. This makes it an ideal choice for high-load servers, game engines, databases, and any C++ code where memory performance matters.

In this article, we'll walk through how to install mimalloc, use its core functions, and provide real-world integration examples for C++ projects. You'll learn how to replace the standard malloc with just one line of code and gain a 30-50% performance boost without changing your business logic.

Installation

You can install mimalloc in several ways. The recommended approach is via the vcpkg or Conan package manager, or by building from source.

Via vcpkg (recommended for Windows/Linux/macOS)

# Install vcpkg if you haven't already
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh  # or .\bootstrap-vcpkg.bat on Windows
./vcpkg integrate install

# Install mimalloc
./vcpkg install mimalloc

Via Conan

conan install mimalloc/2.1.6@

Build from source (Linux/macOS)

git clone https://github.com/microsoft/mimalloc
cd mimalloc
mkdir -p build && cd build
cmake ..
make -j$(nproc)
sudo make install

Integrate with CMake

find_package(mimalloc REQUIRED)
target_link_libraries(my_target PRIVATE mimalloc::mimalloc)

Quick Start: Minimal Working Example

The easiest way to start using mimalloc is to replace the standard malloc/new with its version. Simply override the global new/delete operators.

#include <iostream>
#include <mimalloc.h>
#include <mimalloc-new-delete.h>  // overrides global new/delete

int main() {
    // Now all new/delete calls use mimalloc
    int* arr = new int[1000];
    for (int i = 0; i < 1000; ++i) arr[i] = i;
    std::cout << "Sum: " << arr[500] << std::endl;
    delete[] arr;

    // You can also use C functions directly
    void* p = mi_malloc(64);
    mi_free(p);

    return 0;
}

Compile by linking to mimalloc:

g++ -std=c++17 main.cpp -lmimalloc -o test

Core Methods and Functions

mimalloc provides both a C-compatible API (functions prefixed with mi_) and C++ wrappers. Let's explore 8 key functions.

1. mi_malloc — Allocate Memory

Signature: void* mi_malloc(size_t size)

What it does: Allocates a memory block of size bytes. An analog to the standard malloc, but optimized for multithreading.

#                    

Recommendations