获得当前设备可用的内存 和 获取当前任务所占用的内存 (单位:MB)(转)

时间:2022-03-10 11:16:39

获取当前任务所占的内存:

#include <sys/sysctl.h>
#include <mach/mach.h>
// 任务占用内存
double usedMemory()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(),
TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
if(kernReturn != KERN_SUCCESS) {
return 0;
}
return taskInfo.resident_size / 1024.0 / 1024.0;
#else
return 0;
#endif
}

当前设备可用内存:

#include <sys/sysctl.h>
#include <mach/mach.h> - (double)availableMemory
{
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t kernReturn = host_statistics(mach_host_self(),HOST_VM_INFO,(host_info_t)&vmStats,&infoCount);
if(kernReturn != KERN_SUCCESS)
{
return NSNotFound;
}
return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}

《注》http://blog.csdn.net/zaitianaoxiang/article/details/6859628 (转)