mi-malloc 3.6/2.5/1.15
Loading...
Searching...
No Matches
Profiling

Profiling support is experimental and may still change. It is enabled by default, including in release builds, and adds overhead only when activated. The profiling API in <mimalloc-profile.h> supports both custom profiling hooks and the built-in pprof-compatible profiler.

Programmatic Profiling

Explicit profiler APIs remain available in secure builds and when environment activation is disabled. Build with profiling support (-DMI_PROFILE=ON), then create a profiler, attach it, and start sampling:

#include <mimalloc.h>
#include <mimalloc-profile.h>
int main(void) {
512 * 1024, // sample roughly every 512 KiB
"myprofile", // output basename; protobuf format
0, 0, 0); // no automatic snapshots
if (prof == NULL) return 1;
if (!mi_profile(prof)) {
mi_profile(NULL);
return 1;
}
// Run your workload here; join worker threads before cleanup.
mi_profile(NULL); // detach before deleting
return 0;
}
mi_profiler_t * mi_pprof_profiler_new(size_t initial_threshold, const char *base_file_name, size_t alloc_interval_size, size_t inuse_interval_size, size_t time_interval_secs)
experimental: Create the built-in pprof-compatible profiler.
bool mi_profile(mi_profiler_t *profiler)
experimental: Attach a profiler to the current and future heaps of the main subprocess.
bool mi_profiler_start(mi_profiler_t *profiler)
experimental: Start sampling with an attached profiler.
struct mi_profiler_s mi_profiler_t
Profiler type. Custom profiler fields and callback types are defined in <mimalloc-profile....
Definition mimalloc-doc.h:1597
void mi_profiler_snapshot(mi_profiler_t *profiler)
experimental: Take a snapshot of the current profile.
bool mi_profiler_stop(mi_profiler_t *profiler)
experimental: Stop sampling.
void mi_pprof_profiler_delete(mi_profiler_t *profiler)
experimental: Delete a built-in profiler created by mi_pprof_profiler_new().

Stopping sampling does not detach the profiler: its on_free callback can still run for sampled allocations. Detach it from all associated heaps and subprocesses, and ensure no other thread can still invoke its callbacks before deleting it. See Heap Profiling for the complete API.

Environment-based Profiling

The built-in profiler works with Google's pprof tools. When both MI_SECURE=0 and MI_SECURE_ENV=0, and the process is not elevated, it can also be activated through the environment:

  • MIMALLOC_PROFILE=<basename> activates profiling for the main subprocess and writes pprof-compatible <basename>.<seq>.pb files. A .Heaps or .text extension selects the text heap-profile format instead. For a basename of profile, view the results in a browser with pprof -http=:8080 ./myprogram profile.*.
  • MIMALLOC_PROFILE_ALLOC_INTERVAL=N sets the approximate number of allocated bytes between automatic snapshots (default: 1GiB; 0 disables).
  • MIMALLOC_PROFILE_INUSE_INTERVAL=N takes a snapshot when in-use bytes grow by that amount (default: 0, disabled).
  • MIMALLOC_PROFILE_TIME_INTERVAL=N sets seconds between automatic snapshots, checked on allocation events (default: 0, disabled).
  • MIMALLOC_PROFILE_SAMPLE_RATE=N sets the average number of allocated bytes between samples, using a Poisson distribution (default: 512KiB).