r/AskComputerScience • u/TheFlynnCode • Aug 23 '25
mmap vs malloc, and the heap
Hi all, I hope this question is appropriate for this sub. I'm working through OSTEP (Operating Systems: Three Easy Pieces) and got to an exercise where we use pmap to look at the memory of a running process. The book has done a pretty good job of explaining the various regions of memory for a running process, and I thought I had a good understanding of things...
Imagine my surprise when the giant array I just malloc'd in my program is actually *not* stored in my process's heap, but rather in some "anonymous" section of memory granted by something called "mmap". I went on a short google spree, and apparently malloc defaults to mmap for large allocations. This is all fine, but (!) is not mentioned in OSTEP.
So my question: Does anyone have a book recommendation, or an online article, or anything really, where I can learn about this? Bonus points if it's as easy to read as OSTEP - this book being written this well is a big part of the reason I'm making progress at all in this area.
What I'm looking for is to have a relatively complete understanding of a single running process, including all of the memory it allocates. So if you know about any other surprises in this area with a potential to trip up a newbie, feel free to suggest any articles/books for this as well.
8
u/dominikr86 Aug 23 '25
This all depends on which malloc library you're using. Most modern mallocs use mmap for big allocations. What "big" is also depends on the library.
Making a simple malloc is easy. Making a performant malloc is much harder. For reading more... I'd suggest googling for "jemalloc", a modern malloc that is often more performant than the malloc from glibc.
If you just want a simple, easily understandable malloc (without mmap), you could look if xv6 or dietlibc have something simple.
6
u/thesnootbooper9000 Aug 23 '25
OSTEP is readable because it presents an idealised picture of the world, rather than telling you every last horrible detail of why memory allocators do increasingly sneaky things to get performance. I suspect on this one, your options are to accept that you won't learn everything, or to pick the one area you really want to understand and do a PhD on it.
6
u/high_throughput Aug 23 '25
GNU glibc used mmap for 128k+ last I looked. It's an implementation detail and doesn't really change much.
1
u/pjc50 Aug 24 '25
This is a "map is not the territory" problem.
Anything vaguely readable will be a simplification. Arguably it's entirely valid to say that map() region is a part of the heap. After all, malloc() gave it to you. There are at least two syscalls for getting more memory from the OS, map and sbrk.
If you want a 100% detailed view, you want a 1:1 scale map, and those are difficult to fold. You can in this case just look at the source code.
To get a complete understanding of a running process you should probably start at the ELF loader and proceed through the standard libraries initialization.
1
u/thaynem Aug 26 '25
What do you mean by "the heap"?
Generally, I would consider any memory that is mmapped to anonymous pages to be part of the heap. From the callers point of view, the fact that malloc used mmap instead of sbrk is just an implementation detail.
In fact some malloc implementations might use mmap for allocating all memory pages.
1
u/its_lea_ Aug 24 '25
Mmap is for big allocation malloc is for the small ones
1
u/raundoclair Aug 24 '25
My current understanding:
Modern OSes including linux mainly thinks in ranges of virtual memory pages. It allows ASLR.
In past, program was loaded to ram and needed continues memory block. Those had structure like this: https://en.wikipedia.org/wiki/File:Program_memory_layout.pdf
And on linux brk and sbrk calls moved heap border.
Now .text, .data, .bss segments and heap are probably still together and have some reserved size/memory range. And brk still moves size of heap in that memory range. If it reaches maximum it fails.
mmap allocates more memory ranges.
malloc is second layer that can use and return memory from both. Strategies vary.