hn.today

Doug Lea's Malloc

gee.cs.oswego.edu4 points1 comments
Screenshot of Doug Lea's Malloc

Doug Lea describes the design and evolution of dlmalloc, a public-domain implementation of C’s malloc, free, and realloc, first written to address poor speed and memory use in the systems he used. Its aim is to work as a general-purpose allocator, avoiding the need for programmers to build specialized allocators for common workloads. The design balances compatibility and portability with low memory waste, fast operations, tunability, locality, error detection, and reliable performance across varied applications. Lea emphasizes that minimizing fragmentation is central: an allocator that is fast but never reuses freed memory can exhaust available space, while measures that improve safety or flexibility may cost time or space. The design therefore evolves in response to performance studies, changing workloads and hardware, and user contributions.

The core strategy combines boundary tags with size-based bins. Metadata around memory chunks lets the allocator merge neighboring freed chunks and traverse memory in either direction; newer versions omit some metadata on allocated chunks to reduce overhead. Available chunks are grouped into 128 bins, roughly logarithmic by size, with small bins holding exact sizes in 8-byte increments. The allocator searches in smallest-first, best-fit order, and later versions sort chunks within bins to improve fit quality. This coalescing, best-fit approach aims to limit fragmentation while keeping searches efficient. Its bookkeeping imposes minimum chunk sizes of 16 bytes on 32-bit systems and 24 bytes on 64-bit systems, potentially wasteful for tiny objects. The text describes an earlier version and warns that it does not reflect the current implementation.

Read on gee.cs.oswego.edu1 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.