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 (using the /MD
or /MDd
switch). Moreover, you need to ensure the mimalloc-redirect.dll
(or mimalloc-redirect32.dll
) is available in the same folder as the mimalloc DLL at runtime (as it as referred to by the mimalloc DLL). The redirection DLL's ensure all calls to the C runtime malloc API get redirected to mimalloc.
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.
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 successfully redirected.
(Note: in principle, it should be possible to patch existing executables that are linked with the dynamic C runtime (ucrtbase.dll
) by just putting the mimalloc DLL into the import table (and putting 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: