博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS内存等信息
阅读量:5317 次
发布时间:2019-06-14

本文共 4664 字,大约阅读时间需要 15 分钟。

1. 获取IOS APP占用的内存

#import 
// ...void report_memory(void) { struct task_basic_info info; mach_msg_type_number_t size = sizeof(info); kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size); if( kerr == KERN_SUCCESS ) { NSLog(@"Memory in use (in bytes): %u", info.resident_size); } else { NSLog(@"Error with task_info(): %s", mach_error_string(kerr)); }}
void report_memory(void) {    static unsigned last_resident_size=0;    static unsigned greatest = 0;    static unsigned last_greatest = 0;    struct task_basic_info info;    mach_msg_type_number_t size = sizeof(info);    kern_return_t kerr = task_info(mach_task_self(),                               TASK_BASIC_INFO,                               (task_info_t)&info,                               &size);    if( kerr == KERN_SUCCESS ) {        int diff = (int)info.resident_size - (int)last_resident_size;        unsigned latest = info.resident_size;        if( latest > greatest   )   greatest = latest;  // track greatest mem usage        int greatest_diff = greatest - last_greatest;        int latest_greatest_diff = latest - greatest;        NSLog(@"Mem: %10u (%10d) : %10d :   greatest: %10u (%d)", info.resident_size, diff,          latest_greatest_diff,          greatest, greatest_diff  );    } else {        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));    }    last_resident_size = info.resident_size;    last_greatest = greatest;}
void report_memory(void){    struct mach_task_basic_info info;    mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;    kern_return_t kerr = task_info(mach_task_self(),                                   MACH_TASK_BASIC_INFO,                                   (task_info_t)&info,                                   &size);    if( kerr == KERN_SUCCESS ) {        NSLog(@"Memory in use (in bytes): %u", info.resident_size);    } else {        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));    }}

 

//test 1    struct mach_task_basic_info info;    mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;    kern_return_t kerr = task_info(mach_task_self(),                                   MACH_TASK_BASIC_INFO,                                   (task_info_t)&info,                                   &size);    if( kerr == KERN_SUCCESS ) {        CCLOG("Memory in use (in bytes): %llu", info.resident_size);    } else {        CCLOG("Error with task_info()");    }        //test 2    mach_port_t            host_port = mach_host_self();    mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);    vm_size_t              pagesize;    vm_statistics_data_t   vm_stat;    host_page_size(host_port, &pagesize);//    host_statistics(host_t host_priv, host_flavor_t flavor, host_info_t host_info_out, <#mach_msg_type_number_t *host_info_outCnt#>)//    host_statistics64(host_t host_priv, host_flavor_t flavor, host_info64_t host_info64_out, mach_msg_type_number_t *host_info64_outCnt)    int r = host_statistics(host_port, HOST_VM_INFO, (host_info64_t)&vm_stat, &host_size);    if (r != KERN_SUCCESS)        CCLOG("Failed to fetch vm statistics");    float active = (float)(vm_stat.active_count * pagesize) / 1048576.0f;    float inactive = (float)(vm_stat.inactive_count * pagesize)/1048576.0f;    float wire = (float)(vm_stat.wire_count * pagesize)/1048576.0f;        unsigned long int  mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;    unsigned long int  mem_free = vm_stat.free_count * pagesize;    unsigned long int  mem_total = mem_used + mem_free;    CCLOG("page size: %u", pagesize);    CCLOG("memory active: %f, memory inactive: %f, memory wire: %f", active, inactive, wire);    CCLOG("memory used: %lu, memory free: %lu, memory total: %lu", mem_used, mem_free, mem_total);

 Resident memory is a measurement of the memory that has been allocated to your application and has not yet been reclaimed by the system, but some/most of the resident memory could be reclaimed by the system.

http://stackoverflow.com/questions/18642421/understanding-task-basic-info-task-resident-size

http://stackoverflow.com/questions/787160/programmatically-retrieve-memory-usage-on-iphone

C/C++ tip: How to get the process resident set size (physical memory use)

http://nadeausoftware.com/articles/2012/07/c_c_tip_how_get_process_resident_set_size_physical_memory_use#taskinfoforcurrentresidentsetsize

 

这篇文章救了我一天:

http://gamesfromwithin.com/whered-that-memory-go

转载于:https://www.cnblogs.com/wiessharling/p/4663862.html

你可能感兴趣的文章
JS写一个简单日历
查看>>
Python 发 邮件
查看>>
mysql忘记密码的解决办法
查看>>
全面分析Java的垃圾回收机制2
查看>>
[Code Festival 2017 qual A] C: Palindromic Matrix
查看>>
修改博客园css样式
查看>>
Python3 高阶函数
查看>>
初始面向对象
查看>>
leetcode Letter Combinations of a Phone Number
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
windows基本命令
查看>>
VMware中CentOS设置静态IP
查看>>
[poj1006]Biorhythms
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
Hover功能
查看>>
js千分位处理
查看>>
Mac---------三指拖移
查看>>