当我们想去获取 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. layui-open-上传文件

    <!--选择文件上传--> <script id="upload_file_dialog" type="text/html"> < ...

  2. 【MFC】CHtmlView或WebBrowser禁止脚本错误提示

    错误展示: 解决办法: 1.CHtmlView类或子类 CHtmlView::SetSilent(TRUE); 2.IWebBrowser2控件 IWebBrowser2::put_Silent(TR ...

  3. webstorm git 怎么断开版本控制 webstorm git for windows 禁止 自动运行

    也是无语啊,今天装了下最新版本的webstorm ,  发现特别卡,老动不动就卡死, 看了下进程, 牛X 啊,  git for windows 一直蹭蹭蹭的疯狂增长,一开始的一点到后来的庞然大物. ...

  4. 微服务架构之spring cloud turbine

    在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断 ...

  5. Codeforces Round #417 C. Sagheer and Nubian Market

    C. Sagheer and Nubian Market time limit per test  2 seconds memory limit per test  256 megabytes   O ...

  6. python装饰器 高阶函数 函数闭包

    1.装饰器: 本质是函数,功能是为其他函数添加附加功能 原则:1.不修改被装饰函数的源代码 2.不修改被修饰函数的调用方式 装饰器=高阶函数+函数嵌套+闭包 #装饰器格式框架def wrap(func ...

  7. malloc()函数,calloc()函数,realloc()函数,free()函数

    malloc()函数 头文件:#include <stdlib.h> malloc() 函数用来动态地分配内存空间,其原型为:void* malloc (size_t size); [参数 ...

  8. 【Leetcode】【Easy】Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  9. Windows下Git服务端和客户端的搭建

    1.服务器端的搭建 本人使用的是一款带源码的工具:bonobogitserver,对应的网址为:https://bonobogitserver.com/ 具体操作方式如下: 1.打开网址,下载最新版本 ...

  10. Asp.Net MVC源码调试

    首先下载MVC源代码,下载地址为:https://aspnetwebstack.codeplex.com/ 打开项目,卸载test文件夹下的所有项目和System.Web.WebPages.Admin ...