性能监控之内存篇

RAM(Random-access memory),内存,是非常重要的资源,尤其是在手机这样的受限设备上。
不正确的使用会导致内存泄漏、内存抖动等问题,影响app性能及用户体验,因此我们需要对app内存使用状况进行监控。

那么app内存的使用状况应该怎样查看呢?

内存查看工具

先让我们使用Android Studio内置的开发工具Profiler来看一下app的内存使用状况:

Image of Profiler

我们可以看到红框标出的实时采集的内存状况。

那么这些值分别来自哪里呢?让我们再用dumpsys meminfo来看一下。

adb shell dumpsys meminfo com.jingdong.shperfmonitor
Image of dumpsys-meminfo

我们可以看到红框里的各项数值基本与之前我们使用Android Studio Profiler获取到的各项数值一一对应。
也就是说,app内存统计的实际是一个叫Pss的值。那么,什么是Pss?旁边的Private Dirty/Clean这些又代表什么?

内存的定义

摘自官方文档

  • Private (Clean and Dirty) RAM

This is memory that is being used by only your process. This is the bulk of the RAM that the system can reclaim when your app’s process is destroyed. Generally, the most important portion of this is private dirty RAM, which is the most expensive because it is used by only your process and its contents exist only in RAM so can’t be paged to storage (because Android does not use swap). All Dalvik and native heap allocations you make will be private dirty RAM; Dalvik and native allocations you share with the Zygote process are shared dirty RAM.

  • Proportional Set Size (PSS)

This is a measurement of your app’s RAM use that takes into account sharing pages across processes. Any RAM pages that are unique to your process directly contribute to its PSS value, while pages that are shared with other processes contribute to the PSS value only in proportion to the amount of sharing. For example, a page that is shared between two processes will contribute half of its size to the PSS of each process.

摘自官方文档

The Dalvik heap is constrained to a single virtual memory range for each app process. This defines the logical heap size, which can grow as it needs to but only up to a limit that the system defines for each app.

The logical size of the heap is not the same as the amount of physical memory used by the heap. When inspecting your app's heap, Android computes a value called the Proportional Set Size (PSS), which accounts for both dirty and clean pages that are shared with other processes—but only in an amount that's proportional to how many apps share that RAM. This (PSS) total is what the system considers to be your physical memory footprint.

Pss(Proportional Set Size)并不等同于实际物理内存的大小,它包含了Private (Clean and Dirty) RAM,并且考虑了内存共享的情况,将共享的内存均摊到每一个参与共享的进程上。因此这个值可以比较准确的表示你的memory footprint。

那么,在app运行时我们如何获得这个值呢?

API

1. High level API

调用ActivityManager#getMemoryInfo()得到的ActivityManager.MemoryInfo中提供了系统当前的内存状况:

  • availMem 系统剩余可用内存
  • totalMem 系统总内存
  • threshold 系统剩余内存阈值,小于这个值后系统开始杀进程
  • lowMemory 系统是否处于低内存状态

2. Low level API

调用Debug.getMemoryInfo()或者ActivityManager#getProcessMemoryInfo(int[])得到的Debug.MemoryInfo中提供了进程当前的内存状况,归为dalvik, native, other三类:

/** The proportional set size for dalvik. */
public int dalvikPss;
/** The private dirty pages used by dalvik. */
public int dalvikPrivateDirty;
/** The shared dirty pages used by dalvik. */
public int dalvikSharedDirty;

/** The proportional set size for the native heap. */
public int nativePss;
/** The private dirty pages used by the native heap. */
public int nativePrivateDirty;
/** The shared dirty pages used by the native heap. */
public int nativeSharedDirty;

/** The proportional set size for everything else. */
public int otherPss;
/** The private dirty pages used by everything else. */
public int otherPrivateDirty;
/** The shared dirty pages used by everything else. */
public int otherSharedDirty;

注意:

3. 虚拟机内存状况API

Android系统中,通常每个应用运行在一个自己的进程中,而每一个进程都拥有一个独立的虚拟机实例。
通过如下API可以获得应用当前的内存状况:

Runtime runtime = Runtime.getRuntime();
long freeMemory = runtime.freeMemory(); // 应用剩余可用内存
long totalMemory = runtime.totalMemory(); // 应用当前使用的总内存
long maxMemory = runtime.maxMemory(); // 应用最多可以使用的内存

为了保证多任务的正常使用,Android系统对每个app可以使用的内存设置了"硬"上限。
这个值根据设备内存的不同而不同,可以使用上述的Runtime#maxMemory()或者ActivityManager#getMemoryClass()获得。
如果你的应用达到这个值后继续申请内存,就会收到OutOfMemoryError

监控方案

根据以上这些API,我们可以实现最终的监控方案:

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,976评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,249评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,449评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,433评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,460评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,132评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,721评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,641评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,180评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,267评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,408评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,076评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,767评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,255评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,386评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,764评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,413评论 2 358

推荐阅读更多精彩内容

  • 几个月之前看过一遍,结果过一阵子在查bitmap造成内存泄露时又感觉忘得差不多了,不晓得android os RA...
    mrz_233333阅读 420评论 0 0
  • 内存管理机制概述 Android 的内存管理机制可以简单概括为:系统没有为内存提供交换区,它使用 paging (...
    小面包屑阅读 2,186评论 0 1
  • 自从父亲去逝后,我们家就成了“谈嫂色变”,嫂子成了我们家的母老虎,我们姊妹六个,哥姐们把嫂子当神供着,唯有我不知内...
    叶子_aba5阅读 5,479评论 42 102
  • 夏天到了,瞌睡虫王国一片沸腾,它们纷纷飞出洞口去寻找自己的朋友。 一只瞌睡虫正在想自己找谁做朋友...
    小郑童鞋阅读 4,424评论 0 7
  • 本周作业:记得带着觉知去种下智慧的种子定向回向给自己可以种下健康的种子! 早上听赵老师和李敏的分享,随喜赵老师种下...
    whj夏日心情阅读 140评论 0 2