The preferred usage is including <mimalloc.h>
, linking with the shared- or static library, and using the mi_malloc
API exclusively for allocation. For example,
mimalloc uses only safe OS calls (mmap
and VirtualAlloc
) and can co-exist with other allocators linked to the same program. If you use cmake
, you can simply use:
in your CMakeLists.txt
to find a locally installed mimalloc. Then use either:
to link with the shared (dynamic) library, or:
to link with the static library. See test\CMakeLists.txt
for an example.
For best performance in C++ programs, it is also recommended to override the global new
and delete
operators. For convience, mimalloc provides mimalloc-new-delete.h
which does this for you – just include it in a single(!) source file in your project.
In C++, mimalloc also provides the mi_stl_allocator
struct which implements the std::allocator
interface. For example:
You can pass environment variables to print verbose messages (MIMALLOC_VERBOSE=1
) and statistics (MIMALLOC_SHOW_STATS=1
) (in the debug version):
The above model of using the mi_
prefixed API is not always possible though in existing programs that already use the standard malloc interface, and another option is to override the standard malloc interface completely and redirect all calls to the mimalloc library instead.
See Overriding Malloc for more info.