Objective C 监测App memory和cpu 使用size

又又get到一个比较实用的东西,记录分享一下。

在写app 时候是不是很苦恼memory 和cpu的使用,如何有效的监测app 的memory 和cpu size 使用情况?

我最近就遇到这个情况,无奈之举,目前的打算是在app内部监测使用情况,当超标的时候重启app。

下面是code的处理部分,跟activity monitor相比较,有那么一丢丢误差,不过在可接受范围内。

Memory Usage


- (int64_t)memoryUsage {
    int64_t memoryUsageInByte = 0;
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    kern_return_t kernelReturn = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &vmInfo, &count);
    if(kernelReturn == KERN_SUCCESS) {
        memoryUsageInByte = (int64_t) vmInfo.phys_footprint;
        NSLog(@"Memory in use : %@MB", [self calMemorySize:memoryUsageInByte]);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
    }
    return memoryUsageInByte;
}

-(NSString*)calMemorySize:(int64_t)memoryUsageInByte
{
    NSString *tmp = [NSString stringWithFormat:@"%lld",memoryUsageInByte];
    NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:tmp];
    return [NSString stringWithFormat:@"%.2f",[num floatValue] / 1024/1024];
}

CPU Usage


- (float)cpu_usage
{
   kern_return_t kr;
   ///任务信息
   task_info_data_t tinfo;
   ///任务个数
   mach_msg_type_number_t task_info_count;
   ///最大1024
   task_info_count = TASK_INFO_MAX;
   ///获取当前执行的任务信息和个数
   kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
   ///判断是否获取成功
   if (kr != KERN_SUCCESS) {
       return -1;
   }
   ///基础任务
   task_basic_info_t      basic_info;
   ///线程数组
   thread_array_t        thread_list;
   ///线程个数
   mach_msg_type_number_t thread_count;
   ///线程信息
   thread_info_data_t    thinfo;
   ///线程信息个数
   mach_msg_type_number_t thread_info_count;
   ///基础线程信息
   thread_basic_info_t basic_info_th;
   ///存储运行的线程
   uint32_t stat_thread = 0;
   
   basic_info = (task_basic_info_t)tinfo;
   ///获取当前执行的线程数组和个数
   kr = task_threads(mach_task_self(), &thread_list, &thread_count);
   ///判断是否成功
   if (kr != KERN_SUCCESS) {
       return -1;
   }
   
   if (thread_count > 0) {
       stat_thread += thread_count;
   }
   long tot_sec = 0;
   long tot_usec = 0;
   float tot_cpu = 0;
   int j;
   ///遍历所有线程
   for (j = 0; j < (int)thread_count; j++) {
       ///线程信息最大个数
       thread_info_count = THREAD_INFO_MAX;
       ///获取线程的基础信息和信息个数
       kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
                        (thread_info_t)thinfo, &thread_info_count);
       ///判断是否成功
       if (kr != KERN_SUCCESS) {
           return -1;
       }
       ///转换基础信息类型
       basic_info_th = (thread_basic_info_t)thinfo;
       ///判断不是闲置线程信息
       if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
           ///使用时间计算
           tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
           tot_usec = tot_usec + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;
           ///使用率计算
           tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
       }
   }
   ///释放指针
   kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
   ///成功
   assert(kr == KERN_SUCCESS);
   ///返回CPU使用率
    return tot_cpu;
}
-(NSString*)calCPUSize:(float)CPUUsageSize
{
    NSString *tmp = [NSString stringWithFormat:@"%f",CPUUsageSize];
    NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:tmp];
    return [NSString stringWithFormat:@"%.1f",[num floatValue]];
}
posted @ 2020-04-03 09:50  萧蔷ink  阅读(373)  评论(0编辑  收藏  举报