What is malloc doing, anyway?
Points and comments are a snapshot, not live.
A deep dive into how malloc works, from syscalls to free lists.
The article explains how malloc works under the hood, focusing on glibc. It demonstrates that malloc avoids expensive syscalls by requesting large blocks (e.g., 132 KiB) via brk and managing them in userspace. It builds a toy bump allocator, then a freelist-based allocator with splitting and coalescing, and finally an allocator using mmap for large blocks. Benchmarks show glibc's malloc (0.0789 µs per step) is 65-150x faster than the toy allocators due to size-class bins and a thread-local cache.
What commenters are saying
Commenters largely appreciated the article as a clear introduction to memory allocation. Some argued that avoiding malloc entirely isn't practical, but stack-only languages like Goose offer an interesting alternative. Others pointed out that brk is outdated and mmap is the modern approach. One commenter noted that glibc's performance advantage comes from its sophisticated data structures, not from magic, and that the article's toy allocators highlight the trade-offs involved.