Search code examples
macosperformancemmap

What is the cost of mmaping on Mac OS X?


I have an algorithm where my life would be greatly simplified if I could reserve about 20 blocks of memory addresses of size 4GB. In practice, I never use more than 4GB, but I do not know which block will fill up in advance.

If I mmap 20 blocks of 4GB everything seems to work fine -- until I write to memory the OS does not seem to actually allocate anything.

Is there any reason I should not use mmap to allocate 80GB of memory, and then only using a small amount of it? I assume there is some cost to setting up these buffers. Can I measure it?


Solution

  • The only drawback of mmap-ing 80GB at once is that a page table has to be created for the full 80GB. So if the pages are 4kB, this table could consume a lot of memory (unless huge pages are used).

    For sizes like that it is probably better to use one or more sliding mmap-ed views (i.e. create and remove them when needed).

    On Windows, memory usage for mmap/page tables can be checked with RamMap, not sure about Mac.