在调试一些内存相关的问题时,我们通常需要实时打印出当前系统中所使用的内存信息。
下面提供两种方法来查看这些信息。
方式一,使用 adb shell dumpsys meminfo
使用示例:
C:\Users\Administrator>adb shell dumpsys meminfo
Applications Memory Usage (kB):
Uptime: 14868066 Realtime: 21560285
Total PSS by process:
35958 kB: system (pid 463)
30518 kB: com.bbk.launcher2 (pid 824)
26253 kB: com.android.systemui (pid 630)
20983 kB: com.android.settings (pid 7366)
10239 kB: com.android.phone (pid 706)
9986 kB: com.tencent.qqpimsecure (pid 893)
9326 kB: com.iqoo.ime.service (pid 685)
8962 kB: android.process.media (pid 796)
7418 kB: com.sohu.sohuvideo:log_service (pid 6695)
7140 kB: com.ting.mp3.oemc.android:pushservice_v1 (pid 6502)
7074 kB: com.mediatek.bluetooth (pid 716)
7062 kB: com.sina.weibo (pid 2429)
7033 kB: android.process.acore (pid 1150)
6288 kB: com.sina.weibo.servant (pid 5979)
5898 kB: com.eg.android.AlipayGphone (pid 3473)
5435 kB: com.android.ActivityNetwork (pid 6621)
5327 kB: com.tencent.mm (pid 6553)
5281 kB: com.android.bbkmusic (pid 1322)
4559 kB: com.netease.pris:push (pid 7126)
4445 kB: com.android.mms (pid 6463)
4392 kB: com.mediatek.mdlogger (pid 725)
4347 kB: com.baidu.map.location:remote (pid 843)
4275 kB: com.mediatek.mobilelog (pid 7063)
4254 kB: com.android.contacts (pid 857)
3993 kB: com.bbk.appstore (pid 7262)
3854 kB: com.mediatek.MediatekDM (pid 6281)
3667 kB: com.android.keychain (pid 7276)
3623 kB: com.baidu.map.location (pid 784)
3387 kB: com.mediatek.atci.service (pid 7110)
3359 kB: com.android.bbksoundrecorder (pid 746)
3265 kB: com.svox.pico (pid 7332)
3261 kB: com.android.defcontainer (pid 6448)
Total PSS by OOM adjustment:
35958 kB: System
35958 kB: system (pid 463)
63631 kB: Persistent
26253 kB: com.android.systemui (pid 630)
10239 kB: com.android.phone (pid 706)
7074 kB: com.mediatek.bluetooth (pid 716)
7033 kB: android.process.acore (pid 1150)
5281 kB: com.android.bbkmusic (pid 1322)
4392 kB: com.mediatek.mdlogger (pid 725)
3359 kB: com.android.bbksoundrecorder (pid 746)
29945 kB: Foreground
20983 kB: com.android.settings (pid 7366)
8962 kB: android.process.media (pid 796)
27841 kB: Perceptible
9986 kB: com.tencent.qqpimsecure (pid 893)
9326 kB: com.iqoo.ime.service (pid 685)
4275 kB: com.mediatek.mobilelog (pid 7063)
4254 kB: com.android.contacts (pid 857)
14255 kB: A Services
7140 kB: com.ting.mp3.oemc.android:pushservice_v1 (pid 6502)
3854 kB: com.mediatek.MediatekDM (pid 6281)
3261 kB: com.android.defcontainer (pid 6448)
30518 kB: Home
30518 kB: com.bbk.launcher2 (pid 824)
27087 kB: B Services
7418 kB: com.sohu.sohuvideo:log_service (pid 6695)
6288 kB: com.sina.weibo.servant (pid 5979)
5435 kB: com.android.ActivityNetwork (pid 6621)
4559 kB: com.netease.pris:push (pid 7126)
3387 kB: com.mediatek.atci.service (pid 7110)
41627 kB: Background
7062 kB: com.sina.weibo (pid 2429)
5898 kB: com.eg.android.AlipayGphone (pid 3473)
5327 kB: com.tencent.mm (pid 6553)
4445 kB: com.android.mms (pid 6463)
4347 kB: com.baidu.map.location:remote (pid 843)
3993 kB: com.bbk.appstore (pid 7262)
3667 kB: com.android.keychain (pid 7276)
3623 kB: com.baidu.map.location (pid 784)
3265 kB: com.svox.pico (pid 7332)
Total PSS by category:
145990 kB: Dalvik
44471 kB: Unknown
35557 kB: Other dev
24404 kB: .so mmap
14348 kB: .dex mmap
3830 kB: Other mmap
1214 kB: .apk mmap
524 kB: Native
424 kB: .ttf mmap
68 kB: Ashmem
32 kB: Cursor
0 kB: .jar mmap
Total PSS: 270862 kB
KSM: 0 kB saved from shared 0 kB
0 kB unshared; 0 kB volatile
方式二:使用 adb shell procrank 命令
使用示例:
C:\Users\Administrator>adb shell procrank
PID Vss Rss Pss Uss cmdline
463 61772K 61704K 36103K 32180K system_server
824 55208K 55112K 30643K 29204K com.bbk.launcher2
630 54492K 54376K 26423K 21896K com.android.systemui
7366 48596K 48492K 20979K 17016K com.android.settings
112 32696K 29692K 20346K 12720K /system/bin/surfaceflinger
706 32244K 32192K 10338K 9560K com.android.phone
893 32108K 32084K 10126K 9324K com.tencent.qqpimsecure
117 10984K 10952K 9742K 9512K /system/bin/mediaserver
685 31004K 30952K 9420K 8704K com.iqoo.ime.service
796 31516K 31456K 9057K 8224K android.process.media
6695 30332K 30304K 7490K 6488K com.sohu.sohuvideo:log_service
6502 29896K 29868K 7215K 6232K com.ting.mp3.oemc.android:pushservice_v1
716 29564K 29508K 7174K 6332K com.mediatek.bluetooth
2429 29612K 29584K 7157K 6352K com.sina.weibo
1150 29404K 29352K 7127K 6380K android.process.acore
5979 29196K 29168K 6380K 5548K com.sina.weibo.servant
3473 28424K 28396K 5992K 5208K com.eg.android.AlipayGphone
6553 28108K 28068K 5396K 4480K com.tencent.mm
1322 27544K 27488K 5375K 4624K com.android.bbkmusic
6621 27204K 27172K 4781K 3996K com.android.ActivityNetwork
725 26372K 26308K 4620K 3920K com.mediatek.mdlogger
7126 26968K 26912K 4605K 3868K com.netease.pris:push
6463 26996K 26944K 4534K 3788K com.android.mms
843 26172K 26120K 4441K 3648K com.baidu.map.location:remote
857 26024K 25972K 4349K 3648K com.android.contacts
7063 26664K 26600K 4347K 3636K com.mediatek.mobilelog
7262 26988K 26928K 4043K 3296K com.bbk.appstore
6281 26432K 26368K 3948K 3192K com.mediatek.MediatekDM
784 25524K 25460K 3718K 2988K com.baidu.map.location
7276 26624K 26556K 3716K 2960K com.android.keychain
746 25268K 25200K 3450K 2720K com.android.bbksoundrecorder
7110 25944K 25876K 3438K 2720K com.mediatek.atci.service
6448 25772K 25708K 3354K 2596K com.android.defcontainer
7332 25812K 25748K 3311K 2600K com.svox.pico
113 24144K 24064K 3116K 2308K zygote
8327 2544K 2544K 2244K 2224K procrank
116 2932K 2932K 2136K 2112K /system/bin/drmserver
1491 1968K 1968K 1354K 1328K /system/bin/bbk_em_svr
134 1996K 1996K 1278K 1264K /system/bin/em_svr
128 1848K 1844K 1266K 1252K /system/bin/atcid
1813 1468K 1468K 1177K 1172K logcat
7084 1836K 1836K 1090K 1012K /system/bin/atci_service
130 1336K 1336K 845K 828K /system/bin/mtkbt
3399 1020K 1020K 722K 716K logcat
421 1008K 1008K 559K 552K /system/bin/rild
118 1100K 1100K 522K 452K /system/bin/mdpserver
6970 896K 896K 477K 352K /system/bin/netdiag
5649 768K 768K 455K 448K logcat
109 784K 784K 435K 424K /system/bin/netd
7079 988K 988K 425K 384K /system/bin/mobile_log_d
131 868K 868K 402K 392K /system/bin/mdpd
124 760K 760K 397K 392K /system/bin/mtk_agpsd
1 492K 492K 386K 320K /init
101 672K 672K 352K 344K /system/bin/vold
401 1272K 708K 306K 300K /system/bin/mdlogger
106 716K 716K 278K 272K /system/bin/vivo_daemon
123 536K 536K 271K 268K /system/bin/keystore
400 528K 528K 251K 248K /system/bin/gsm0710muxd
8209 288K 288K 248K 248K /sbin/adbd
129 676K 676K 242K 236K /system/bin/dm_agent_binder
105 612K 612K 233K 228K /system/bin/debuggerd
8326 488K 484K 223K 204K /system/bin/sh
136 648K 648K 222K 216K /system/bin/nvram_agent_binder
114 440K 440K 181K 176K /system/bin/hald
7081 748K 748K 177K 136K /system/bin/mobile_log_d
110 592K 592K 177K 52K /system/bin/netdiag
102 524K 432K 171K 168K /system/bin/ccci_fsd
7080 728K 728K 165K 124K /system/bin/mobile_log_d
7083 700K 700K 152K 112K /system/bin/mobile_log_d
7082 708K 708K 151K 108K /system/bin/mobile_log_d
119 416K 416K 151K 148K /system/bin/mtkGD
65 220K 220K 150K 84K /sbin/ueventd
7179 460K 460K 147K 140K logcat
125 400K 400K 143K 140K /system/bin/stp_dump3
120 372K 368K 139K 136K /system/bin/dbus-daemon
122 424K 420K 131K 128K /system/bin/installd
100 324K 324K 118K 116K /system/bin/servicemanager
140 336K 332K 110K 108K /system/bin/sh
103 332K 332K 106K 104K /system/bin/ccci_mdinit
1222 356K 356K 103K 100K /data/data/com.tencent.qqpimsecure/files/athena_v2.dat
104 324K 324K 102K 100K /system/bin/6620_launcher
107 680K 680K 94K 52K /system/bin/mobile_log_d
1238 320K 316K 90K 88K sh
1229 316K 312K 90K 88K sh
1219 316K 312K 90K 88K sh
------ ------ ------
328033K 282552K TOTAL
RAM: 482092K total, 3732K free, 4052K buffers, 110608K cached, 476K shmem, 24624K slab
方式二中能看出当前系统总共有多少内存,有多少空闲,有多少buffer,缓存等信息
当前使用这两种方式的前提是 adb remount 能够成功。
可以通过 adb shell kill PIDNumber 来杀死你想杀死的后台进程来模拟某种 bug 的复现条件。
譬如 adb shell kill 630 就能杀死 com.android.systemui 这个进程,有兴趣的可以尝试一下,盯住手机屏幕看下有什么反应……
adb shell ps 可以查看当前终端中的进程信息
C:\Users\Administrator>adb shell ps
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 644 492 c0140624 0000fa38 S /init
root 2 0 0 0 c0083274 00000000 S kthreadd
root 3 2 0 0 c0069564 00000000 S ksoftirqd/0
root 6 2 0 0 c00c4a58 00000000 S migration/0
root 10 2 0 0 c007e778 00000000 S khelper
root 11 2 0 0 c007e778 00000000 S fs_sync
root 12 2 0 0 c007e778 00000000 S suspend
root 13 2 0 0 c0114cbc 00000000 S sync_supers
root 14 2 0 0 c0115d48 00000000 S bdi-default
root 15 2 0 0 c007e778 00000000 S kblockd
root 16 2 0 0 c02db5a8 00000000 S khubd
root 17 2 0 0 c007e778 00000000 S _mtkfb
root 18 2 0 0 c007e778 00000000 S cfg80211
root 20 2 0 0 c010de00 00000000 S kswapd0
root 21 2 0 0 c016b74c 00000000 S fsnotify_mark
root 22 2 0 0 c007e778 00000000 S crypto
root 42 2 0 0 c0071488 00000000 D binder_watchdog
root 43 2 0 0 c007e778 00000000 S binder
root 44 2 0 0 c007e778 00000000 S trace mon
root 45 2 0 0 c007e778 00000000 S g2d_workqueue
root 46 2 0 0 c007e778 00000000 S mtk_vibrator
root 47 2 0 0 c03ffc84 00000000 D pmic_thread_kth
root 48 2 0 0 c0411648 00000000 D bat_thread_kthr
root 49 2 0 0 c041079c 00000000 S mtk charger_hv_
root 50 2 0 0 c03ace00 00000000 S screen_update_k
root 51 2 0 0 c007e778 00000000 S non_sta
root 52 2 0 0 c007e778 00000000 S accdet
root 53 2 0 0 c007e778 00000000 S accdet_eint
root 54 2 0 0 c029fccc 00000000 S kworker/u:2
root 55 2 0 0 c007e778 00000000 S acc_sensor_eint
root 56 2 0 0 c03489bc 00000000 S mmcqd/0
root 57 2 0 0 c03489bc 00000000 S mmcqd/0boot0
root 58 2 0 0 c03489bc 00000000 S mmcqd/0boot1
root 60 2 0 0 c007e778 00000000 S deferwq
root 61 2 0 0 c007e778 00000000 S f_mtp
root 62 2 0 0 c0301eb0 00000000 S file-storage
root 63 2 0 0 c0071488 00000000 D wdtk-0
root 64 2 0 0 c0071488 00000000 D wdtk-1
root 65 2 0 0 c007e778 00000000 S 0-0038
root 66 1 424 220 c0140624 0000fa38 S /sbin/ueventd
root 68 2 0 0 c020a0e0 00000000 S jbd2/mmcblk0p4-
root 69 2 0 0 c007e778 00000000 S ext4-dio-unwrit
root 70 2 0 0 c02a1ac8 00000000 S loop0
root 71 2 0 0 c015679c 00000000 S flush-179:0
root 74 2 0 0 c020a0e0 00000000 S jbd2/mmcblk0p6-
root 75 2 0 0 c007e778 00000000 S ext4-dio-unwrit
root 80 2 0 0 c020a0e0 00000000 S jbd2/mmcblk0p5-
root 81 2 0 0 c007e778 00000000 S ext4-dio-unwrit
root 84 2 0 0 c020a0e0 00000000 S jbd2/mmcblk0p2-
root 85 2 0 0 c007e778 00000000 S ext4-dio-unwrit
root 86 2 0 0 c007e778 00000000 S pvr_timer
root 87 2 0 0 bf141a5c 00000000 S mtk_stp_psm
root 88 2 0 0 bf141a5c 00000000 S mtk_stp_btm
root 89 2 0 0 bf141868 00000000 S mtk_wmtd
root 90 2 0 0 c007e778 00000000 S fm_timer_wq
root 91 2 0 0 c007e778 00000000 S fm_eint_wq
system 93 1 1104 348 c0366868 400c8f74 S /system/bin/servicemanager
root 94 1 5016 668 ffffffff 400bd700 S /system/bin/vold
ccci 95 1 1400 440 bf091e48 40117f74 S /system/bin/ccci_fsd
system 96 1 1980 356 ffffffff 40128d3c S /system/bin/ccci_mdinit
system 97 1 960 352 c0140624 400a4ec8 S /system/bin/6620_launcher
root 98 1 1792 580 c0140624 4014bec8 S /system/bin/debuggerd
root 99 1 6708 640 ffffffff 40055f74 S /system/bin/vivo_daemon
shell 100 1 2696 596 ffffffff 4011b700 S /system/bin/mobile_log_d
root 102 1 11892 984 ffffffff 400a3700 S /system/bin/netd
radio 103 1 4536 576 ffffffff 401d9084 S /system/bin/netdiag
system 105 1 55108 24708 ffffffff 400e0f74 S /system/bin/surfaceflinger
root 106 1 286264 23048 ffffffff 400dd098 S zygote
system 107 1 3352 500 ffffffff 400e2700 S /system/bin/hald
drm 108 1 21300 2324 ffffffff 40194f74 S /system/bin/drmserver
media 109 1 62376 8236 ffffffff 40123f74 S /system/bin/mediaserver
system 110 1 7964 1128 ffffffff 400fef74 S /system/bin/mdpserver
system 111 1 1196 460 c0444894 400d6a04 S /system/bin/mtkGD
bluetooth 112 1 1644 392 c0140624 40158ec8 S /system/bin/dbus-daemon
root 114 1 1160 444 c04fd670 400edd3c S /system/bin/installd
keystore 115 1 2148 712 c0444894 40076a04 S /system/bin/keystore
gps 116 1 13836 772 ffffffff 4007dec8 S /system/bin/mtk_agpsd
system 117 1 1148 436 c0140624 400f6098 S /system/bin/stp_dump3
root 119 1 17608 1476 ffffffff 4016e700 S /system/bin/atci_service
root 120 1 10588 1696 c0140624 40113098 S /system/bin/atcid
system 121 1 4740 620 ffffffff 4007af74 S /system/bin/dm_agent_binder
bluetooth 122 1 8224 1212 ffffffff 40234098 S /system/bin/mtkbt
system 123 1 3864 740 c00562e8 40118f74 S /system/bin/mdpd
root 126 1 1648 560 c00881a8 4009c700 S /system/bin/hscdtd007a
root 127 1 15312 1792 ffffffff 4004d700 S /system/bin/em_svr
system 128 1 4680 580 ffffffff 40150f74 S /system/bin/nvram_agent_binder
root 131 1 1028 352 c0288208 40086d3c S /system/bin/sh
root 134 2 0 0 c007e778 00000000 S pvr_workqueue
root 328 2 0 0 c007e778 00000000 S mtklfb
radio 347 1 13476 556 ffffffff 400e1700 S /system/bin/gsm0710muxd
shell 348 1 5148 660 ffffffff 40144d3c S /system/bin/mdlogger
radio 371 1 26488 944 ffffffff 4009a700 S /system/bin/rild
system 398 106 445644 58800 ffffffff 400dcf74 S system_server
root 482 2 0 0 c0344c0c 00000000 S ksdioirqd/mmc2
system 489 106 371684 52680 ffffffff 400dde50 S com.android.systemui
root 535 2 0 0 bf2124c8 00000000 S tx_thread
log 536 1 952 348 c0288208 40102d3c S /system/bin/logwrapper
wifi 538 536 3108 1112 c0140624 400f5098 S /system/bin/wpa_supplicant
radio 558 106 332432 33844 ffffffff 400dde50 S com.android.phone
u0_a51 564 106 299024 26940 ffffffff 400dde50 S com.vivo.motionrecognition
u0_a53 576 106 312772 29008 ffffffff 400dde50 S com.mediatek.bluetooth
system 585 1 1152 476 c016f01c 4008ae50 S /system/xbin/mnld
radio 587 106 296552 26788 ffffffff 400dde50 S com.mediatek.mdlogger
那么如何在代码中判断当前的硬件系统有多少的 RAM 呢?
使用下面代码可以奏效:
在 Framework ProcessList.java 中有如下代码可用:
ProcessList() {
MemInfoReader minfo = new MemInfoReader();
minfo.readMemInfo();
mTotalMemMb = minfo.getTotalSize()/(1024*1024);
}
这个可以用于 app 层来适配不同内存配置的机型。