Author builds four allocators with sbrk to show what glibc malloc trades away
malloc's interface is simple to the point of magic: ask for memory, get a pointer; after free, the memory is gone. But user-space programs can only get memory through system calls, and one empty system call measured 575 nanoseconds, so going into the kernel on every malloc would make small allocations absurdly slow. The author wanted to find out what glibc does behind the scenes and what each of those choices costs. The original does not say which allocator design is best overall.
malloc's interface is simple to the point of magic: ask for memory, get a pointer; after free, the memory is gone. But user-space programs can only get memory through system calls, and one empty system call measured 575 nanoseconds, so going into the kernel on every malloc would make small allocations absurdly slow. The author wanted to find out what glibc does behind the scenes and what each of those choices costs.
The author watched with strace: 1000 consecutive malloc(16) calls triggered only two brk system calls, the first asking where the top of the heap is and the second growing the heap by 132 KiB, and the other 999 calls were carved out of that memory. Following this lead, the author used sbrk, the libc wrapper that moves the top of the heap, to build four allocators step by step. The first is a bump allocator that moves the top of the heap on every request; it measured 610 nanoseconds per call, 22.7 times slower than malloc's 26.9 nanoseconds, and it never reuses freed memory. The second adds a block header, which records the size so free knows how much to reclaim, and a free list, and it merges adjacent free blocks on free; memory use dropped from 1425 MiB to 17.8 MiB, but every allocation and free has to walk the list. The third adds trim: when the free block at the top of the heap exceeds 128 KiB, it lowers the top of the heap to return memory to the kernel, leaving only 0.188 MiB after everything is freed, at the cost of one more list walk on every free. The fourth switches requests above 128 KiB to a separate mmap mapping and munmaps it immediately on free, giving the lowest memory use while active. In the benchmark, glibc took 0.0789 microseconds per step, 65 to 142 times faster than the author's allocators, because it looks up memory by size in bins and uses a thread-local tcache that caches up to 7 just-freed blocks of each small size and hands them straight to the next request of that size. The cost is that blocks in tcache count as in use, so glibc still held 19.0 MiB after everything was freed; turning tcache off brought that down to 0.250 MiB but made it 1.5 times slower. glibc also raises the threshold after freeing an mmap block so later large requests go back to the heap, making only 51 mmap calls in the whole run; fixing the threshold at 128 KiB made it 10.7 times slower.
malloc's core strategy is to trade memory for system calls: it carves up large blocks in user space so most allocations never touch the kernel. The author's data shows that the trade-off between speed and resident memory can be tuned through tcache and the mmap threshold, and that long-running services should watch for RSS held onto by fragmentation and caching.