A modern, high-concurrency custom memory pool in C++ inspired by Google's TCMalloc architecture. This project is designed to eliminate global lock contention and kernel-space transition overheads that plague standard system malloc under high-concurrency, frequent small-object allocation workloads.
By implementing a three-tier caching architecture (ThreadCache, CentralCache, and PageHeap), combined with lock-free hot paths and optimized metadata management, this memory pool achieves extreme throughput and rock-solid stability under heavy loads.
The memory pool adopts a classic three-tiered hierarchy to filter out the vast majority of requests that would otherwise hit the operating system kernel:
[ Worker Thread 1 ] [ Worker Thread 2 ] [ Worker Thread N ] (16 High-Concurrency Threads)
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ ThreadCache │ │ ThreadCache │ │ ThreadCache │ (Lock-free Fast Path, thread_local)
└────────┬────────┘ └────────┬────────┘ └────────┬────────┘
│ (Batch fetch / return on miss) │
└─────────────┬─────────────────────────────┘
▼
┌───────────────────┐
│ CentralCache │ (Bucket-level mutexes, manages Spans by Size Class)
└────────┬──────────┘
│ (Requests new Spans when exhausted)
▼
┌───────────────────┐
│ PageHeap │ (Page-level memory management, caching & reuse)
└────────┬──────────┘
│ (Requests raw memory via mmap when empty)
▼
┌───────────────────┐
│ Linux Kernel │ (Underlying OS memory pages)
└───────────────────┘
- Lock-Free Hot Path:
- Small-object allocations (up to 256 KB) are served directly via
thread_localThreadCaches. Multiple threads operate concurrently without interference, completely bypassing global lock contention.
- Small-object allocations (up to 256 KB) are served directly via
- High-Speed Metadata Indexing (
FastPageMap):- Replaced slow, lock-heavy
std::unordered_maplookups with a two-level fixed-size chunked array (Radix Tree style), achieving O(1) lock-free mapping from page addresses toSpanmetadata.
- Replaced slow, lock-heavy
- Memory Pooling & Reuse (Page Heap Caching):
- Implements page-level caching. Freed memory is recycled into PageHeap's free span lists instead of being immediately returned via
munmap, drastically reducing costly kernel-space context switches.
- Implements page-level caching. Freed memory is recycled into PageHeap's free span lists instead of being immediately returned via
- Extreme Robustness:
- Verified through 16-thread, 30-minute ultra-high-intensity stress tests, demonstrating zero memory fragmentation buildup and zero memory leaks.
The project is evaluated using Google Benchmark for micro-benchmarks and a custom 16-thread, 30-minute macro stress test. Quantitative results are summarized below:
| Workload Scenario | Threads | Latency / Allocation (Time) | Comparison vs System malloc |
|---|---|---|---|
| FixedSmall (32B) | 1 Thread | 9.67 ns | Significantly faster |
| FixedSmall (32B) | 8 Threads | 3.23 ns (CPU: 14.1 ns) | Zero-lock ThreadCache hit advantage |
| MixedWorkload | 1 Thread | 22.5 ns | Highly efficient |
| MixedWorkload | 16 Threads | 9.07 ns (CPU: 45.5 ns) | Outperforms system malloc (~2.4x) |
Test Environment: Linux 4-Core CPU / 16 Concurrent Worker Threads / Dynamic Mixed Allocations (16B ~ 256B)
| Performance Metric | Quantitative Test Result | Industrial Assessment |
|---|---|---|
| Total Operations (Total Ops) | 294,189,133,229 ops (~294 Billion) | Sustained high throughput without degradation |
| Average Throughput (Overall QPS) | 163,426,439.30 ops/sec (~163M ops/sec) | Exceptional multi-core scalability |
| Average Allocation Latency | Stable between 170 ns ~ 230 ns | Ultra-low jitter and deterministic latency |
| Memory Leaks & Stability | 0 Memory Leaks / 0 Crashes | Successfully passed 30-minute endurance test |
- C++17 compatible compiler (GCC 7+)
- CMake 3.10+
- Google Benchmark (Optional, for micro-benchmarks)