Programmatically retrieve memory usage on iPhone

前端 未结 9 948
迷失自我
迷失自我 2020-11-22 16:56

I\'m trying to retrieve the amount of memory my iPhone app is using at anytime, programmatically. Yes I\'m aware about ObjectAlloc/Leaks. I\'m not interested in those, only

9条回答
  •  萌比男神i
    2020-11-22 17:48

    The headers forTASK_BASIC_INFO say:

    /* Don't use this, use MACH_TASK_BASIC_INFO instead */
    

    Here is a version using 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));
        }
    }
    

提交回复
热议问题