adb 打印終端中內存使用情況的方法

在調試一些內存相關的問題時,我們通常需要實時打印出當前系統中所使用的內存信息。

下面提供兩種方法來查看這些信息。

方式一,使用 adb shell dumpsys meminfo

使用示例:

[java] view plaincopy
  1. C:\Users\Administrator>adb shell dumpsys meminfo  
  2. Applications Memory Usage (kB):  
  3. Uptime: 14868066 Realtime: 21560285  
  4.   
  5. Total PSS by process:  
  6.     35958 kB: system (pid 463)  
  7.     30518 kB: com.bbk.launcher2 (pid 824)  
  8.     26253 kB: com.android.systemui (pid 630)  
  9.     20983 kB: com.android.settings (pid 7366)  
  10.     10239 kB: com.android.phone (pid 706)  
  11.      9986 kB: com.tencent.qqpimsecure (pid 893)  
  12.      9326 kB: com.iqoo.ime.service (pid 685)  
  13.      8962 kB: android.process.media (pid 796)  
  14.      7418 kB: com.sohu.sohuvideo:log_service (pid 6695)  
  15.      7140 kB: com.ting.mp3.oemc.android:pushservice_v1 (pid 6502)  
  16.      7074 kB: com.mediatek.bluetooth (pid 716)  
  17.      7062 kB: com.sina.weibo (pid 2429)  
  18.      7033 kB: android.process.acore (pid 1150)  
  19.      6288 kB: com.sina.weibo.servant (pid 5979)  
  20.      5898 kB: com.eg.android.AlipayGphone (pid 3473)  
  21.      5435 kB: com.android.ActivityNetwork (pid 6621)  
  22.      5327 kB: com.tencent.mm (pid 6553)  
  23.      5281 kB: com.android.bbkmusic (pid 1322)  
  24.      4559 kB: com.netease.pris:push (pid 7126)  
  25.      4445 kB: com.android.mms (pid 6463)  
  26.      4392 kB: com.mediatek.mdlogger (pid 725)  
  27.      4347 kB: com.baidu.map.location:remote (pid 843)  
  28.      4275 kB: com.mediatek.mobilelog (pid 7063)  
  29.      4254 kB: com.android.contacts (pid 857)  
  30.      3993 kB: com.bbk.appstore (pid 7262)  
  31.      3854 kB: com.mediatek.MediatekDM (pid 6281)  
  32.      3667 kB: com.android.keychain (pid 7276)  
  33.      3623 kB: com.baidu.map.location (pid 784)  
  34.      3387 kB: com.mediatek.atci.service (pid 7110)  
  35.      3359 kB: com.android.bbksoundrecorder (pid 746)  
  36.      3265 kB: com.svox.pico (pid 7332)  
  37.      3261 kB: com.android.defcontainer (pid 6448)  
  38.   
  39. Total PSS by OOM adjustment:  
  40.     35958 kB: System  
  41.                35958 kB: system (pid 463)  
  42.     63631 kB: Persistent  
  43.                26253 kB: com.android.systemui (pid 630)  
  44.                10239 kB: com.android.phone (pid 706)  
  45.                 7074 kB: com.mediatek.bluetooth (pid 716)  
  46.                 7033 kB: android.process.acore (pid 1150)  
  47.                 5281 kB: com.android.bbkmusic (pid 1322)  
  48.                 4392 kB: com.mediatek.mdlogger (pid 725)  
  49.                 3359 kB: com.android.bbksoundrecorder (pid 746)  
  50.     29945 kB: Foreground  
  51.                20983 kB: com.android.settings (pid 7366)  
  52.                 8962 kB: android.process.media (pid 796)  
  53.     27841 kB: Perceptible  
  54.                 9986 kB: com.tencent.qqpimsecure (pid 893)  
  55.                 9326 kB: com.iqoo.ime.service (pid 685)  
  56.                 4275 kB: com.mediatek.mobilelog (pid 7063)  
  57.                 4254 kB: com.android.contacts (pid 857)  
  58.     14255 kB: A Services  
  59.                 7140 kB: com.ting.mp3.oemc.android:pushservice_v1 (pid 6502)  
  60.                 3854 kB: com.mediatek.MediatekDM (pid 6281)  
  61.                 3261 kB: com.android.defcontainer (pid 6448)  
  62.     30518 kB: Home  
  63.                30518 kB: com.bbk.launcher2 (pid 824)  
  64.     27087 kB: B Services  
  65.                 7418 kB: com.sohu.sohuvideo:log_service (pid 6695)  
  66.                 6288 kB: com.sina.weibo.servant (pid 5979)  
  67.                 5435 kB: com.android.ActivityNetwork (pid 6621)  
  68.                 4559 kB: com.netease.pris:push (pid 7126)  
  69.                 3387 kB: com.mediatek.atci.service (pid 7110)  
  70.     41627 kB: Background  
  71.                 7062 kB: com.sina.weibo (pid 2429)  
  72.                 5898 kB: com.eg.android.AlipayGphone (pid 3473)  
  73.                 5327 kB: com.tencent.mm (pid 6553)  
  74.                 4445 kB: com.android.mms (pid 6463)  
  75.                 4347 kB: com.baidu.map.location:remote (pid 843)  
  76.                 3993 kB: com.bbk.appstore (pid 7262)  
  77.                 3667 kB: com.android.keychain (pid 7276)  
  78.                 3623 kB: com.baidu.map.location (pid 784)  
  79.                 3265 kB: com.svox.pico (pid 7332)  
  80.   
  81. Total PSS by category:  
  82.    145990 kB: Dalvik  
  83.     44471 kB: Unknown  
  84.     35557 kB: Other dev  
  85.     24404 kB: .so mmap  
  86.     14348 kB: .dex mmap  
  87.      3830 kB: Other mmap  
  88.      1214 kB: .apk mmap  
  89.       524 kB: Native  
  90.       424 kB: .ttf mmap  
  91.        68 kB: Ashmem  
  92.        32 kB: Cursor  
  93.         0 kB: .jar mmap  
  94.   
  95. Total PSS: 270862 kB  
  96.       KSM: 0 kB saved from shared 0 kB  
  97.            0 kB unshared; 0 kB volatile  

