您的位置:首页 > 其它

获取 iphone 手机的总内存和可用的内存大小

2011-12-06 15:14 363 查看
typedef struct UIDeviceMemoryInfo {
float totalMemory;
float freeMemory;
} UIDeviceMemoryInfo;


- (UIDeviceMemoryInfo)memoryInfo {
size_t length;
int mib[6];
int result;

int pagesize;
mib[0] = CTL_HW;
mib[1] = HW_PAGESIZE;
length = sizeof(pagesize);
if (sysctl(mib, 2, &pagesize, &length, NULL, 0) < 0)
{
perror("getting page size");
}

mach_msg_type_number_t count = HOST_VM_INFO_COUNT;

vm_statistics_data_t vmstat;
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmstat, &count) != KERN_SUCCESS)
{
}

double total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
//	double wired = vmstat.wire_count / total;
//	double active = vmstat.active_count / total;
//	double inactive = vmstat.inactive_count / total;
//	double free = vmstat.free_count / total;

mib[0] = CTL_HW;
mib[1] = HW_PHYSMEM;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting physical memory");
}
mib[0] = CTL_HW;
mib[1] = HW_USERMEM;
length = sizeof(result);
if (sysctl(mib, 2, &result, &length, NULL, 0) < 0)
{
perror("getting user memory");
}

UIDeviceMemoryInfo info;
info.freeMemory = vmstat.free_count * pagesize;
info.totalMemory = total * pagesize;
return info;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息