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);
The memory mapping process is handled by the virtual memory manager, which is the same subsystem responsible for dealing with the page file. Memory mapped files are loaded into memory one entire page at a time. The page size is selected by the operating system for maximum performance.
What Is Memory-Mapping? Memory-mapping is a mechanism that maps a portion of a file, or an entire file, on disk to a range of addresses within an application's address space. The application can then access files on disk in the same way it accesses dynamic memory.
File mapping is the association of a file's contents with a portion of the virtual address space of a process. The system creates a file mapping object (also known as a section object) to maintain this association. A file view is the portion of virtual address space that a process uses to access the file's contents.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With