Unable to override `new/delete` on macOS
I was trying to use mimalloc debug mode to see statistics. Here's my simple test program.
#include <mimalloc-new-delete.h>
#include <mimalloc-override.h>
#include <vector>
int main() {
std::vector<int, mi_stl_allocator<int>> vec(10000, 0);
[[maybe_unused]] auto x = mi_new(1000);
[[maybe_unused]] auto y = new int[1000];
[[maybe_unused]] auto z = malloc(500);
}
The report is shown below. It seems that new operator is not overrided by mimalloc.
heap stats: peak total freed current unit count
normal 20: 512 B 512 B 0 512 B 512 B 1 not all freed
normal 24: 1.0 KiB 1.0 KiB 0 1.0 KiB 1.0 KiB 1 not all freed
normal 45: 40.1 KiB 40.1 KiB 40.1 KiB 0 40.1 KiB 1 ok
heap stats: peak total freed current unit count
normal: 41.6 Ki 41.6 Ki 40.1 Ki 1.4 Ki 13.8 KiB 3 not all freed!
large: 0 0 0 0 ok
huge: 0 0 0 0 ok
total: 41.6 KiB 41.6 KiB 40.1 KiB 1.4 KiB not all freed
malloc req: 40.6 KiB 40.6 KiB 39.2 KiB 1.4 KiB not all freed
reserved: 1.0 GiB 1.0 GiB 0 1.0 GiB
committed: 1.0 GiB 1.0 GiB 0 1.0 GiB
reset: 0
purged: 0
touched: 232.9 KiB 232.9 KiB 160.6 KiB 72.2 KiB not all freed
segments: 1 1 0 1 not all freed!
-abandoned: 0 0 0 0 ok
-cached: 0 0 0 0 ok
pages: 3 3 1 2 not all freed!
-abandoned: 0 0 0 0 ok
-extended: 3
-noretire: 1
mmaps: 2
commits: 0
resets: 0
purges: 0
threads: 0 0 0 0 ok
searches: 0.0 avg
numa nodes: 1
elapsed: 0.000 s
process: user: 0.014 s, system: 0.007 s, faults: 0, rss: 2.9 MiB, commit: 1.0 GiB
Compiling & Running:
clang++ -std=c++20 -Wall -Wextra -Wpedantic -fuse-ld=lld -Og t.cc -lmimalloc-debug -o t
env MIMALLOC_SHOW_STATS=1 DYLD_INSERT_LIBRARIES=/usr/local/lib/libmimalloc-debug.dylib ./t
Clang version:
Homebrew clang version 16.0.4
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
System: macOS Monterey version 12.5
In addition, I compiled mimalloc with -DMI_OVERRIDE=OFF.
I've been struggling with a similar problem; Clang related but not on macOS.
Apparently, Clang has builtin functions for operator new / delete. When the builtin function is used, it bypasses any the mimalloc override.
Please try running your example with -fno-bultin to see whether this makes a difference?
However, the bad news is that -fno-builtin will disable all of Clang's builtin functions. You probably don't want that. Furthermore, there does not seem to be a way of disabling builtin operator new / delete explicitly (src).
You could try -fno-builtin-malloc and -fno-builtin-free and see whether that works.
Edit: Also check the resulting binary using a disassembler. The compiler might remove allocations due to optimization.