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

最新文章

  1. Displaying Data in a Chart with ASP.NET Web Pages (Razor)
  2. savedInstanceState的作用
  3. TCP、UDP、HTTP、SOCKET之间的区别
  4. OC点语法和变量作用域
  5. aspose输出表格
  6. 1439. Battle with You-Know-Who(splay树)
  7. Html5 Canvas Hit Testing
  8. 增加配置Apache2 管理 SVN 访问权限
  9. Matlab与.NET混合编程解决人脸识别问题
  10. JSP自定义标签必知必会
  11. Azure DevOps Server/Team Foundation Server
  12. 《梦断代码》Scott Rosenberg著(二)
  13. PAT Basic 1006
  14. SE Springer小组之《Spring音乐播放器》需求分析说明书一
  15. 关于vs2013进行单元测试
  16. 高通QMI协议
  17. JS对象中的原型
  18. 7.25 8figting!
  19. Exception in thread &quot;main&quot; java.nio.channels.NotYetConnectedException
  20. javascript的时间描述图怎么写

热门文章

  1. .NET Framework(一)
  2. workerman需要的php模块posix、pcntl、sysvshm、sysvmsg缺少,怎么办
  3. Redux1
  4. Windows环境下安装PHPUnit
  5. C# 日期格式精确到毫秒 【转】
  6. PHP中的MVC
  7. BZOJ 1487 无归岛
  8. BZOJ 1027 合金
  9. 【技术贴】同一台机器Tomcat7多版本共存配置文档
  10. SKYLINE