6

I have an application that can potentially have hundreds of memory mapped, i.e., mmap(), files opened at any point in time.

I'm looking for some help understanding what, if any, the practical limit is on the number of opened memory mapped files is.

I create these mmap files like:

void* map = mmap(0, *capacity, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
khr055
  • 28,690
  • 16
  • 36
  • 48
Tim Reddy
  • 4,340
  • 1
  • 40
  • 77
  • Have you tried just creating them until the app crashes? The error message you get will tell you which limit you hit first. – Douglas Feb 08 '12 at 00:47

1 Answers1

6

iOS kernel allocates around 700mb of virtual memory per process. So that will be your limit.

The limit you have on RAM will differ as the kernel pages data into RAM from virtual memory as you touch on the mapped data. When the RAM itself fills up, around 40mb on the iphone 4, depending on how much RAM is wired by other applications, and you request more mapped data, the kernel will need to page data out of RAM and replace it with the requested data by paging it into RAM.

Another thing to remember is that if you use PROT_READ | PROT_WRITE then you are allowing data to be written to the mapped file. This will then impact the 700mb of allocated space if you decide to write data to the mapped file.

So the limit is 700mb for virtual memory, whether you map one file of 500mb and then write another 200mb of data to it, or if you have e.g. 10 X 70mb mapped files that you just read.

One last thing is that you can release the opened file that was the source of the mapped data as soon as you have received a successfully mapped file using mmap().

Hope this helps.

Additional info:

  • Regarding the iphone's 700mb virtual memory and around 40mb RAM, this comes from doing profiling using instruments.

  • Regarding the actual workings of a systems memory management. Read up on virtual memory Regarding how this works on iOS. Read the apple docs on virtual memory which focuses on OS X, but mentions differences on iOS

some_id
  • 29,466
  • 62
  • 182
  • 304
  • Thanks for the answer! Is this information defined somewhere on Apple's site so I can read about it in greater detail? – Tim Reddy Apr 04 '12 at 17:06
  • @TReddy - I updated my answer. If you are interested in how the kernel actually handles memory on a lower level, add a comment and I can add further sources. :D – some_id Apr 07 '12 at 19:00