Skip to content
systemcoding.dev
Performance

Print mimalloc statistics at exit with MIMALLOC_SHOW_STATS=1

Set MIMALLOC_SHOW_STATS=1 and mimalloc 2.1 prints reserved, committed and peak heap numbers to stderr when the process exits, with no code changes.

1 min read
Summarize in AI

You want to know how much memory a program really asks mimalloc for, without editing it. The allocator reads its options from the environment, and MIMALLOC_SHOW_STATS=1 turns on a statistics report that is printed from an exit hook.

Preload the shared library into an unmodified binary and run it:

Shell
MIMALLOC_SHOW_STATS=1 LD_PRELOAD=/usr/lib/libmimalloc.so ./myapp

The report goes to stderr, so it does not mix with the program's own output. The block starts like this (the numbers, units and the exact set of rows depend on the program and the mimalloc build):

Text
heap stats:     peak       total     current       block     total#
  reserved:   64.0 MiB    64.0 MiB    64.0 MiB
  committed:  12.3 MiB    13.1 MiB     1.9 MiB

The gap between reserved and committed is the part the manual's option table does not make obvious: mimalloc reserves address space in large chunks and only commits what the heap touches, so reserved is not your memory footprint. Read peak on the committed row for that.

The option list is in the mimalloc environment options page. If the process ends through _exit or a fatal signal, the exit hook never runs and nothing is printed. Call mi_stats_print(NULL) from your own code when you need the numbers at a chosen moment instead.

Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.