Overriding the standard malloc
(and new
) can be done either dynamically or statically.
This is the recommended way to override the standard malloc interface.
On these ELF-based systems we preload the mimalloc shared library so all calls to the standard malloc
interface are resolved to the mimalloc library.
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.
Note that certain security restrictions may apply when doing this from the shell.
Dynamically overriding on mimalloc 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. As it intercepts all allocation calls on a low level, it can be used reliably on large programs that include other 3rd party components. There are four requirements to make the overriding work well:
/MD
or /MDd
switch).mimalloc.lib
export library for the mimalloc.dll
. (which must be compiled with -DMI_OVERRIDE=ON
, which is the default though). To ensure the mimalloc.dll
is actually 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 command, or similarly, #pragma comment(linker, "/include:mi_version")
in some source file). See the mimalloc-test-override
project for an example on how to use this.mimalloc-redirect.dll
must be put in the same directory as the main mimalloc.dll
at runtime (as it is a dependency of that DLL). The redirection DLL ensures that all calls to the C runtime malloc API get redirected to mimalloc functions (which reside in mimalloc.dll
).mimalloc.dll
comes as early as possible in the import list of the final executable (so it can intercept all potential allocations). You can use minject -l <exe>
to check this if needed.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.
For different platforms than x64, you may need a specific [redirection dll](bin). Furthermore, we cannot always re-link an executable or ensure mimalloc.dll
comes first in the import table. In such cases the [minject
](bin) tool can be used to patch the executable's import tables.
On Unix-like 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.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:
Another way to override statically that works on all platforms, is to link statically to mimalloc (as shown in the introduction) and include a header file in each source file that re-defines malloc
etc. to mi_malloc
. This is provided by mimalloc-override.h
. This only works reliably though if all sources are under your control or otherwise mixing of pointers from different heaps may occur!
The specific functions that get redirected to the mimalloc library are: