Every time your program calls malloc(), it hands a puzzle to a piece of system software that most programmers never think about: the memory allocator. The heap is a finite stretch of bytes. The allocator must hand out chunks of the right size, track which parts are free, and eventually reuse them — all in microseconds, thousands of times per second.
The core tension is simple: fragmentation. After many allocations and frees, the free space can scatter into dozens of small gaps that together would fit a new request but individually can't. An allocator that grabs memory too greedily wastes space; one that splits it too finely turns the heap into Swiss cheese.
Three strategies dominate in practice — the buddy system, the slab allocator, and tcmalloc — and each makes a radically different bet about what programs usually need. Understanding them means understanding a trade-off that shapes every server, game engine, and database running today.
Comments
Loading comments...