Obtains the memory status of iOS devices.

Source: Internet
Author: User

Because the memory of mobile devices such as the iPhone is limited and the swap zone cannot be used, in order not to cause insufficient memory and cause lower running efficiency or application crash, sometimes you need to obtain the current memory status, to determine the cache policy.

However, this underlying API is not mentioned in the iOS SDK documentation, so I searched for it and found the host_statistics () function.

Although there are many parameters, they are basically fixed values, so I will not explain them. I will directly go to the Code:

 
 
  1. #include <mach/mach.h> 
  2.  
  3. BOOL memoryInfo(vm_statistics_data_t *vmStats) { 
  4.     mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT; 
  5.     kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)vmStats, &infoCount); 
  6.      
  7.     return kernReturn == KERN_SUCCESS; 
  8.  
  9. void logMemoryInfo() { 
  10.     vm_statistics_data_t vmStats; 
  11.      
  12.     if (memoryInfo(&vmStats)) { 
  13.         NSLog(@"free: %u\nactive: %u\ninactive: %u\nwire: %u\nzero fill: %u\nreactivations: %u\npageins: %u\npageouts: %u\nfaults: %u\ncow_faults: %u\nlookups: %u\nhits: %u", 
  14.             vmStats.free_count * vm_page_size, 
  15.             vmStats.active_count * vm_page_size, 
  16.             vmStats.inactive_count * vm_page_size, 
  17.             vmStats.wire_count * vm_page_size, 
  18.             vmStats.zero_fill_count * vm_page_size, 
  19.             vmStats.reactivations * vm_page_size, 
  20.             vmStats.pageins * vm_page_size, 
  21.             vmStats.pageouts * vm_page_size, 
  22.             vmStats.faults, 
  23.             vmStats.cow_faults, 
  24.             vmStats.lookups, 
  25.             vmStats.hits 
  26.         ); 
  27.     } 

Call memoryInfo () to obtain the memory information. Its type is vm_statistics_data_t. This struct has many fields and shows how to obtain them in logMemoryInfo. Note that most of these fields are the number of pages. You must multiply vm_page_size to get the number of bytes.

By the way, let's briefly introduce: free memory is idle; active memory is used, but can be paged (in iOS, only static storage on the disk can be paged, for example, the File Memory ing, And the dynamically allocated memory cannot be paged); inactive is inactive. In fact, when the memory is insufficient, your application can seize this part of memory, therefore, it can also be seen as idle memory; wire is used and cannot be paged.

In the end, you will find that even if you add up all the memory, it is much less than the device memory, so the rest will be treated as the occupied mysterious memory. However, in the simulator, the four are basically the physical memory of the Mac, with a difference of less than 2 MB.

The total physical memory can be obtained using NSRealMemoryAvailable (). This function does not need to provide parameters and is recorded in the document. I will not write the Demo code.

Related Article

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.