方式二:使用 adb shell procrank 命令

使用示例:

[java] view plaincopy
  1. C:\Users\Administrator>adb shell procrank  
  2.   PID      Vss      Rss      Pss      Uss  cmdline  
  3.   463   61772K   61704K   36103K   32180K  system_server  
  4.   824   55208K   55112K   30643K   29204K  com.bbk.launcher2  
  5.   630   54492K   54376K   26423K   21896K  com.android.systemui  
  6.  7366   48596K   48492K   20979K   17016K  com.android.settings  
  7.   112   32696K   29692K   20346K   12720K  /system/bin/surfaceflinger  
  8.   706   32244K   32192K   10338K    9560K  com.android.phone  
  9.   893   32108K   32084K   10126K    9324K  com.tencent.qqpimsecure  
  10.   117   10984K   10952K    9742K    9512K  /system/bin/mediaserver  
  11.   685   31004K   30952K    9420K    8704K  com.iqoo.ime.service  
  12.   796   31516K   31456K    9057K    8224K  android.process.media  
  13.  6695   30332K   30304K    7490K    6488K  com.sohu.sohuvideo:log_service  
  14.  6502   29896K   29868K    7215K    6232K  com.ting.mp3.oemc.android:pushservice_v1  
  15.   716   29564K   29508K    7174K    6332K  com.mediatek.bluetooth  
  16.  2429   29612K   29584K    7157K    6352K  com.sina.weibo  
  17.  1150   29404K   29352K    7127K    6380K  android.process.acore  
  18.  5979   29196K   29168K    6380K    5548K  com.sina.weibo.servant  
  19.  3473   28424K   28396K    5992K    5208K  com.eg.android.AlipayGphone  
  20.  6553   28108K   28068K    5396K    4480K  com.tencent.mm  
  21.  1322   27544K   27488K    5375K    4624K  com.android.bbkmusic  
  22.  6621   27204K   27172K    4781K    3996K  com.android.ActivityNetwork  
  23.   725   26372K   26308K    4620K    3920K  com.mediatek.mdlogger  
  24.  7126   26968K   26912K    4605K    3868K  com.netease.pris:push  
  25.  6463   26996K   26944K    4534K    3788K  com.android.mms  
  26.   843   26172K   26120K    4441K    3648K  com.baidu.map.location:remote  
  27.   857   26024K   25972K    4349K    3648K  com.android.contacts  
  28.  7063   26664K   26600K    4347K    3636K  com.mediatek.mobilelog  
  29.  7262   26988K   26928K    4043K    3296K  com.bbk.appstore  
  30.  6281   26432K   26368K    3948K    3192K  com.mediatek.MediatekDM  
  31.   784   25524K   25460K    3718K    2988K  com.baidu.map.location  
  32.  7276   26624K   26556K    3716K    2960K  com.android.keychain  
  33.   746   25268K   25200K    3450K    2720K  com.android.bbksoundrecorder  
  34.  7110   25944K   25876K    3438K    2720K  com.mediatek.atci.service  
  35.  6448   25772K   25708K    3354K    2596K  com.android.defcontainer  
  36.  7332   25812K   25748K    3311K    2600K  com.svox.pico  
  37.   113   24144K   24064K    3116K    2308K  zygote  
  38.  8327    2544K    2544K    2244K    2224K  procrank  
  39.   116    2932K    2932K    2136K    2112K  /system/bin/drmserver  
  40.  1491    1968K    1968K    1354K    1328K  /system/bin/bbk_em_svr  
  41.   134    1996K    1996K    1278K    1264K  /system/bin/em_svr  
  42.   128    1848K    1844K    1266K    1252K  /system/bin/atcid  
  43.  1813    1468K    1468K    1177K    1172K  logcat  
  44.  7084    1836K    1836K    1090K    1012K  /system/bin/atci_service  
  45.   130    1336K    1336K     845K     828K  /system/bin/mtkbt  
  46.  3399    1020K    1020K     722K     716K  logcat  
  47.   421    1008K    1008K     559K     552K  /system/bin/rild  
  48.   118    1100K    1100K     522K     452K  /system/bin/mdpserver  
  49.  6970     896K     896K     477K     352K  /system/bin/netdiag  
  50.  5649     768K     768K     455K     448K  logcat  
  51.   109     784K     784K     435K     424K  /system/bin/netd  
  52.  7079     988K     988K     425K     384K  /system/bin/mobile_log_d  
  53.   131     868K     868K     402K     392K  /system/bin/mdpd  
  54.   124     760K     760K     397K     392K  /system/bin/mtk_agpsd  
  55.     1     492K     492K     386K     320K  /init  
  56.   101     672K     672K     352K     344K  /system/bin/vold  
  57.   401    1272K     708K     306K     300K  /system/bin/mdlogger  
  58.   106     716K     716K     278K     272K  /system/bin/vivo_daemon  
  59.   123     536K     536K     271K     268K  /system/bin/keystore  
  60.   400     528K     528K     251K     248K  /system/bin/gsm0710muxd  
  61.  8209     288K     288K     248K     248K  /sbin/adbd  
  62.   129     676K     676K     242K     236K  /system/bin/dm_agent_binder  
  63.   105     612K     612K     233K     228K  /system/bin/debuggerd  
  64.  8326     488K     484K     223K     204K  /system/bin/sh  
  65.   136     648K     648K     222K     216K  /system/bin/nvram_agent_binder  
  66.   114     440K     440K     181K     176K  /system/bin/hald  
  67.  7081     748K     748K     177K     136K  /system/bin/mobile_log_d  
  68.   110     592K     592K     177K      52K  /system/bin/netdiag  
  69.   102     524K     432K     171K     168K  /system/bin/ccci_fsd  
  70.  7080     728K     728K     165K     124K  /system/bin/mobile_log_d  
  71.  7083     700K     700K     152K     112K  /system/bin/mobile_log_d  
  72.  7082     708K     708K     151K     108K  /system/bin/mobile_log_d  
  73.   119     416K     416K     151K     148K  /system/bin/mtkGD  
  74.    65     220K     220K     150K      84K  /sbin/ueventd  
  75.  7179     460K     460K     147K     140K  logcat  
  76.   125     400K     400K     143K     140K  /system/bin/stp_dump3  
  77.   120     372K     368K     139K     136K  /system/bin/dbus-daemon  
  78.   122     424K     420K     131K     128K  /system/bin/installd  
  79.   100     324K     324K     118K     116K  /system/bin/servicemanager  
  80.   140     336K     332K     110K     108K  /system/bin/sh  
  81.   103     332K     332K     106K     104K  /system/bin/ccci_mdinit  
  82.  1222     356K     356K     103K     100K  /data/data/com.tencent.qqpimsecure/files/athena_v2.dat  
  83.   104     324K     324K     102K     100K  /system/bin/6620_launcher  
  84.   107     680K     680K      94K      52K  /system/bin/mobile_log_d  
  85.  1238     320K     316K      90K      88K  sh  
  86.  1229     316K     312K      90K      88K  sh  
  87.  1219     316K     312K      90K      88K  sh  
  88.                           ------   ------  ------  
  89.                          328033K  282552K  TOTAL  
  90.   
  91. 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 可以查看當前終端中的進程信息

