title: What if I allocate memory using mmap instead of malloc?
body: <p>What are the disadvantages of allocating memory using <code>mmap</code> (with MAP_PRIVATE and MAP_ANONYMOUS) than using <code>malloc</code>? For data in function scope, I would use stack memory anyway and therefore not <code>malloc.</code></p>
  
  <p>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 <code>mmap</code> 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.</p>
  
  <p>But in other scenarios, do you think <code>malloc</code> is better than <code>mmap</code>? Secondly, am I overestimating disadvantage of <code>mmap</code> for dynamic data structures?</p>
  
  <p>One advantage of <code>mmap</code> over <code>malloc</code> I can think of is that memory is immediately returned to the OS, when you do <code>munmap</code>, whereas with <code>malloc/free</code>, I guess memory uptil the data segment break point is never returned, but kept for reusage.</p>