安卓性能专项之CPU内存篇

对手游进行测试的时候,需要留意游戏在各种常见场景下CPU,内存的使用情况。

空闲状态的场景,比如主城界面待机,游戏Home到后台运行。

高负荷状态的场景,比如多人同屏,连续的战斗场景等。

怎么去获取当前的CPU,内存占用情况呢?有两个比较常用的方式:

APP性能测试工具

第三方的APP性能测试工具:GT,Wetest,Emmagee等。

使用方式:各APP的帮助文件已经详细说明了~不再重复~

adb方式

查看CPU占用

1
adb shell dumpsys cpuinfo

可以看到类似的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CPU usage from 16749ms to 1803ms ago:
24% 15167/com.supercell.clashroyale.qihoo: 23% user + 1% kernel / faults: 504 minor
7.8% 2146/surfaceflinger: 3.3% user + 4.4% kernel
3% 16479/com.qihoo.daemon: 2.5% user + 0.5% kernel / faults: 267 minor
......
0% 2867/com.android.phone: 0% user + 0% kernel
0% 2937/com.meizu.cloud: 0% user + 0% kernel
0% 3499/sogou.mobile.explorer.hotwords: 0% user + 0% kernel
0% 3641/com.android.settings: 0% user + 0% kernel
0% 14666/com.github.uiautomator: 0% user + 0% kernel
0% 15156/ksoftirqd/1: 0% user + 0% kernel
0% 15218/com.jedigames.qh360:QRemote: 0% user + 0% kernel / faults: 2 minor
0% 31582/wpa_supplicant: 0% user + 0% kernel
9.4% TOTAL: 7.3% user + 2% kernel + 0% iowait + 0% softirq

可以通过指定游戏包名来减少显示的结果,以最近在玩的皇室战争为例:

1
adb shell dumpsys cpuinfo |findstr "com.supercell.clashroyale.qihoo TOTAL" # 查找包含com.supercell.clashroyale.qihoo或者TOTAL的行

显示的结果减少为:

1
2
33% 15167/com.supercell.clashroyale.qihoo: 28% user + 4.8% kernel / faults: 28531 minor 20 major
12% TOTAL: 7.8% user + 3.3% kernel + 0.7% iowait + 0.1% softirq

这里表示:当前CPU总占用为12%,com.supercell.clashroyale.qihoo(皇室战争)的CPU占用为12%*33%=3.96%

查看内存占用

1
adb shell dumpsys meminfo com.supercell.clashroyale.qihoo

得到以下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Applications Memory Usage (kB):
Uptime: 79407732 Realtime: 700252674
** MEMINFO in pid 18040 [com.supercell.clashroyale.qihoo] **
Pss Private Private Swapped Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 0 0 0 0 106160 52209 262
Dalvik Heap 13819 13380 0 0 67460 29143 38317
Dalvik Other 4046 3976 0 0
Stack 344 344 0 0
Ashmem 138 128 0 0
Other dev 85959 3032 82852 0
.so mmap 5421 1016 2200 0
.jar mmap 0 0 0 0
.apk mmap 1414 0 1232 0
.ttf mmap 655 0 428 0
.dex mmap 4206 44 2632 0
Other mmap 65 12 12 0
Unknown 42884 42876 0 0
TOTAL 158951 64808 89356 0 173620 81352 38579
Objects
Views: 137 ViewRootImpl: 5
AppContexts: 4 Activities: 2
Assets: 5 AssetManagers: 5
Local Binders: 23 Proxy Binders: 44
Death Recipients: 13
OpenSSL Sockets: 9
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 62

这里表示:当前的应用,内存总占用是158951KB

PS:几个内存相关的术语,一般关注PSS。

VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)