[java] view plaincopy
  1. C:\Users\Administrator>adb shell ps  
  2. USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME  
  3. root      1     0     644    492   c0140624 0000fa38 S /init  
  4. root      2     0     0      0     c0083274 00000000 S kthreadd  
  5. root      3     2     0      0     c0069564 00000000 S ksoftirqd/0  
  6. root      6     2     0      0     c00c4a58 00000000 S migration/0  
  7. root      10    2     0      0     c007e778 00000000 S khelper  
  8. root      11    2     0      0     c007e778 00000000 S fs_sync  
  9. root      12    2     0      0     c007e778 00000000 S suspend  
  10. root      13    2     0      0     c0114cbc 00000000 S sync_supers  
  11. root      14    2     0      0     c0115d48 00000000 S bdi-default  
  12. root      15    2     0      0     c007e778 00000000 S kblockd  
  13. root      16    2     0      0     c02db5a8 00000000 S khubd  
  14. root      17    2     0      0     c007e778 00000000 S _mtkfb  
  15. root      18    2     0      0     c007e778 00000000 S cfg80211  
  16. root      20    2     0      0     c010de00 00000000 S kswapd0  
  17. root      21    2     0      0     c016b74c 00000000 S fsnotify_mark  
  18. root      22    2     0      0     c007e778 00000000 S crypto  
  19. root      42    2     0      0     c0071488 00000000 D binder_watchdog  
  20. root      43    2     0      0     c007e778 00000000 S binder  
  21. root      44    2     0      0     c007e778 00000000 S trace mon  
  22. root      45    2     0      0     c007e778 00000000 S g2d_workqueue  
  23. root      46    2     0      0     c007e778 00000000 S mtk_vibrator  
  24. root      47    2     0      0     c03ffc84 00000000 D pmic_thread_kth  
  25. root      48    2     0      0     c0411648 00000000 D bat_thread_kthr  
  26. root      49    2     0      0     c041079c 00000000 S mtk charger_hv_  
  27. root      50    2     0      0     c03ace00 00000000 S screen_update_k  
  28. root      51    2     0      0     c007e778 00000000 S non_sta  
  29. root      52    2     0      0     c007e778 00000000 S accdet  
  30. root      53    2     0      0     c007e778 00000000 S accdet_eint  
  31. root      54    2     0      0     c029fccc 00000000 S kworker/u:2  
  32. root      55    2     0      0     c007e778 00000000 S acc_sensor_eint  
  33. root      56    2     0      0     c03489bc 00000000 S mmcqd/0  
  34. root      57    2     0      0     c03489bc 00000000 S mmcqd/0boot0  
  35. root      58    2     0      0     c03489bc 00000000 S mmcqd/0boot1  
  36. root      60    2     0      0     c007e778 00000000 S deferwq  
  37. root      61    2     0      0     c007e778 00000000 S f_mtp  
  38. root      62    2     0      0     c0301eb0 00000000 S file-storage  
  39. root      63    2     0      0     c0071488 00000000 D wdtk-0  
  40. root      64    2     0      0     c0071488 00000000 D wdtk-1  
  41. root      65    2     0      0     c007e778 00000000 S 0-0038  
  42. root      66    1     424    220   c0140624 0000fa38 S /sbin/ueventd  
  43. root      68    2     0      0     c020a0e0 00000000 S jbd2/mmcblk0p4-  
  44. root      69    2     0      0     c007e778 00000000 S ext4-dio-unwrit  
  45. root      70    2     0      0     c02a1ac8 00000000 S loop0  
  46. root      71    2     0      0     c015679c 00000000 S flush-179:0  
  47. root      74    2     0      0     c020a0e0 00000000 S jbd2/mmcblk0p6-  
  48. root      75    2     0      0     c007e778 00000000 S ext4-dio-unwrit  
  49. root      80    2     0      0     c020a0e0 00000000 S jbd2/mmcblk0p5-  
  50. root      81    2     0      0     c007e778 00000000 S ext4-dio-unwrit  
  51. root      84    2     0      0     c020a0e0 00000000 S jbd2/mmcblk0p2-  
  52. root      85    2     0      0     c007e778 00000000 S ext4-dio-unwrit  
  53. root      86    2     0      0     c007e778 00000000 S pvr_timer  
  54. root      87    2     0      0     bf141a5c 00000000 S mtk_stp_psm  
  55. root      88    2     0      0     bf141a5c 00000000 S mtk_stp_btm  
  56. root      89    2     0      0     bf141868 00000000 S mtk_wmtd  
  57. root      90    2     0      0     c007e778 00000000 S fm_timer_wq  
  58. root      91    2     0      0     c007e778 00000000 S fm_eint_wq  
  59. system    93    1     1104   348   c0366868 400c8f74 S /system/bin/servicemanager  
  60. root      94    1     5016   668   ffffffff 400bd700 S /system/bin/vold  
  61. ccci      95    1     1400   440   bf091e48 40117f74 S /system/bin/ccci_fsd  
  62. system    96    1     1980   356   ffffffff 40128d3c S /system/bin/ccci_mdinit  
  63. system    97    1     960    352   c0140624 400a4ec8 S /system/bin/6620_launcher  
  64. root      98    1     1792   580   c0140624 4014bec8 S /system/bin/debuggerd  
  65. root      99    1     6708   640   ffffffff 40055f74 S /system/bin/vivo_daemon  
  66. shell     100   1     2696   596   ffffffff 4011b700 S /system/bin/mobile_log_d  
  67. root      102   1     11892  984   ffffffff 400a3700 S /system/bin/netd  
  68. radio     103   1     4536   576   ffffffff 401d9084 S /system/bin/netdiag  
  69. system    105   1     55108  24708 ffffffff 400e0f74 S /system/bin/surfaceflinger  
  70. root      106   1     286264 23048 ffffffff 400dd098 S zygote  
  71. system    107   1     3352   500   ffffffff 400e2700 S /system/bin/hald  
  72. drm       108   1     21300  2324  ffffffff 40194f74 S /system/bin/drmserver  
  73. media     109   1     62376  8236  ffffffff 40123f74 S /system/bin/mediaserver  
  74. system    110   1     7964   1128  ffffffff 400fef74 S /system/bin/mdpserver  
  75. system    111   1     1196   460   c0444894 400d6a04 S /system/bin/mtkGD  
  76. bluetooth 112   1     1644   392   c0140624 40158ec8 S /system/bin/dbus-daemon  
  77. root      114   1     1160   444   c04fd670 400edd3c S /system/bin/installd  
  78. keystore  115   1     2148   712   c0444894 40076a04 S /system/bin/keystore  
  79. gps       116   1     13836  772   ffffffff 4007dec8 S /system/bin/mtk_agpsd  
  80. system    117   1     1148   436   c0140624 400f6098 S /system/bin/stp_dump3  
  81. root      119   1     17608  1476  ffffffff 4016e700 S /system/bin/atci_service  
  82. root      120   1     10588  1696  c0140624 40113098 S /system/bin/atcid  
  83. system    121   1     4740   620   ffffffff 4007af74 S /system/bin/dm_agent_binder  
  84. bluetooth 122   1     8224   1212  ffffffff 40234098 S /system/bin/mtkbt  
  85. system    123   1     3864   740   c00562e8 40118f74 S /system/bin/mdpd  
  86. root      126   1     1648   560   c00881a8 4009c700 S /system/bin/hscdtd007a  
  87. root      127   1     15312  1792  ffffffff 4004d700 S /system/bin/em_svr  
  88. system    128   1     4680   580   ffffffff 40150f74 S /system/bin/nvram_agent_binder  
  89. root      131   1     1028   352   c0288208 40086d3c S /system/bin/sh  
  90. root      134   2     0      0     c007e778 00000000 S pvr_workqueue  
  91. root      328   2     0      0     c007e778 00000000 S mtklfb  
  92. radio     347   1     13476  556   ffffffff 400e1700 S /system/bin/gsm0710muxd  
  93. shell     348   1     5148   660   ffffffff 40144d3c S /system/bin/mdlogger  
  94. radio     371   1     26488  944   ffffffff 4009a700 S /system/bin/rild  
  95. system    398   106   445644 58800 ffffffff 400dcf74 S system_server  
  96. root      482   2     0      0     c0344c0c 00000000 S ksdioirqd/mmc2  
  97. system    489   106   371684 52680 ffffffff 400dde50 S com.android.systemui  
  98. root      535   2     0      0     bf2124c8 00000000 S tx_thread  
  99. log       536   1     952    348   c0288208 40102d3c S /system/bin/logwrapper  
  100. wifi      538   536   3108   1112  c0140624 400f5098 S /system/bin/wpa_supplicant  
  101. radio     558   106   332432 33844 ffffffff 400dde50 S com.android.phone  
  102. u0_a51    564   106   299024 26940 ffffffff 400dde50 S com.vivo.motionrecognition  
  103. u0_a53    576   106   312772 29008 ffffffff 400dde50 S com.mediatek.bluetooth  
  104. system    585   1     1152   476   c016f01c 4008ae50 S /system/xbin/mnld  
  105. radio     587   106   296552 26788 ffffffff 400dde50 S com.mediatek.mdlogger  


那麼如何在代碼中判斷當前的硬件系統有多少的 RAM 呢?

使用下面代碼可以奏效:

在 Framework  ProcessList.java 中有如下代碼可用:

[java] view plaincopy
  1. ProcessList() {  
  2.    MemInfoReader minfo = new MemInfoReader();  
  3.    minfo.readMemInfo();  
  4.    mTotalMemMb = minfo.getTotalSize()/(1024*1024);  
  5. }        

這個可以用於 app 層來適配不同內存配置的機型。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章