Programmatically retrieve memory usage on iPhone

前端 未结 9 981
迷失自我
迷失自我 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条回答
  •  忘掉有多难
    2020-11-22 17:39

    To get the actual bytes of memory that your application is using, you can do something like the example below. However, you really should become familiar with the various profiling tools as well as they are designed to give you a much better picture of usage over-all.

    #import 
    
    // ...
    
    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));
      }
    }
    

    There is also a field in the structure info.virtual_size which will give you the number of bytes available virtual memory (or memory allocated to your application as potential virtual memory in any event). The code that pgb links to will give you the amount of memory available to the device and what type of memory it is.

提交回复
热议问题