Sunday, 29 November 2026

Trusted News Discovery โ€“ Powered by AI and Verified by Humans

Doug Lea's malloc, the memory allocator he began in 1987, still shapes how Linux programs get memory

Doug Lea began writing dlmalloc in 1987, and the public-domain allocator became the root of the malloc in the GNU C library on Linux.

Produced with AI assistance and reviewed by the Badiyaa Editorial Desk. How we work.

Grok Fact CheckCheck it yourself in one tapVerify with Grok ↗Ask @grok on X ↗
A desktop computer memory module with a heat spreader lies on a plain surface.
Image: Victorrocha, CC BY 3.0 via Wikimedia Commons

Doug Lea never gave his memory allocator a name. He started writing it in 1987, put the code in the public domain, and everyone simply started calling it Doug Lea's Malloc, or dlmalloc. Nearly four decades later, its design still sits underneath a huge share of the software running on Linux.

In his own write-up of the allocator, "A Memory Allocator," Lea explains why he bothered. His C++ programs leaned heavily on dynamic memory, and on the SunOS and BSD systems he used at the time they ran slower and ate more memory than he expected. He first wrote special allocators for individual classes, then decided that was a losing strategy and built one general allocator good enough that programmers would rarely need their own.

What malloc actually does

Every time a C program asks for a block of memory of a size it only learns at run time, it calls malloc; when it is done, it calls free. The allocator's job is to hand out those blocks fast and to reuse freed space without leaving the heap riddled with unusable gaps, a problem called fragmentation.

Lea's answer rests on two ideas that, by his account, never changed from the earliest versions:

  • Boundary tags. Each chunk of memory carries its size at both ends, so two neighboring free chunks can be merged into one bigger chunk, and the allocator can walk the heap in either direction.
  • Bins. Free chunks are sorted into 128 size classes. Below 512 bytes, every bin holds exactly one size, spaced 8 bytes apart. A request is filled from the smallest chunk that fits, a best-fit strategy that wastes the least memory on real workloads.

The cost of that bookkeeping is a minimum chunk size: 16 bytes on 32-bit systems and 24 bytes on 64-bit ones. Lea is candid that this hurts programs that allocate millions of tiny linked-list nodes, which can pay 100% overhead.

The wilderness and the 1MB rule

Some of the vocabulary is wonderfully odd. The free space at the very top of the heap, the only chunk that can grow by asking the operating system for more, is called the "wilderness," a name Lea credits to Kiem-Phong Vo. dlmalloc treats it as bigger than every other chunk, so it is used only when nothing else fits. Very large requests skip the heap entirely: in the version his article describes, anything over a default threshold of 1MB is mapped straight from the operating system with mmap.

Lea also confesses to one compatibility quirk. Some early Unix allocators let programs resize memory they had already freed, and he kept that behavior until 1993. When he dropped it, he writes, "no one at all complained."

From one programmer's code to Linux

The stakes are bigger than they look. Lea passes on a point from allocator researcher Paul Wilson: across the world, poor allocation schemes may cost "billions of dollars in memory chips."

According to Wikipedia's guide to C dynamic memory allocation, the GNU C library's allocator descends from Wolfram Gloger's ptmalloc, a fork of dlmalloc with threading improvements. The same article lists dlmalloc's latest release as version 2.8.6 from August 2012 and notes it was relicensed from CC0 to MIT-0 in 2023.

The Badiyaa takeaway: if a program on your Linux machine calls malloc, it is very likely running descendants of ideas one programmer sketched in 1987. For more on how C keeps evolving, read about Type-C, which adds generics and pattern matching to C, and why memory bugs in Ruby's regex engine still matter.

Sources

Image: Victorrocha, CC BY 3.0 via Wikimedia Commons

This article was produced with AI assistance and reviewed by our Editorial Desk. How we work.

Grok Fact CheckCheck it yourself in one tapVerify with Grok ↗Ask @grok on X ↗