1. 获取IOS APP占用的内存

#import <mach/mach.h>

// ...

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=;
static unsigned greatest = ;
static unsigned last_greatest = ; 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

IOS内存等信息的更多相关文章

  1. iOS 获取APP的CPU、内存等信息

    目标是开发一个SDK,嵌入到APP里面,用来统计当前APP的实时CPU.内存等信息 2015.11.17 http://stackoverflow.com/questions/12889422/ios ...

  2. 【Bugly干货分享】iOS内存管理:从MRC到ARC实践

    Bugly 技术干货系列内容主要涉及移动开发方向,是由Bugly邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处. 对于iOS程序员来说,内存管理是入门的 ...

  3. iOS内存管理 ARC与MRC

    想驾驭一门语言,首先要掌握它的内存管理特性.iOS开发经历了MRC到ARC的过程,下面就记录一下本人对iOS内存管理方面的一些理解. 说到iOS开发,肯定离不开objective-c语言(以下简称OC ...

  4. 【YFMemoryLeakDetector】人人都能理解的 iOS 内存泄露检测工具类

    背景 即使到今天,iOS 应用的内存泄露检测,仍然是一个很重要的主题.我在一年前,项目中随手写过一个简单的工具类,当时的确解决了大问题.视图和控制器相关的内存泄露,几乎都不存在了.后来想着一直就那个工 ...

  5. iOS内存管理策略和实践

    转:http://www.cocoachina.com/applenews/devnews/2013/1126/7418.html 内存管理策略(memory Management Policy) N ...

  6. 精准 iOS 内存泄露检测工具

    MLeaksFinder:精准 iOS 内存泄露检测工具 发表于 2016-02-22   |   zepo   |   23 Comments 背景 平常我们都会用 Instrument 的 Lea ...

  7. 写给Unity开发者的iOS内存调试指南

    0x00 前言 工作的过程中,常常会发现有小伙伴对Unity的Profiler提供的内存数据与某些原生平台Profiler工具,例如iOS系统和Xcode,所提供的内存数据有差异而感到好奇.而且大家对 ...

  8. iOS内存管理

    iOS内存管理的方式是引用计数机制.分为MRC(人式引用计数)和ARC(自动引用计数). 为什么要学习内存管理? 内存管理方式是引用计数机制,通过控制对象的引用计数来实现操作对象的功能.一个对象的生命 ...

  9. iOS内存管理个人总结

    一.变量,本质代表一段可以操作的内存,她使用方式无非就是内存符号化+数据类型 1.保存变量有三个区域: 1>静态存储区 2>stack 3>heap 2.变量又根据声明的位置有两种称 ...

随机推荐

  1. NewRowNeeded和UserAddedRow事件以及RowsAdded的区别使用

    NewRowNeeded事件当 VirtualMode 属性为 true 时,将在用户定位到 DataGridView 底部的新行时发生,适合给新行建立一些默认数据和按规则应该产生的数据,但此时不推荐 ...

  2. yii 验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),

    //验证用户名是否存在                     array("name","unique",'message'=>'用户名已经存在'),

  3. 关于asp.net core部署到iis中出现 HTTP Error 502.5 - Process Failure的问题

    环境是windows Server2008R2 出现这个问题搞了一下午都没解决,最后又加班才算搞定,由于英文不太好,官方的文档看了好几遍,也按照文档做的,但还是出现这个问题,百度google搜了很多解 ...

  4. jquery $(this).attr $(this).val方法使用介绍--useful

    $(this).attr(key); 获取节点属性名的值,相当于getAttribute(key)方法,本文整理了一些相关的示例,感兴趣的朋友可以参考下 $(this).attr(key); 获取节点 ...

  5. shell条件测试

    文件状态测试-b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当p ...

  6. 【译】UI设计基础(UI Design Basics)--自动适配与布局(Adaptivity and Layout)(四)

    2.3  自动适配与布局(Adaptivity and Layout) 2.3.1  开发成自动适配(Build In Adaptivity) 用户通常希望在自己的所有设备,各种场景中使用他们喜欢的a ...

  7. 【Linux】基础配置-修改命令提示符的风格

    1,效果图: [groot]$ 2,设置步骤: 编辑~/.bashrc文件,在最后增加设置行: #显示当面目录的最后一层目录#PS1='\[\e[32m\][\u@\h \W]$\[\e[m\]'#只 ...

  8. Delphi重载,覆盖,多态

    一.override 重载 type TFigure = class procedure Draw; virtual;//(我的理解是)父类中可以使用父类的,子类中使用子类的.与“四”是有区别的. e ...

  9. 用c写99乘法表

    int main(int argc,char **argv){ int a; for(a=1;a<=9;a++){ int b; for(b=1;b<=a;b++){ printf(&qu ...

  10. JFS 文件系统概述及布局分析

    JFS 文件系统概述及布局分析 日志文件系统如何缩短系统重启时间 如果发生系统崩溃,JFS 提供了快速文件系统重启.通过使用数据库日志技术,JFS 能在几秒或几分钟之内把文件系统恢复到一致状态,而非日 ...