The general consensus it that malloc(3) (and calloc(3) and realloc[array](3)) obtains its memory from the heap segment. This is indeed the case, but digging a bit deeper reveals that it's not always the case. The modern glibc malloc(3) engine uses some subtle strategies to make the most optimal use of available memory regions and the process VAS—which, especially on today's 32-bit systems, is fast becoming a rather scarce resource.
So, how does it work? The library uses a predefined MMAP_THRESHOLD variable – its value is 128 KB by default – to determine from where memory gets allocated. Let's imagine we are allocating n bytes of memory with malloc(n):
-
If n < MMAP_THRESHOLD, use the heap segment to allocate the ...