malloc: brk() vs mmap()...

Wilco Dijkstra Wilco.Dijkstra@arm.com
Mon Jun 23 21:01:26 GMT 2025


Hi hpa,

> is there a reason that glibc uses brk() instead of mmap() to allocate 
> memory from the operating system (at least for smallish allocations?)

I guess it's how things started before there was mmap, so it's traditional.
Also mmap was slower last time I benchmarked it (years ago). Modern
kernels may do better since nobody noticed GLIBC was accidentally using
mmap by default recently...

> Using mmap() has the advantages that memory can be returned to the OS 
> other than at the end, the address space becomes more randomized, and an 
> out-of-range pointer reference is at least more likely to be invalid.

I think most importantly mmap allows better use of huge pages. Current support
in GLIBC for this is behind other allocators, so one thing I'd like to do is follow the
OS default setting for huge pages so that you don't need a tunable to enable it
(which presumably nobody ever does...).

Cheers,
Wilco


More information about the Libc-alpha mailing list