Configs2026年5月26日·1 分钟阅读

mimalloc — Compact General-Purpose Memory Allocator by Microsoft

A high-performance, compact general-purpose allocator from Microsoft Research with excellent multi-threaded scaling and minimal fragmentation.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
mimalloc Overview
先审查命令
npx -y tokrepo@latest install e3727a4e-5898-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

mimalloc is a general-purpose allocator developed by Microsoft Research. It achieves performance on par with or better than jemalloc and tcmalloc while using significantly less code and memory overhead. Its design focuses on free-list sharding per mimalloc page, which reduces contention in multi-threaded workloads and limits fragmentation.

What mimalloc Does

  • Replaces the system malloc/free with a faster, more scalable implementation
  • Reduces memory fragmentation through size-class segregation and eager page reuse
  • Provides thread-local heaps that eliminate atomic operations on the fast path
  • Supports secure mode with guard pages, randomized allocation, and double-free detection
  • Offers a drop-in replacement via LD_PRELOAD without recompiling applications

Architecture Overview

mimalloc organizes memory into segments (large OS-allocated regions) divided into pages (each serving one size class). Each thread owns local pages, so allocation and deallocation on the hot path require no atomic operations. When a thread frees memory belonging to another thread, the pointer is placed on a thread-safe delayed-free list processed lazily. This design keeps the fast path to just a few instructions while maintaining bounded fragmentation.

Self-Hosting & Configuration

  • Build from source: cmake -B build && cmake --build build && sudo cmake --install build
  • Use as LD_PRELOAD for transparent replacement of system allocator
  • Static or dynamic linking; compatible with C and C++ programs
  • Configure via environment variables: MIMALLOC_VERBOSE=1, MIMALLOC_SHOW_STATS=1
  • Enable secure mode with -DMI_SECURE=ON for guard pages and allocation randomization

Key Features

  • Consistent top-tier performance across allocation-heavy benchmarks (cfrac, espresso, redis)
  • Compact codebase (~8K lines of C) making it auditable and portable
  • Supports huge/large OS pages (2MB / 1GB) for TLB-sensitive workloads
  • Built-in statistics and heap visualization for debugging memory behavior
  • MIT licensed and used in production at Microsoft, including .NET and Edge

Comparison with Similar Tools

  • jemalloc — strong multi-threaded performance but larger and more complex; mimalloc matches speed with less code
  • tcmalloc (Google) — thread-caching allocator; similar design philosophy but heavier memory overhead
  • glibc ptmalloc2 — the Linux default; significantly slower under contention
  • Hoard — academic allocator focusing on scalability; mimalloc achieves similar scaling with simpler design
  • snmalloc (Microsoft) — message-passing allocator; higher throughput in some patterns but less portable

FAQ

Q: How do I use mimalloc without recompiling my application? A: On Linux, set LD_PRELOAD=/usr/lib/libmimalloc.so before launching your program. On macOS, use DYLD_INSERT_LIBRARIES.

Q: Does mimalloc work with C++ new/delete? A: Yes. When linked or preloaded, it overrides the global operator new and operator delete automatically.

Q: What is secure mode? A: Secure mode adds guard pages around segments, randomizes allocation order, and detects double-frees. It has a small performance cost (~10%) but hardens against heap exploits.

Q: How does mimalloc compare to jemalloc for Redis? A: Benchmarks show mimalloc and jemalloc perform similarly for Redis workloads, with mimalloc using slightly less RSS due to better page reuse.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产