Get the memory status of your iOS device

Source: Internet
Author: User

Reprinted from: http://mobile.51cto.com/iphone-285371.htm

Mobile devices such as the iphone have limited memory and cannot use swap zones, to reduce operational inefficiencies or crash applications in order not to run out of memory, and sometimes need to get current memory conditions to determine the caching strategy adopted.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

Because of the limited memory of mobile devices such as the iphone and the inability to use swap areas, to avoid running out of memory or to crash the application, it is sometimes necessary to obtain the current memory state to determine the caching strategy adopted.

However, the iOS SDK documentation does not mention the underlying API, so I searched and found the Host_statistics () function.

Although the parameters are many, but basically are fixed values, I do not explain, directly on the code:

  1. #include <mach/mach.h>
  2. BOOL memoryinfo (vm_statistics_data_t *vmstats) {
  3. mach_msg_type_number_t infocount = host_vm_info_count;
  4. kern_return_t Kernreturn = host_statistics (mach_host_self (), Host_vm_info, (host_info_t) VmStats, & Infocount);
  5. return Kernreturn = = kern_success;
  6. }
  7. void Logmemoryinfo () {
  8. vm_statistics_data_t Vmstats;
  9. if (Memoryinfo (&vmstats)) {
  10. 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 ",
  11. Vmstats.free_count * Vm_page_size,
  12. Vmstats.active_count * Vm_page_size,
  13. Vmstats.inactive_count * Vm_page_size,
  14. Vmstats.wire_count * Vm_page_size,
  15. Vmstats.zero_fill_count * Vm_page_size,
  16. Vmstats.reactivations * Vm_page_size,
  17. Vmstats.pageins * Vm_page_size,
  18. Vmstats.pageouts * Vm_page_size,
  19. Vmstats.faults,
  20. Vmstats.cow_faults,
  21. Vmstats.lookups,
  22. Vmstats.hits
  23. );
  24. }
  25. }

Call Memoryinfo () to get the memory information, its type is vm_statistics_data_t. This struct has many fields and shows how to get them in Logmemoryinfo (). Note that most of these fields are the number of pages, multiplied by vm_page_size to get the number of bytes.

By the way: Free is idle memory, active is used, but can be paged (in iOS, only the static on the disk can be paged, such as the memory map of the file, and the dynamic allocation of memory is not paged); inactive is inactive, in fact, when memory is low, Your app can preempt this part of the memory, so it can also be thought of as free memory; a wire is already in use and cannot be paged.

In the end, you'll find that even if you add all of this to a much smaller amount of memory than the device, the rest of the memory will have to be taken up as a mystery. But on the simulator, these 4 add up basically is the physical memory of the Mac, the difference is less than 2MB.

And the total physical memory can be obtained with nsrealmemoryavailable (), this function does not need to provide parameters, the document is also documented, I do not write the demo code.

Get the memory status of your iOS device (go)

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.