记录一下最近在扩展GT时遇到的一些问题
-
通过
residentSize
获取的常驻内存和Xcode上显示的内存值不一致。
原因大致如下:
residentSize
返回的内存由这三部分构成(DirtyMemory + CompressedMemory + CleanMemory)
而
Xcode统计的是MemoryFootPrint
,不包含CleanMemory。
用WWDC-Memory Deep Dive中的一句话总结:
when we talk about the app's footprint, we're really talking about the dirty and compressed segments.Clean memory doesn't really count.
深度揭秘各大 APM 厂商 iOS SDK 背后的核心技术和实现细节
- GT源码中获取residentMemorySize的API已经不被Apple建议使用
源API:
- (NSUInteger)getResidentMemory
{
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
int r = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
if (r == KERN_SUCCESS)
{
return t_info.resident_size;
}
else
{
return -1;
}
}
可以更换为Apple建议的API:
- (NSUInteger)updateMemoryResidentSize {
struct mach_task_basic_info t_info;
mach_msg_type_number_t t_info_count = MACH_TASK_BASIC_INFO_COUNT;
int r = task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
if (r == KERN_SUCCESS) {
return t_info.resident_size;
} else {
return -1;
}
}