以编程方式检索iPhone上的内存使用情况我正在尝试检索我的iPhone应用程序在任何时候以编程方式使用的内存量。是的,我知道Objectalloc/泄漏。我对这些并不感兴趣,只是想知道是否有可能编写一些代码,获取所使用的字节数并通过NSLog报告它。
3 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
#import <mach/mach.h>// ...void report_memory(void) { struct task_basic_info info; mach_msg_type_number_t size = TASK_BASIC_INFO_COUNT; 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): %lu", info.resident_size); NSLog(@"Memory in use (in MiB): %f", ((CGFloat)info.resident_size / 1048576)); } else { NSLog(@"Error with task_info(): %s", mach_error_string(kerr)); }}
料青山看我应如是
TA贡献1772条经验 获得超8个赞
TASK_BASIC_INFO
/* Don't use this, use MACH_TASK_BASIC_INFO instead */
MACH_TASK_BASIC_INFO
:
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)); }}
慕无忌1623718
TA贡献1744条经验 获得超4个赞
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;}
- 3 回答
- 0 关注
- 554 浏览
添加回答
举报
0/150
提交
取消