当我们想去获取 iOS 应用的占用内存时,通常我们能找到的方法是这样的,用 resident_size

 
#import <mach/mach.h>
- (int64_t)memoryUsage {
int64_t memoryUsageInByte = ;
struct task_basic_info taskBasicInfo;
mach_msg_type_number_t size = sizeof(taskBasicInfo);
kern_return_t kernelReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) &taskBasicInfo, &size); if(kernelReturn == KERN_SUCCESS) {
memoryUsageInByte = (int64_t) taskBasicInfo.resident_size;
NSLog(@"Memory in use (in bytes): %lld", memoryUsageInByte);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
} return memoryUsageInByte;
}

但是测试的时候,我们会发现这个跟我们在 Instruments 里面看到的内存大小不一样,有时候甚至差别很大。

更加准确的方式应该是用 phys_footprint

#import <mach/mach.h>
- (int64_t)memoryUsage {
int64_t memoryUsageInByte = ;
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 (in bytes): %lld", memoryUsageInByte);
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kernelReturn));
}
return memoryUsageInByte;
}

关于 phys_footprint 的定义可以在 XNU 源码中,找到 osfmk/kern/task.c 里对于 phys_footprint 的注释:

/*
* phys_footprint
* Physical footprint: This is the sum of:
* + (internal - alternate_accounting)
* + (internal_compressed - alternate_accounting_compressed)
* + iokit_mapped
* + purgeable_nonvolatile
* + purgeable_nonvolatile_compressed
* + page_table
*
* internal
* The task's anonymous memory, which on iOS is always resident.
*
* internal_compressed
* Amount of this task's internal memory which is held by the compressor.
* Such memory is no longer actually resident for the task [i.e., resident in its pmap],
* and could be either decompressed back into memory, or paged out to storage, depending
* on our implementation.
*
* iokit_mapped
* IOKit mappings: The total size of all IOKit mappings in this task, regardless of
clean/dirty or internal/external state].
*
* alternate_accounting
* The number of internal dirty pages which are part of IOKit mappings. By definition, these pages
* are counted in both internal *and* iokit_mapped, so we must subtract them from the total to avoid
* double counting.
*/

注释里提到的公式计算的应该才是应用实际使用的物理内存。

转载自新浪博客

获取 iOS APP 内存占用的大小的更多相关文章

  1. iOS app内存分析套路

    iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...

  2. iOS App内存优化之 解决UIImagePickerController的图片对象占用RAM过高问题

    这个坑会在特定的情况下特别明显: 类似朋友圈的添加多张本地选择\拍照 的图片 并在界面上做一个预览功能 由于没有特别的相机\相册需求,则直接使用系统自带的UIImagePickerController ...

  3. 如何获取 iOS APP 的 scheme URL ?

    获取IPA文件 拷贝到桌面上 后缀名由 .ipa 改为 .zip 解压之后进入,进入名为Payload的目录 右键点击里面的跟App同名的文件,选择'显示包内容' 用文本编辑器打开当前文件夹下的inf ...

  4. 通过ideviceinstaller获取IOS APP bundleId

    查看ios设备udid: idevice_id -l 查看ios应用的bundleId: # 安装ideviceinstaller brew install ideviceinstaller # 查看 ...

  5. 关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析

    原文:关于Android中图片大小.内存占用与drawable文件夹关系的研究与分析 相关: Android drawable微技巧,你所不知道的drawable的那些细节 经常会有朋友问我这个问题: ...

  6. 3、获取APP 内存占用率

    关于APP内存占用,不用多说,应该是APP性能测试中比较重要的一点.试想一下,开个应用把手机内存占满了,其它应用无法打开,那么这个应用还会有人安装吗?我觉得是没有的.下面就通过adb命令获取APP虚存 ...

  7. android应用内存占用测试(每隔一秒打印procrank的信息)

    1.内存占用 对于智能手机而言,内存大小是固定的:因此,如果单个app的内存占用越小,手机上可以安装运行的app就越多:或者说app的内存占用越小,在手机上运行就会越流畅.所以说,内存占用的大小,也是 ...

  8. Linux系统下输出某进程内存占用信息的c程序实现

    在实际工作中有时需要程序打印出某个进程的内存占用情况以作参考, 下面介绍一种通过Linux下的伪文件系统/proc 计算某进程内存占用的程序实现方法. 首先, 为什么会有所谓的 伪文件 呢. Linu ...

  9. Linux下计算进程的CPU占用和内存占用的编程方法[转]

    from:https://www.cnblogs.com/cxjchen/archive/2013/03/30/2990548.html Linux下没有直接可以调用系统函数知道CPU占用和内存占用. ...

随机推荐

  1. JQ的几组API辨析:

    1.插入: Jq的插入一共有八个API可供选择,结果相类似,下面将以例子,简单明了的介绍下用法: <select name="one" multiple="mult ...

  2. Linux Notes

    Do what we want based on what others already did with additional abstraction and organization to ser ...

  3. The difference between creating a string object constructor and assigning it directly

    字符串对象构造方法创建和直接赋值的区别? package com.itheima_02; /* * 通过构造方法创建的字符串对象和直接赋值方式创建的字符串对象有什么区别呢? * 通过构造方法创建字符串 ...

  4. bat 常见问题及小实例

    bat 常用命令小实例 常见问题: 1.如果你自己编写的.bat文件,双击打开,出现闪退 原因:执行速度很快,执行完之后,自行关闭 解决办法:在最后面一行加上 pause 例如: @echo off ...

  5. python item repr doc format slots doc module class 析构 call 描述符

    1.item # __getitem__ __setitem__ __delitem__ obj['属性']操作触发 class Foo: def __getitem__(self, item): r ...

  6. 需求分析之WBS

    我们的产品就是当你把废旧的塑料瓶和电池投入回收箱中会产生能量,那么这能量可以干嘛呢那就通过我们的APP进行扫码记录所产生的能量这写能量可以在我们的APP的换吧中进行兑换用户所需要的东西比如:共享单车的 ...

  7. SQL读取注册表值

    最近写一个自动检查SQL Serve安全配置的检查脚本,需要查询注册表,下面是使用SQL查询注册表值的方法. ) ) ) ) --For Named instance --SET @Instance ...

  8. Current_Path 获取脚本所在路径(当前路径),取当前时间做文件名(uformat)

    获取脚本当前所在路径: $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.Las ...

  9. 第一课 PPT 所学内容总结

    制作PPT时要注意三要素即:图形,颜色搭配,字数适当. 感悟:制作一个好的PPT也并不需要华丽的画面.只需清晰的表达出自己想要表达的,就是一个好PPT.

  10. iPhone的设备名转换

    def convertDeviceName(self, deviceName): """ 转换deviceName(如iPhone 6,2)为用户习惯形式(如iPhone ...