What is malloc doing, anyway?(benmandrew.com)
255 points by benmandrew 2 days ago | 64 comments
tl;dr: The article walks through building a toy malloc from scratch using sbrk and mmap, explaining chunk headers, free lists, splitting/coalescing, demand paging, and trimming the heap back to the kernel. Benchmarks show the toy allocators run 65-150x slower than glibc, which stays fast by using size-segregated bins, a thread-local cache (tcache) of recently freed chunks, and an adaptive mmap threshold—trading memory overhead to avoid syscalls on the hot path.
HN Discussion:
  • ↓The article describes an obsolete brk-based allocator; modern allocators like jemalloc/mimalloc work differently
  • ~Recommends alternative learning resources for understanding modern allocators and memory management
  • ↑Shares personal experience building custom malloc implementations as valuable learning exercises
  • •The malloc interface itself is poorly designed (no alignment control, no allocation size reporting)
  • •Interest in alternatives that avoid malloc complexity entirely, like stack-only languages