當我們想去獲取 iOS 應用的占用內存時,通常我們能找到的方法是這樣的,用 resident_size
:
#import <mach/mach.h> - (int64_t)memoryUsage { int64_t memoryUsageInByte = 0; struct task_basic_info taskBasicInfo; mach_msg_type_number_t size = sizeof(taskBasicInfo); kern_return_t kernelReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) &taskBasicInfo, &size); if(kernelReturn == KERN_SUCCESS) { memoryUsageInByte = (int64_t) taskBasicInfo.resident_size; NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte); } else { NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn)); } return memoryUsageInByte; }
但是測試的時候,我們會發現這個跟我們在 Instruments 里面看到的內存大小不一樣,有時候甚至差別很大。
更加准確的方式應該是用 phys_footprint
:
#import <mach/mach.h> - (int64_t)memoryUsage { int64_t memoryUsageInByte = 0; task_vm_info_data_t vmInfo; mach_msg_type_number_t count = TASK_VM_INFO_COUNT; kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count); if(kernelReturn == KERN_SUCCESS) { memoryUsageInByte = (int64_t) vmInfo.phys_footprint; NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte); } else { NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn)); } return memoryUsageInByte;
}
關於 phys_footprint
的定義可以在 XNU 源碼中,找到 osfmk/kern/task.c
里對於 phys_footprint
的注釋:
/* * phys_footprint * Physical footprint: This is the sum of: * + (internal - alternate_accounting) * + (internal_compressed - alternate_accounting_compressed) * + iokit_mapped * + purgeable_nonvolatile * + purgeable_nonvolatile_compressed * + page_table * * internal * The task's anonymous memory, which on iOS is always resident. * * internal_compressed * Amount of this task's internal memory which is held by the compressor. * Such memory is no longer actually resident for the task [i.e., resident in its pmap], * and could be either decompressed back into memory, or paged out to storage, depending * on our implementation. * * iokit_mapped * IOKit mappings: The total size of all IOKit mappings in this task, regardless of clean/dirty or internal/external state]. * * alternate_accounting * The number of internal dirty pages which are part of IOKit mappings. By definition, these pages * are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid * double counting. */
注釋里提到的公式計算的應該才是應用實際使用的物理內存。