iOS maps large files to memory (reads large files)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.