26

What are the disadvantages of allocating memory using mmap (with MAP_PRIVATE and MAP_ANONYMOUS) than using malloc? For data in function scope, I would use stack memory anyway and therefore not malloc.

One disadvantage that comes to mind is for dynamic data structures such as trees and linked lists, where you frequently require to allocate and deallocate small chunks of data. Using mmap there would be expensive for two reasons, one for allocating at granularity of 4096 bytes and the other for requiring to make a system call.

But in other scenarios, do you think malloc is better than mmap? Secondly, am I overestimating disadvantage of mmap for dynamic data structures?

One advantage of mmap over malloc I can think of is that memory is immediately returned to the OS, when you do munmap, whereas with malloc/free, I guess memory uptil the data segment break point is never returned, but kept for reusage.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • 11
    malloc will call mmap for you, if necessary... Just stick to malloc. – Macmade Jan 15 '12 at 13:33
  • 5
    Another disadvantage is that if you litter your code with calls to `mmap`, it becomes less portable. – Oliver Charlesworth Jan 15 '12 at 13:35
  • 4
    And why make life more complicated than it needs to be? – Ed Heal Jan 15 '12 at 13:37
  • 1
    Malloc is better for small allocations and for high rate of malloc/free of not-so-huge sizes. This is because malloc can ask a huge pool for small sized requests and do small allocations/deallocations from it, without any mmap/munmap. – osgx Jan 15 '12 at 14:59
  • 1
    The only place I can think this would be really useful is implementing memory pools. With malloc you leave yourself open to fragmentation. With mmap you could return the entire pool to the OS without leaving a tiny string fragment (for example) sitting at the end of the address space. – Zan Lynx Aug 21 '13 at 01:33

3 Answers3

33

Yes, malloc is better than mmap. It's much easier to use, much more fine-grained and much more portable. In the end, it will call mmap anyway.

If you start doing everyday memory management with mmap, you'll want to implement some way of parceling it out in smaller chunks than pages and you will end up reimplementing malloc -- in a suboptimal way, probably.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
21

First off, mmap() is a platform specific construct, so if you plan on writing portable C, it's already out.

Second, malloc() is essentially implemented in terms of mmap(), but it's a sort of intelligent library wrapper around the system call: it will request new memory from the system when needed, but until then it will pick a piece of memory in an area that's already committed to the process.

Therefore, if you want to do ordinary dynamic memory allocation, use malloc(), end of story. Use of mmap() for memory allocation should be reserved for special situations (e.g. if you actually want a whole page for yourself, aligned at the page boundary), and always abstracted into a single piece of library code so that others may easily understand what you're doing.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Even if you do want a whole page for yourself then it's probably better to use `posix_memalign`, it'll be quicker to allocate, quicker to free and will free the kernel from the hassle of tracking another vma. Really the only reason to use `mmap` is if you want to map a file descriptor into memory (the most common use) or are implementing a memory allocator (and even then there are good reasons to prefer `sbrk`). – jleahy May 14 '13 at 20:27
6

One feature that mmap has that malloc doesn't, is mmap allows you to allocate using Huge Pages (flag argument has MAP_HUGETLB set), while malloc doesn't have that option.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
  • 1
    And also it allows you to ask the operating system to allocate all the memory upfront, in order to avoid page faults (used for low-latency programming). – Bogi Apr 22 '21 at 16:36