检测内存

Objective-c代码  收藏代码
//检测内存  
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;  
}  

 

posted @ 2012-04-12 21:33  生活不是用来挥霍的  阅读(286)  评论(0编辑  收藏  举报