Overriding the standard malloc
can be done either dynamically or statically.
This is the recommended way to override the standard malloc interface.
On these systems we preload the mimalloc shared library so all calls to the standard malloc
interface are resolved to the mimalloc library.
env LD_PRELOAD=/usr/lib/libmimalloc.so myprogram
You can set extra environment variables to check that mimalloc is running, like:
or run with the debug version to get detailed statistics:
On macOS we can also preload the mimalloc shared library so all calls to the standard malloc
interface are resolved to the mimalloc library.
env DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=/usr/lib/libmimalloc.dylib myprogram
Note that certain security restrictions may apply when doing this from the shell.
Note: unfortunately, at this time, dynamic overriding on macOS seems broken but it is actively worked on to fix this (see issue #50
).
On Windows you need to link your program explicitly with the mimalloc DLL, and use the C-runtime library as a DLL (the /MD
or /MDd
switch). To ensure the mimalloc DLL gets loaded it is easiest to insert some call to the mimalloc API in the main
function, like mi_version()
(or use the /INCLUDE:mi_version
switch on the linker)
Due to the way mimalloc intercepts the standard malloc at runtime, it is best to link to the mimalloc import library first on the command line so it gets loaded right after the universal C runtime DLL (ucrtbase
). See the mimalloc-override-test
project for an example.
Note: the current overriding on Windows works for most programs but some programs still have trouble – the dev-exp
branch contains a newer way of overriding that is more robust; try this out if you experience troubles.
On Unix systems, you can also statically link with mimalloc to override the standard malloc interface. The recommended way is to link the final program with the mimalloc single object file (mimalloc-override.o
). We use an object file instead of a library file as linkers give preference to that over archives to resolve symbols. To ensure that the standard malloc interface resolves to the mimalloc library, link it as the first object file. For example:
The specific functions that get redirected to the mimalloc library are: