Quick Answer: What does malloc do? It hands out a block from a heap that glibc already owns, so most calls never reach the kernel. The allocator only asks for more memory by moving the program break with brk or sbrk, a syscall that costs roughly 600 ns. Each block keeps a size header, which lets free return it to a free list without being told its size.

Most of us call malloc thousands of times without wondering what happens underneath. You ask for 16 bytes, get a pointer back, and call free later. The question is simple: what does malloc do between your call and the kernel? A walkthrough by Ben Mandrew answers it with strace output and benchmarks, and the numbers explain why the design exists. On his machine, an empty syscall took 575 ns, while malloc(16) took 26.9 ns. If every allocation went to the kernel, small-object code would spend most of its time crossing the user-kernel boundary.

What malloc Does on the First Call

Run a loop that calls malloc(16) a thousand times under strace, and you'll see something surprising. Only two brk calls appear. The first asks where the heap currently ends. The second grows it by 132 KiB. The other 999 calls take memory from that block without touching the kernel at all.

Here's what happens on that first call:

  1. glibc calls brk(NULL) to learn the current program break, the address where the heap ends.
  2. It calls brk again with a larger address, extending the heap by a chunk of its own choosing.
  3. It carves a 16-byte piece from that region and returns a pointer.

You may also see sbrk in discussions. brk sets the break to an absolute address. sbrk is a libc wrapper that takes a delta and moves the break by that amount. Both are heap-extension primitives, and glibc uses them for its main arena. Try it yourself: strace -e trace=brk,mmap ./your-program shows exactly how often your process asks the kernel for memory. For most small-object workloads, the answer is "rarely."

Your Process Has More Regions Than You Think

A running process has an address space split into regions: text for code, data for initialised globals, bss for zeroed globals, the heap that grows upward from after bss, the stack, and shared libraries such as libc. Printing one address from each shows the layout. In the source run, the heap sat at roughly 0x59c0903182b0, while printf in libc sat near 0x711dcf7c15b0, about 23.4 TiB higher.

That gap is the counter-intuitive part. The distance is about 1,530 times the 15.6 GiB of RAM in that machine, yet it costs almost nothing. The kernel only needs to track mapped ranges. Unmapped addresses consume no physical memory. The gap is where a process can grow without colliding with anything.

Virtual address space isn't the same as physical memory, though. A process can map terabytes it never uses, so you should never judge memory use by looking at the address map alone.

Why a Bump Allocator Is a Trap

Once you know brk and sbrk exist, the obvious first allocator is a bump allocator. Each call moves the break forward by the requested size, and free does nothing. It's about ten lines of C, and it's wrong in two ways.

The first problem is cost. A bump allocator calling sbrk on every request pays for a syscall each time. In the same benchmark, sbrk(16) cost 610.1 ns per call, while malloc(16) cost 26.9 ns. That's a 22.7x difference, and the syscall cost dominates it. The fix isn't a faster syscall. It's to ask the kernel for large blocks and cut them up yourself.

The second problem is reuse. A bump allocator can't reuse freed memory. In a test run, a, b, and c were allocated at offsets 0, 16, and 32. After freeing b, the next allocation d still landed at offset 144, after c, leaving the 16 bytes behind b wasted. Real programs with churning allocation patterns will exhaust the heap long before they run out of live data.

Here's where the real allocator starts: it batches calls to the kernel and keeps track of what you've returned.

Headers Are How free Knows the Size

Look at the signature of free: it takes only a pointer. So where does the size come from? glibc stores it in a header located just before the pointer it returns. The header is 8 bytes on x86-64, and the chunk, meaning the header plus the usable block, is always a multiple of 16 bytes. The 16-byte alignment matches max_align_t on x86-64, so the returned memory suits any C type.

Chunk sizes are always multiples of 16, which leaves the low bits free. glibc uses three of them as flags: A marks a chunk from a secondary arena, M marks a chunk with its own mmap, and P means the previous chunk is in use. Masking them off gives the true size.

The source measured how requests map to chunks:

Request (bytes)Chunk (bytes)Overhead (bytes)
13231
24328
254823
100010088

The 32-byte minimum exists because a freed chunk needs room for its free-list pointer. The rounding is why malloc(25) costs you 23 bytes of overhead, not 8. Most guides skip this part, and it matters if you're allocating millions of tiny objects.

Here's the failure mode that practitioners hit: a buffer overflow that writes past the end of a block can overwrite the next chunk's header. Then free or the next malloc reads a corrupt size, and the crash shows up far from the bug. Tools such as AddressSanitizer catch this at the moment of the bad write.

Free Lists, Splitting, and Coalescing

When you free a chunk, glibc doesn't return it to the kernel. It places the chunk on a free list so the next request can reuse it. A freed chunk stores its free-list pointer in the space where your data used to be, so the list costs no extra memory.

Allocation searches the list for a chunk big enough. If the chunk is much larger than the request, it's split, and the remainder goes back on the list. Freeing does the reverse. If a freed chunk sits right next to a free neighbour, the two merge into one. This merging is called coalescing, and it's what stops the heap from fragmenting into unusable slivers.

Here's the trace from the source. Allocate a, b, and c, then free a and b. They merge into one 352-byte chunk. Allocating d(50) splits that chunk, leaving 272 bytes free. Freeing c and d merges everything back into a single 64 KiB chunk.

The simple first-fit list is a trade-off. Scanning a long list on every allocation is slow, which is why production allocators go further. glibc has per-thread caches (tcache, added in glibc 2.26), plus fast and small bins for common sizes. The list you've just seen is the fallback path, not the hot path.

Free Doesn't Mean Free RAM

Here's the misconception that trips up almost everyone: free returns memory to the OS. In practice, it doesn't. Whether a freed chunk still costs RAM depends on demand paging. The kernel only puts physical memory behind a virtual page on its first write, and once written, the page stays resident.

In the source's test, moving the break up by 1 GiB gave a virtual size of about 1 GiB with a resident set size of only 1308 KiB. Writing one byte to every 4 KiB page caused 65,536 page faults per 256 MiB, and RSS climbed in step with each write. After free, those pages remain resident. The allocator keeps them for reuse. Your process's RSS will not drop just because you called free.

So when does memory go back? glibc trims the heap when the free chunk at the top reaches a threshold. The source's toy allocator uses 128 KiB and keeps 64 KiB as a buffer so that a few requests don't force the break back up. glibc has the same idea, configurable with mallopt(M_TRIM_THRESHOLD, ...). Large requests take a different path entirely. glibc serves them with mmap once they cross the M_MMAP_THRESHOLD, which defaults to 128 KiB and adjusts dynamically. Those blocks are unmapped immediately on free.

The trade-off is real. Trimming too eagerly means repeated brk churn for workloads that oscillate in size. Trimming too lazily keeps RSS high. One long-lived allocation sitting at the top of the heap pins everything below it, which is the classic fragmentation problem.

Frequently Asked Questions

what does malloc do when you call free?

free doesn't return memory to the kernel. It marks the chunk as available by putting it on a free list, merging it with free neighbours where possible. Physical RAM stays in use until the allocator trims the top of the heap or the block was served by mmap, which is unmapped immediately.

what is the program break in malloc?

The program break is the address where the process heap currently ends. malloc moves it upward with brk or sbrk when it needs more memory from the kernel. Memory between the start of the heap and the break is mapped and usable, and everything above it is unmapped until the next extension.

how to check how much memory malloc is actually using?

Read VmRSS in /proc/self/status for physical memory, and VmSize for mapped address space. For allocator-level detail on glibc 2.33 and later, call mallinfo2(), which reports arena, mmap, and free-chunk totals. Use strace to see how often brk or mmap is actually called.

why doesn't free reduce my process's memory usage?

Freed chunks stay in the process's heap for reuse, and their pages stay resident once written. The kernel only reclaims them if the allocator trims the top of the heap, which requires the free space to sit at the end. A single live allocation near the top of the heap can keep everything below it resident.

Most people stop here. Don't. The next time you see a memory number that won't drop, the explanation is almost always the allocator doing its job. Run strace -e trace=brk,mmap on one of your programs this week and count the calls. Then pass this to someone chasing a process whose memory never seems to fall, because what does malloc do with freed memory is the first thing to check.