在Android系統開發過程中,經常會要去分析進程的內存的使用情況,簡單介紹下Android內存分析的相關工具。
文章參考:
1.內存的指標
Item | 全稱 | 描述 | 類型 |
---|---|---|---|
USS | Unique Set Size | 進程獨占的內存(不包含共享庫占用的內存) | 物理內存 |
PSS | Proportional Set Size | 進程獨占 + 比例分配共享庫占用的內存 | 物理內存 |
RSS | Resident Set Size | 進程獨占 + 共享庫占用的內存 | 物理內存 |
VSS | Virtual Set Size | RSS + 未分配的物理內存 | 虛擬內存 |
2.內存分析工具
2.1 proc/meminfo
查看詳細的內存信息
# cat /proc/meminfo
MemTotal: 3938668 kB
MemFree: 2766932 kB
MemAvailable: 3366724 kB
Buffers: 54764 kB
Cached: 683932 kB
SwapCached: 0 kB
Active: 468604 kB
Inactive: 493776 kB
Active(anon): 226044 kB
Inactive(anon): 114680 kB
Active(file): 242560 kB
Inactive(file): 379096 kB
Unevictable: 256 kB
Mlocked: 256 kB
SwapTotal: 1969000 kB
SwapFree: 1969000 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 223980 kB
Mapped: 376328 kB
Shmem: 117060 kB
Slab: 119740 kB
SReclaimable: 80184 kB
SUnreclaim: 39556 kB
KernelStack: 11872 kB
PageTables: 15752 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 3938332 kB
Committed_AS: 40624220 kB
VmallocTotal: 258867136 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
CmaTotal: 16384 kB
CmaFree: 13892 kB
-
MemTotal:表示系統內存總數
系統從上電啟動到引導完成,內核中需要保留部分內存,剩下的內存總數即為 MemTotal,這個值在系統運行期間保持不變。
-
MemFree:空閑內存總數
表示系統中尚未使用的內存, MemTotal - MemFree 表示已經用掉的內存。
-
MemAvailable:可用內存
應用程序可用內存數,系統中存在一些已經被使用但是可以回收的內存,比如Buffers、Cached和slab的部分,所以 MemFree 不能代表全部可用的內存,加上可以回收的內存才是系統可用內存的總和。
比如 MemAvailable ≈ MemFree + Buffers + Cached,這里是內核計算出來的估值。MemFree表示系統層面,MemAvailable泛指應用層面。
-
Buffers:緩沖區內存
-
Cached:緩存區內存
2.2 free
# free
total used free shared buffers
Mem: 4033196032 1199968256 2833227776 119869440 56078336
-/+ buffers/cache: 1143889920 2889306112
Swap: 2016256000 0 2016256000
# free -m
total used free shared buffers
Mem: 3846 1144 2701 114 53
-/+ buffers/cache: 1091 2755
Swap: 1922 0 1922
輕量級的內存查看工具,內容來源於proc/meminfo.
Mem.total = Mem.used + Mem.free,即 3846 = 1144 + 2701;
Mem.used - Mem.buffers = buffers.used,即 1144 - 53 = 1091;
Mem.free + Mem.buffers = buffers.free, 即 2701 + 53 = 2755;
2.3 dumpsys meminfo
輸出的結果大致按照4個部分排序,分別Total PSS by process、Total PSS by OOM adjustment、Total PSS by category 和 Total RAM.
Total PSS by process:以進程的PSS從大到小排序
Total PSS by OOM adjustment:分別顯示每個類型進程的情況,Native/System/Persistent/Foreground/Visible/Perceptible/A Services/Home/B Services/Cached.
Total PSS by category:以Dalvik/Native/.art mmap/.dex map等划分的各類進程的總PSS情況
Total:顯示總內存、可用內存、已使用內存、其他內存.
# dumpsys meminfo
Applications Memory Usage (in Kilobytes):
Uptime: 18012360 Realtime: 92063532
Total PSS by process:
93,509K: system (pid 469)
79,586K: com.android.systemui (pid 628)
63,474K: zygote (pid 378)
32,858K: com.android.launcher3 (pid 1054 / activities)
28,094K: zygote64 (pid 377)
25,557K: webview_zygote32 (pid 687)
25,377K: com.android.phone (pid 749)
18,679K: android.hardware.neuralnetworks@1.0-service-armnn (pid 263)
15,488K: com.android.inputmethod.latin (pid 606)
13,830K: surfaceflinger (pid 269)
13,543K: android.process.media (pid 1170)
12,843K: com.android.email (pid 1302)
10,147K: android.hardware.camera.provider@2.4-service (pid 255)
9,675K: android.rockchip.update.service (pid 1140)
8,219K: com.android.calendar (pid 1850)
8,027K: com.android.providers.calendar (pid 1284)
7,646K: mediaserver (pid 387)
6,977K: com.android.quicksearchbox (pid 1190)
6,827K: com.android.mtp (pid 1876)
6,820K: media.codec (pid 391)
6,675K: com.android.printspooler (pid 1021)
6,587K: audioserver (pid 379)
6,504K: com.cghs.stresstest (pid 1011)
6,449K: media.extractor (pid 385)
6,246K: android.ext.services (pid 1407)
5,812K: com.android.keychain (pid 1114)
5,633K: com.android.smspush (pid 1069)
4,095K: android.hardware.audio@2.0-service (pid 253)
3,916K: cameraserver (pid 380)
3,191K: media.metrics (pid 386)
2,998K: android.hardware.graphics.composer@2.1-service (pid 261)
2,910K: logd (pid 232)
2,803K: drmserver (pid 381)
2,789K: android.hardware.wifi@1.0-service (pid 266)
2,442K: android.hardware.drm@1.0-service (pid 258)
2,405K: vold (pid 245)
2,214K: netd (pid 388)
1,970K: android.hardware.drm@1.0-service.widevine (pid 259)
1,799K: keystore (pid 383)
1,798K: installd (pid 382)
1,707K: /init (pid 1)
1,642K: hwservicemanager (pid 234)
1,509K: adbd (pid 1841)
1,497K: wificond (pid 390)
1,430K: android.hardware.sensors@1.0-service (pid 265)
1,410K: android.hardware.keymaster@3.0-service (pid 244)
1,276K: mediadrmserver (pid 384)
1,243K: android.hardware.graphics.allocator@2.0-service (pid 260)
1,241K: ip6tables-restore (pid 403)
1,234K: gatekeeperd (pid 393)
1,163K: iptables-restore (pid 402)
1,158K: ueventd (pid 207)
1,133K: thermalserviced (pid 270)
1,082K: storaged (pid 389)
1,034K: android.hardware.bluetooth@1.0-service (pid 254)
1,009K: android.hardware.configstore@1.0-service (pid 257)
1,005K: android.hardware.cas@1.0-service (pid 256)
985K: healthd (pid 267)
888K: android.hardware.light@2.0-service (pid 262)
861K: android.hardware.power@1.0-service (pid 264)
839K: servicemanager (pid 233)
801K: vndservicemanager (pid 235)
770K: dumpsys (pid 2032)
759K: android.hidl.allocator@1.0-service (pid 252)
733K: sh (pid 1983)
724K: sh (pid 274)
676K: lmkd (pid 268)
658K: tombstoned (pid 395)
Total PSS by OOM adjustment:
253,080K: Native
63,474K: zygote (pid 378)
28,094K: zygote64 (pid 377)
25,557K: webview_zygote32 (pid 687)
18,679K: android.hardware.neuralnetworks@1.0-service-armnn (pid 263)
13,830K: surfaceflinger (pid 269)
10,147K: android.hardware.camera.provider@2.4-service (pid 255)
7,646K: mediaserver (pid 387)
6,820K: media.codec (pid 391)
6,587K: audioserver (pid 379)
6,449K: media.extractor (pid 385)
4,095K: android.hardware.audio@2.0-service (pid 253)
3,916K: cameraserver (pid 380)
3,191K: media.metrics (pid 386)
2,998K: android.hardware.graphics.composer@2.1-service (pid 261)
2,910K: logd (pid 232)
2,803K: drmserver (pid 381)
2,789K: android.hardware.wifi@1.0-service (pid 266)
2,442K: android.hardware.drm@1.0-service (pid 258)
2,405K: vold (pid 245)
2,214K: netd (pid 388)
1,970K: android.hardware.drm@1.0-service.widevine (pid 259)
1,799K: keystore (pid 383)
1,798K: installd (pid 382)
1,707K: /init (pid 1)
1,642K: hwservicemanager (pid 234)
1,509K: adbd (pid 1841)
1,497K: wificond (pid 390)
1,430K: android.hardware.sensors@1.0-service (pid 265)
1,410K: android.hardware.keymaster@3.0-service (pid 244)
1,276K: mediadrmserver (pid 384)
1,243K: android.hardware.graphics.allocator@2.0-service (pid 260)
1,241K: ip6tables-restore (pid 403)
1,234K: gatekeeperd (pid 393)
1,163K: iptables-restore (pid 402)
1,158K: ueventd (pid 207)
1,133K: thermalserviced (pid 270)
1,082K: storaged (pid 389)
1,034K: android.hardware.bluetooth@1.0-service (pid 254)
1,009K: android.hardware.configstore@1.0-service (pid 257)
1,005K: android.hardware.cas@1.0-service (pid 256)
985K: healthd (pid 267)
888K: android.hardware.light@2.0-service (pid 262)
861K: android.hardware.power@1.0-service (pid 264)
839K: servicemanager (pid 233)
801K: vndservicemanager (pid 235)
770K: dumpsys (pid 2032)
759K: android.hidl.allocator@1.0-service (pid 252)
733K: sh (pid 1983)
724K: sh (pid 274)
676K: lmkd (pid 268)
658K: tombstoned (pid 395)
93,509K: System
93,509K: system (pid 469)
111,467K: Persistent
79,586K: com.android.systemui (pid 628)
25,377K: com.android.phone (pid 749)
6,504K: com.cghs.stresstest (pid 1011)
32,858K: Foreground
32,858K: com.android.launcher3 (pid 1054 / activities)
5,633K: Visible
5,633K: com.android.smspush (pid 1069)
15,488K: Perceptible
15,488K: com.android.inputmethod.latin (pid 606)
9,675K: B Services
9,675K: android.rockchip.update.service (pid 1140)
75,169K: Cached
13,543K: android.process.media (pid 1170)
12,843K: com.android.email (pid 1302)
8,219K: com.android.calendar (pid 1850)
8,027K: com.android.providers.calendar (pid 1284)
6,977K: com.android.quicksearchbox (pid 1190)
6,827K: com.android.mtp (pid 1876)
6,675K: com.android.printspooler (pid 1021)
6,246K: android.ext.services (pid 1407)
5,812K: com.android.keychain (pid 1114)
Total PSS by category:
157,906K: .so mmap
109,632K: Native
106,914K: .dex mmap
56,243K: .oat mmap
44,939K: .apk mmap
32,029K: .art mmap
24,460K: Unknown
23,246K: Dalvik
18,957K: Other mmap
11,423K: .ttf mmap
7,471K: Dalvik Other
2,288K: Stack
879K: Other dev
320K: Ashmem
172K: .jar mmap
0K: Cursor
0K: Gfx dev
0K: EGL mtrack
0K: GL mtrack
0K: Other mtrack
Total RAM: 3,938,668K (status normal)
Free RAM: 3,283,817K ( 75,169K cached pss + 442,724K cached kernel + 2,765,924K free)
Used RAM: 750,258K ( 521,710K used pss + 228,548K kernel)
Lost RAM: -95,419K
ZRAM: 12K physical used for 0K in swap (1,969,000K total swap)
Tuning: 192 (large 512), oom 322,560K, restore limit 107,520K (high-end-gfx)
2.4 procrank
獲取所有進程的內存使用的排行榜,排行是以Pss的大小而排序.
# procrank
PID Vss Rss Pss Uss cmdline
469 4715244K 199568K 93953K 76772K system_server
628 4774604K 184272K 79868K 63260K com.android.systemui
378 1581340K 116848K 63536K 35956K zygote
1054 5048780K 130504K 33086K 20376K com.android.launcher3
377 4239348K 130300K 28338K 8012K zygote64
687 1333924K 54604K 25609K 5504K /system/bin/webview_zygote32
749 4338336K 108900K 25539K 16612K com.android.phone
263 75200K 27620K 18690K 16088K /vendor/bin/hw/android.hardware.neuralnetworks@1.0-service-armnn
606 4330764K 91380K 15649K 9656K com.android.inputmethod.latin
269 312884K 27868K 13869K 10336K /system/bin/surfaceflinger
1170 4321764K 92900K 13701K 6336K android.process.media
1302 4323936K 84736K 13004K 8064K com.android.email
255 53284K 21308K 10199K 6972K /vendor/bin/hw/android.hardware.camera.provider@2.4-service
1140 4317440K 82832K 9876K 3808K android.rockchip.update.service
1850 4318536K 75692K 8379K 4096K com.android.calendar
1284 4315752K 77148K 8185K 3796K com.android.providers.calendar
387 73924K 18420K 7686K 5064K /system/bin/mediaserver
1190 4315400K 71948K 7137K 2800K com.android.quicksearchbox
1876 4315996K 74824K 6986K 2668K com.android.mtp
391 31860K 12104K 6845K 5644K media.codec
1021 4315960K 70452K 6835K 3020K com.android.printspooler
1011 4314012K 69076K 6658K 2864K com.cghs.stresstest
379 43520K 12616K 6621K 4920K /system/bin/audioserver
385 2164032K 18588K 6493K 4324K media.extractor
1407 4315816K 70584K 6403K 2296K android.ext.services
1114 4314788K 69144K 5972K 2240K com.android.keychain
1069 4313648K 67308K 5792K 2156K com.android.smspush
253 16256K 7892K 4116K 2808K /vendor/bin/hw/android.hardware.audio@2.0-service
380 24908K 11124K 3948K 2544K /system/bin/cameraserver
386 2128240K 11312K 3220K 2156K media.metrics
261 33120K 7556K 3022K 2432K /vendor/bin/hw/android.hardware.graphics.composer@2.1-service
232 19996K 5304K 2915K 2820K /system/bin/logd
381 18964K 9884K 2836K 1652K /system/bin/drmserver
266 14404K 6140K 2798K 2604K /vendor/bin/hw/android.hardware.wifi@1.0-service
258 12756K 6560K 2463K 1904K /vendor/bin/hw/android.hardware.drm@1.0-service
245 55064K 7468K 2421K 2024K /system/bin/vold
388 31004K 6548K 2225K 1980K /system/bin/netd
259 11140K 5328K 1985K 1588K /vendor/bin/hw/android.hardware.drm@1.0-service.widevine
383 19280K 6492K 1811K 1348K /system/bin/keystore
382 19676K 5636K 1809K 1616K /system/bin/installd
1 11968K 2436K 1714K 1092K /init
234 14408K 5576K 1653K 1352K /system/bin/hwservicemanager
1841 12932K 1700K 1511K 1504K /system/bin/adbd
390 15096K 5632K 1507K 1312K /system/bin/wificond
265 16888K 5044K 1439K 1164K /vendor/bin/hw/android.hardware.sensors@1.0-service
244 14848K 5904K 1422K 928K /vendor/bin/hw/android.hardware.keymaster@3.0-service
384 10688K 5308K 1297K 804K /system/bin/mediadrmserver
260 17480K 5224K 1261K 960K /vendor/bin/hw/android.hardware.graphics.allocator@2.0-service
393 15560K 5612K 1245K 1000K /system/bin/gatekeeperd
403 9468K 3296K 1244K 1188K /system/bin/ip6tables-restore
402 9452K 3200K 1165K 1116K /system/bin/iptables-restore
207 6080K 1768K 1158K 568K /sbin/ueventd
2038 16940K 3304K 1145K 1088K procrank
270 13924K 5000K 1142K 944K /system/bin/thermalserviced
389 15988K 4292K 1090K 960K /system/bin/storaged
254 12244K 4212K 1042K 868K /vendor/bin/hw/android.hardware.bluetooth@1.0-service
256 9048K 4108K 1018K 596K /vendor/bin/hw/android.hardware.cas@1.0-service
257 15372K 4372K 1017K 828K /vendor/bin/hw/android.hardware.configstore@1.0-service
267 13464K 4504K 993K 860K /system/bin/healthd
262 12136K 4168K 896K 676K /vendor/bin/hw/android.hardware.light@2.0-service
264 12140K 4168K 870K 668K /vendor/bin/hw/android.hardware.power@1.0-service
233 10444K 3428K 848K 760K /system/bin/servicemanager
235 10244K 3256K 805K 696K /vendor/bin/vndservicemanager
252 12088K 4088K 767K 636K /system/bin/hw/android.hidl.allocator@1.0-service
1983 8932K 3008K 736K 548K /system/bin/sh
274 8932K 2944K 727K 540K /system/bin/sh
268 9180K 3072K 681K 624K /system/bin/lmkd
395 9028K 3004K 662K 600K /system/bin/tombstoned
------ ------ ------
601566K 385996K TOTAL
RAM: 3938668K total, 2765940K free, 54772K buffers, 683920K cached, 117060K shmem, 119748K slab
2.5 showmap
該命令的輸出每一行代表一個虛擬地址區域(vm_area),內容基本和proc/pid/maps基本一致.
# showmap -a 1302
start end virtual shared shared private private
addr addr size RSS PSS clean dirty clean dirty swap swapPSSobject
-------- -------- -------- -------- -------- -------- -------- -------- -------- -------- -------- ------------------------------
0b65b000 0b65c000 4 0 0 0 0 0 0 0 0 [anon:thread stack guard page]
start addr: 虛擬地址的起始地址
end addr: 虛擬地址的結束地址
virutal size: 占用虛擬內存大小
RSS:進程獨占內存 + 共享庫占用內存的物理內存
PSS:進程獨占內存 + 比例分配共享庫占用的物理內存
shared:共享數據
private:私有數據
2.6 vmstat
不僅可以查看內存情況,還可以查看進程運行隊列、系統切換、CPU時間占比等情況.