#1410·mimalloc

当一个线程很少分配和定期释放其他线程的内存时,`malloc_normal.current`/`malloc_huge.current` 会发生偏移

作者: molnsona创建于 2026年9月18日更新于 2026年9月18日

// cc -O2 -pthread repro.c -lmimalloc // (default build, MI_STATS=1) #include <mimalloc.h> #include <mimalloc-stats.h> #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdatomic.h> #include <unistd.h>

mi_stats_t collect_stats() { mi_stats_t s; mi_stats_init(&s); mi_stats_get(&s); return s; }

size_t collect_rss() { size_t rss = 0; mi_process_info(NULL, NULL, NULL, &rss, NULL, NULL, NULL, NULL); return rss; }

#define BLOCK 4096 #define BATCH 1024 #define MiB 1048576.0 static void** mailbox; static atomic_bool full, stop; static atomic_llong live;

static void* producer(void* arg) { while (!stop) { void** b = malloc(BATCH * sizeof(void*)); for (int i = 0; i < BATCH; i++) { b[i] = malloc(BLOCK); memset(b[i], 1, 64); live += mi_usable_size(b[i]); } while (full && !stop); mailbox = b; full = true; } return NULL; }

static void* consumer(void* arg) { // remove this line to fix stats mi_free(mi_malloc(64));

while (!stop) { while …

内容来源: microsoft/mimalloc