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=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)); } }
时间: 2024-11-04 05:46:27