http://blog.csdn.net/xyt243135803/article/details/40995759
In the article "GPS offset correction in China (for Google Maps)", a 78M large data file was read, beginning with the NSData Datawithcontentsoffile: method. Many people say that if used directly, it will run out of iOS memory. In fact, this can be improved. NSData also has an API:
+ (id)dataWithContentsOfFile:(NSString *)path options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr;
Where nsdatareadingoptions can attach a parameter nsdatareadingmappedifsafe parameter. With this parameter, iOS does not take the entire file to the full memory, but instead maps the file to the process's address space, which does not occupy the actual memory. This will solve the problem of full memory.
Actual test results
Memory consumption without parameters:
Memory consumption after using the Nsdatareadingmappedifsafe parameter:
Thus, for large volumes of files, using memory-mapped mode reads will reduce memory consumption. What is a file memory map?
File memory mapping refers to the mapping of the contents of a file into the memory virtual address space of the process, which does not actually allocate physical memory for the contents of the file. This is actually the equivalent of pointing the memory address value to the file's disk address. If you read and write the memory, you are actually reading and writing the contents of the file on disk.
iOS maps large files to memory (reads large files)