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
(on Linux, BSD, etc.)env DYLD_INSERT_LIBRARIES=usr/lib/libmimalloc.dylib myprogram
(On macOS)
Note certain security restrictions may apply when doing this from the shell.
You can set extra environment variables to check that mimalloc is running, like:
or run with the debug version to get detailed statistics:
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()
.
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.
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: