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: macOS support for dynamic overriding is recent, please report any issues.)
Overriding on Windows is robust and has the particular advantage to be able to redirect all malloc/free calls that go through the (dynamic) C runtime allocator, including those from other DLL's or libraries.
The overriding on Windows requires that you link your program explicitly with the mimalloc DLL and use the C-runtime library as a DLL (using the /MD
or /MDd
switch). Also, the mimalloc-redirect.dll
(or mimalloc-redirect32.dll
) must be available in the same folder as the main mimalloc-override.dll
at runtime (as it is a dependency). The redirection DLL ensures that all calls to the C runtime malloc API get redirected to mimalloc (in mimalloc-override.dll
).
To ensure the mimalloc DLL is loaded at run-time 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). See the mimalloc-override-test
project for an example on how to use this. For best performance on Windows with C++, it is also recommended to also override the new
/delete
operations (by including mimalloc-new-delete.h
a single(!) source file in your project).
The environment variable MIMALLOC_DISABLE_REDIRECT=1
can be used to disable dynamic overriding at run-time. Use MIMALLOC_VERBOSE=1
to check if mimalloc was successfully redirected.
(Note: in principle, it is possible to even patch existing executables without any recompilation if they are linked with the dynamic C runtime (ucrtbase.dll
) – just put the mimalloc-override.dll
into the import table (and put mimalloc-redirect.dll
in the same folder) Such patching can be done for example with CFF Explorer).
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: