1. procstats
procstats
是 android 提供的一个用于分析内存使用情况的工具,可以查看一段时间内应用使用的内存信息,与之对应的是用于查看当前内存使用情况的工具 meminfo
。它用于查看一段时间内应用使用的 PSS(proportional set size)和 USS(unique set size)。
VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS- Proportional Set Size 实际使用的物理内(USS大小 + 私有库大小 + 按进程数分配共享库大小)
USS- Unique Set Size 进程独占的物理内存(不包含共享库占用的内存)
2. 使用说明
首先使用之前说到过的 -h
查看帮助信息:
adb shell dumpsys procstats -h
得到一下结果:
Process stats (procstats) dump options:
[--checkin|-c|--csv] [--csv-screen] [--csv-proc] [--csv-mem]
[--details] [--full-details] [--current] [--hours N] [--last N]
[--max N] --active] [--commit] [--reset] [--clear] [--write] [-h]
[--start-testing] [--stop-testing] [<package.name>]
--checkin: perform a checkin: print and delete old committed states.
-c: print only state in checkin format.
--csv: output data suitable for putting in a spreadsheet.
--csv-screen: on, off.
--csv-mem: norm, mod, low, crit.
--csv-proc: pers, top, fore, vis, precept, backup,
service, home, prev, cached
--details: dump per-package details, not just summary.
--full-details: dump all timing and active state details.
--current: only dump current state.
--hours: aggregate over about N last hours.
--last: only show the last committed stats at index N (starting at 1).
--max: for -a, max num of historical batches to print.
--active: only show currently active processes/services.
--commit: commit current stats to disk and reset to start new stats.
--reset: reset current stats, without committing.
--clear: clear all stats; does both --reset and deletes old stats.
--write: write current in-memory stats to disk.
--read: replace current stats with last-written stats.
--start-testing: clear all stats and starting high frequency pss sampling.
--stop-testing: stop high frequency pss sampling.
-a: print everything.
-h: print this help text.
<package.name>: optional name of package to filter output by.
看以上帮助信息最后一条,能够查看单个 apk 的信息。看到 -a
参数的说明,打印所有信息,尝试一下,15977
条信息。
3. 示例
使用 -a
得到的输出结果太多,但是我们还是需要看一下里面有哪些内容,好在输出的内容的格式比较统一,层级比较明显,找到不是以空白符开始的行即可以看到所有类别的信息:
COMMITTED STATS FROM 2016-06-15-12-40-29:
Run time Stats:
Memory usage:
COMMITTED STATS FROM 2016-06-15-17-16-21:
Run time Stats:
Memory usage:
CURRENT STATS:
System memory usage:
Per-Package Stats:
Multi-Package Common Processes:
Summary:
Run time Stats:
Memory usage:
Internal state:
AGGREGATED OVER LAST 24 HOURS:
System memory usage:
Per-Package Stats:
Multi-Package Common Processes:
Summary:
Run time Stats:
Memory usage:
Internal state:
AGGREGATED OVER LAST 3 HOURS:
System memory usage:
Per-Package Stats:
Multi-Package Common Processes:
Summary:
Run time Stats:
Memory usage:
Internal state:
可以看到每一部分的结构比较相似,选用参考链接中使用的示例来进行解读。
查看 3 小时内的内存使用情况,命令:
adb shell dumpsys procstats --hours 3
得到如下输出:
AGGREGATED OVER LAST 3 HOURS:
* com.google.android.gms.persistent / u0a7 / v9877438:
TOTAL: 100% (31MB-44MB-55MB/25MB-40MB-48MB over 14)
Top: 0.49% (44MB-44MB-44MB/36MB-37MB-38MB over 2)
Imp Fg: 100% (31MB-44MB-55MB/25MB-40MB-48MB over 12)
* com.android.phone / 1001 / v23:
TOTAL: 100% (14MB-28MB-40MB/13MB-27MB-39MB over 13)
Persistent: 100% (14MB-28MB-40MB/13MB-27MB-39MB over 13)
* com.sina.weibo / u0a122 / v3249:
TOTAL: 100% (22MB-130MB-191MB/16MB-127MB-190MB over 11)
Top: 54% (190MB-191MB-191MB/188MB-189MB-190MB over 7)
Service: 46% (22MB-23MB-25MB/16MB-17MB-19MB over 4)
* system / 1000 / v23:
TOTAL: 100% (70MB-94MB-108MB/65MB-90MB-107MB over 13)
Persistent: 100% (70MB-94MB-108MB/65MB-90MB-107MB over 13)
* com.android.systemui / u0a12 / v23:
TOTAL: 100% (92MB-114MB-128MB/86MB-110MB-126MB over 13)
Persistent: 100% (92MB-114MB-128MB/86MB-110MB-126MB over 13)
// 省略 N 条记录,阅读方便,
* com.google.android.apps.docs / u0a35 / v63512435:
(Cached): 2.5%
* com.google.android.music:main / u0a58 / v35190:
(Cached): 15%
* com.google.android.apps.docs.editors.sheets / u0a80 / v61920835:
(Cached): 2.2%
* com.google.android.apps.docs.editors.docs / u0a79 / v61920635:
(Cached): 2.2%
* com.google.process.gapps / u0a100 / v23:
(Cached): 24% (2.9MB-2.9MB-2.9MB/2.2MB-2.2MB-2.2MB over 1)
Run time Stats:
SOff/Norm: +1h51m46s794ms
Mod : +2m32s783ms
Low : +1s901ms
SOn /Norm: +2m17s649ms
TOTAL: +1h56m39s127ms
Memory usage:
Kernel : 132MB (24 samples)
Native : 63MB (24 samples)
Persist: 242MB (130 samples)
Top : 154MB (109 samples)
ImpFg : 165MB (324 samples)
ImpBg : 63MB (190 samples)
Service: 319MB (732 samples)
Receivr: 56KB (250 samples)
Home : 41MB (26 samples)
CchEmty: 204MB (388 samples)
Cached : 263MB (24 samples)
Free : 70MB (24 samples)
TOTAL : 1.7GB
ServRst: 190KB (180 samples)
Start time: 2016-06-15 17:16:21
Total elapsed time: +18h19m26s408ms (partial) libart.so
以上输出可以分为三部分:
1) 进程详情
格式:
进程名称/USER/VersionCode
状态:频率(minPSS-avgPSS-maxPSS/minUSS-avgUSS-maxUSS over samples)
2) 运行时间统计信息
不同运行时的统计
3) 内存使用统计信息
不同状态进程使用的内存统计
4) 统计的时间段信息
这个时间怎么看都不对啊!