What Is malloc Doing, Anyway?

November 9, 2026 · 4 min read

malloc has a very nice and simple interface: you ask for a certain amount of memory, and you are given a void pointer to your block of contiguous bytes. Once you are done with it, you call free with the same pointer and the memory disappears into the ether. But what is actually happening under the hood? [1]

Ben M. Andrew has published a detailed walkthrough of what malloc does in glibc, the GNU C library used on most Linux systems. The post uses strace, memory layout diagrams, and benchmarking to explain the mechanics of memory allocation from userspace down to the kernel [1].

The First malloc Call

When you call malloc(16) a thousand times in a loop, strace reveals that the whole loop makes only two brk syscalls. The first asks where the program break is (the address where the process's heap ends), and the second grows the heap by 132 KiB. Both are done in the first malloc call. The other 999 calls just take from already allocated memory [1].

This is the key insight: syscalls are expensive, measured at 575 nanoseconds for an empty one. glibc avoids making a syscall for every malloc by requesting a large chunk from the kernel upfront and then carving out smaller pieces from that chunk in userspace. The bookkeeping happens in free lists, linked lists of previously allocated and freed blocks [1].

The Address Space

Andrew demonstrates the layout of a process's address space with a small program that prints one address from each region: text (executable code), data (initialised globals), bss (uninitialised globals), heap (malloc), program break, libc, and stack. The addresses rise from the binary's code up to its stack, with some regions separated by enormous gaps [1].

For example, in one run the program break and printf in libc were 23.4 TiB apart in the virtual address space. This gap is not backed by physical memory. It exists because the kernel maps memory lazily, only allocating physical pages when they are actually written to [1].

brk vs mmap

For small allocations, glibc uses brk to grow the heap. For larger allocations, typically above 128 KiB, glibc switches to mmap, which allocates memory at an arbitrary location in the address space rather than extending the heap. This threshold is tunable via the M_MMAP_THRESHOLD parameter [1].

The distinction matters for performance. brk allocations are served from a contiguous heap region and can reuse freed blocks adjacent to the break. mmap allocations are independent mappings that must be unmapped individually when freed, which can lead to fragmentation in the virtual address space over time [1].

Why This Matters

Understanding malloc is not just academic. Memory allocation patterns have real performance consequences: fragmentation can waste memory, syscall overhead can dominate latency-sensitive workloads, and the choice between brk and mmap affects cache locality. Developers who understand the allocator can write code that works with it rather than against it [1].

Andrew's post is a clear, evidence-based introduction that starts from the syscall interface and builds up to the free-list bookkeeping that makes modern malloc implementations efficient. For anyone who has ever wondered what happens between calling malloc and getting a pointer back, it is worth reading [1].

Sources:

[1] Ben M. Andrew: What is malloc doing, anyway?