iOS 获取设备占用内存

获取应用占用内存

  1. 获取应用进程占用内存
c 复制代码
- (NSUInteger)memoryUsage {
    task_vm_info_data_t vmInfo;
    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
    kern_return_t result = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &count);
    if (result != KERN_SUCCESS) {
        return 0;
    }
    // 单位为字节
    return (NSUInteger)vmInfo.phys_footprint; 
}
  1. 获取free内存(与xcode基本一致)
c 复制代码
- (double)usageComparisonFree {
    mach_port_t host_port = mach_host_self();
    mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
    
    vm_size_t page_size;
    vm_statistics64_data_t vminfo;
    host_page_size(host_port, &page_size);
    host_statistics64(host_port, HOST_VM_INFO64, (host_info64_t)&vminfo,&count);
    
    uint64_t free_size = (vminfo.free_count + vminfo.external_page_count + vminfo.purgeable_count - vminfo.speculative_count) * page_size;
    return free_size / 1024.0 / 1024.0;
}
  1. 获取APP可用内存
c 复制代码
- (NSInteger)memoryUsageLimitByByte {
    NSInteger memoryLimit = 0;
    // 获取当前内存使用数据
    if (@available(iOS 13.0, *)) {
        task_vm_info_data_t vmInfo;
        mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
        kern_return_t kr = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&vmInfo, &count);
        if (kr == KERN_SUCCESS) {
            // 间接获取一下当前进程可用的最大内存上限
            // iOS13+可以这样计算:当前进程占用内存+还可以使用的内存=上限值
            NSInteger memoryCanBeUse = (NSInteger)(os_proc_available_memory());
            if (memoryCanBeUse > 0) {
                NSInteger memoryUsed = (NSInteger)(vmInfo.phys_footprint);
                memoryLimit = memoryUsed + memoryCanBeUse;
            }
        }
    }
    if (memoryLimit <= 0) {
        NSInteger deviceMemory = [NSProcessInfo processInfo].physicalMemory;
        memoryLimit = deviceMemory * 0.55;
    }
    if (memoryLimit <= 0) {
        // 这个值一般要小很多, 上面都获取不到才使用
        mach_port_t host_port = mach_host_self();
        mach_msg_type_number_t host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
        vm_size_t page_size;
        vm_statistics_data_t vm_stat;
        kern_return_t kr;
        kr = host_page_size(host_port, &page_size);
        if (kr == KERN_SUCCESS) {
            kr = host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size);
            if (kr == KERN_SUCCESS)            {
                memoryLimit = vm_stat.free_count * page_size;
            }
        }
    }
    // 单位 Byte
    return memoryLimit;
}
  1. 获取应用cpu使用情况
c 复制代码
- (float)cpuUsage {
    kern_return_t kr;
    task_info_data_t tinfo;
    mach_msg_type_number_t task_info_count = TASK_INFO_MAX;
    
    kr = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)tinfo, &task_info_count);
    if (kr != KERN_SUCCESS) return -1;
    
    task_thread_times_info_t times_info = (task_thread_times_info_t)tinfo;
    double userTime = times_info->user_time.seconds + times_info->user_time.microseconds / 1e6;
    double systemTime = times_info->system_time.seconds + times_info->system_time.microseconds / 1e6;
    double totalTime = userTime + systemTime;
    
    static double lastTotalTime = 0;
    static NSTimeInterval lastSamplingTime = 0;
    
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval elapsedTime = currentTime - lastSamplingTime;
    float cpuUsage = 0.0;
    
    if (lastSamplingTime != 0 && elapsedTime > 0) {
        cpuUsage = (totalTime - lastTotalTime) / elapsedTime * 100;
    }
    
    lastTotalTime = totalTime;
    lastSamplingTime = currentTime;
    
    return cpuUsage;
}
相关推荐
2501_915909061 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_915106322 天前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
要站在顶端2 天前
iOS自动化测试全流程教程(基于WebDriverAgent+go-ios)
开发语言·ios·golang
2501_916008892 天前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
腾讯云qcloud07552 天前
腾讯位置商业授权iOS 轨迹SDK
macos·ios·cocoa
2501_916007472 天前
没有 Mac,如何在 Windows 上架 iOS 应用?一套可落地的工程方案
android·macos·ios·小程序·uni-app·iphone·webview
2501_915106322 天前
uni-app 上架 iOS 的完整实践,从跨端开发到稳定提交的工程路径
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916007472 天前
HTTPS工作原理与重要性:全面安全指南
网络协议·安全·ios·小程序·https·uni-app·iphone
函数的彼端3 天前
iOS Model Generator - 让 JSON 转模型变得简单高效
ios·json·cocoa
2501_915918413 天前
HTTPS 端口深度解析,443 并不是唯一入口,理解 TLS 流量行为与抓包策略
网络协议·http·ios·小程序·https·uni-app·iphone