美文网首页
Android内存的相关排查方法

Android内存的相关排查方法

作者: 五道杠小学生 | 来源:发表于2017-12-14 23:47 被阅读0次

一、dumpsys meminfo命令

dumpsys meminfo 是Android系统提供的查询内存命令,用该命令可以看到:
每个进程占用的物理内存大小
系统内存分布状态,包括
总的可用物理内存 Total RAM
当前可用物理内存 Free RAM
已用物理内存 Used RAM
不可见内存 Lost RAM

比如输入该命令后输出如下日志:

130|root@MR820:# dumpsys meminfo
Applications Memory Usage (kB):
Uptime: 698643 Realtime: 698643
Total PSS by process:
    23437 kB: system (pid 1980)
    21293 kB: com.android.systemui (pid 2139)
    19164 kB: com.android.settings (pid 2660 / activities)
    18460 kB: im.yixin.tv (pid 2300)
    18358 kB: com.fiberhome.iptv (pid 2356 / activities)
    17374 kB: com.example.wd.mylauncher (pid 3035 / activities)
    14480 kB: com.android.smart.tv.cloud189 (pid 2618)
    12488 kB: zygote (pid 1409)
     8324 kB: com.egame.tv (pid 2769)
     7637 kB: com.fiberhome.fhnotice (pid 3065 / activities)
     7215 kB: com.yx.itvhelpself (pid 2883 / activities)
     7083 kB: im.yixin.tv:core (pid 2494)
     7068 kB: com.fh.tr069 (pid 2397)
     6778 kB: com.fiberhome.factoryTest (pid 2371)
     6418 kB: android.process.media (pid 2513)
     6223 kB: com.android.smart.tv.cloud189:upnp (pid 2639)
     6169 kB: android.process.acore (pid 2475)
     5707 kB: com.fiberhome.fhupgrade (pid 2384)
     5101 kB: com.hisilicon.android.hiRMService (pid 2336)
     4804 kB: com.hisilicon.android.inputmethod.remote (pid 2276)
     4576 kB: mediaserver (pid 1411)
     4289 kB: com.android.gallery3d (pid 2977)
     4288 kB: com.hisilicon.dlna.dmr (pid 2322)
     4281 kB: com.android.bluetooth (pid 2714)
     4252 kB: com.hisilicon.dlna.dms (pid 2314)
     4076 kB: tel.gateway (pid 2602)
     3899 kB: com.hisilicon.android.music (pid 2899)
     2888 kB: com.android.keychain (pid 2992)
     2880 kB: com.fh.iptablespatch (pid 2583)
     2860 kB: com.android.defcontainer (pid 2927)
     2855 kB: com.android.musicfx (pid 2954)
     2748 kB: com.android.printspooler (pid 2462)
     2724 kB: com.example.neterrorcodemanagement (pid 2850)
     2506 kB: com.svox.pico (pid 3010)
     2439 kB: com.FHSqmLoader (pid 2411)
     2341 kB: com.hisilicon.uvmos.monitor (pid 2699)
     1540 kB: surfaceflinger (pid 1408)
     1242 kB: hisysmanager (pid 1419)
     1169 kB: drmserver (pid 1410)
     1080 kB: hiaoservice (pid 1417)
      971 kB: sample_ipcfg (pid 1555)
      681 kB: android_ir_user (pid 1425)
      593 kB: netd (pid 1406)
      588 kB: vold (pid 1405)
      550 kB: displaysetting (pid 1421)
      542 kB: frontPanel (pid 1416)
      475 kB: httpd (pid 1583)
      431 kB: keystore (pid 1413)
      421 kB: hikaraokeservice (pid 1420)
      370 kB: netshared (pid 1418)
      345 kB: /init (pid 1)
      304 kB: udhcpc (pid 3726)
      301 kB: configserver (pid 1422)
      297 kB: upgradeserver (pid 1415)
      204 kB: sh (pid 1426)
      203 kB: installd (pid 1412)
      172 kB: sh (pid 3707)
      172 kB: dumpsys (pid 3731)
      167 kB: httpd (pid 1603)
      164 kB: ueventd (pid 1061)
      162 kB: httpd (pid 1604)
      162 kB: httpd (pid 1606)
      162 kB: httpd (pid 1609)
      162 kB: httpd (pid 1610)
      150 kB: debuggerd (pid 1407)
      136 kB: healthd (pid 1403)
      115 kB: servicemanager (pid 1404)

Total PSS by OOM adjustment:
    31095 kB: Native
               12488 kB: zygote (pid 1409)
                4576 kB: mediaserver (pid 1411)
                1540 kB: surfaceflinger (pid 1408)
                1242 kB: hisysmanager (pid 1419)
                1169 kB: drmserver (pid 1410)
                1080 kB: hiaoservice (pid 1417)
                 971 kB: sample_ipcfg (pid 1555)
                 681 kB: android_ir_user (pid 1425)
                 593 kB: netd (pid 1406)
                 588 kB: vold (pid 1405)
                 550 kB: displaysetting (pid 1421)
                 542 kB: frontPanel (pid 1416)
                 475 kB: httpd (pid 1583)
                 431 kB: keystore (pid 1413)
                 421 kB: hikaraokeservice (pid 1420)
                 370 kB: netshared (pid 1418)
                 345 kB: /init (pid 1)
                 304 kB: udhcpc (pid 3726)
                 301 kB: configserver (pid 1422)
                 297 kB: upgradeserver (pid 1415)
                 204 kB: sh (pid 1426)
                 203 kB: installd (pid 1412)
                 172 kB: sh (pid 3707)
                 172 kB: dumpsys (pid 3731)
                 167 kB: httpd (pid 1603)
                 164 kB: ueventd (pid 1061)
                 162 kB: httpd (pid 1604)
                 162 kB: httpd (pid 1606)
                 162 kB: httpd (pid 1609)
                 162 kB: httpd (pid 1610)
                 150 kB: debuggerd (pid 1407)
                 136 kB: healthd (pid 1403)
                 115 kB: servicemanager (pid 1404)
    23437 kB: System
               23437 kB: system (pid 1980)
    98548 kB: Persistent
               21293 kB: com.android.systemui (pid 2139)
               18460 kB: im.yixin.tv (pid 2300)
               18358 kB: com.fiberhome.iptv (pid 2356 / activities)
                7068 kB: com.fh.tr069 (pid 2397)
                6778 kB: com.fiberhome.factoryTest (pid 2371)
                5707 kB: com.fiberhome.fhupgrade (pid 2384)
                5101 kB: com.hisilicon.android.hiRMService (pid 2336)
                4804 kB: com.hisilicon.android.inputmethod.remote (pid 2276)
                4288 kB: com.hisilicon.dlna.dmr (pid 2322)
                4252 kB: com.hisilicon.dlna.dms (pid 2314)
                2439 kB: com.FHSqmLoader (pid 2411)
     7637 kB: Foreground
                7637 kB: com.fiberhome.fhnotice (pid 3065 / activities)
    18579 kB: Visible
                7215 kB: com.yx.itvhelpself (pid 2883 / activities)
                7083 kB: im.yixin.tv:core (pid 2494)
                4281 kB: com.android.bluetooth (pid 2714)
     4076 kB: A Services
                4076 kB: tel.gateway (pid 2602)
     2724 kB: B Services
                2724 kB: com.example.neterrorcodemanagement (pid 2850)
   105418 kB: Cached
               19164 kB: com.android.settings (pid 2660 / activities)
               17374 kB: com.example.wd.mylauncher (pid 3035 / activities)
               14480 kB: com.android.smart.tv.cloud189 (pid 2618)
                8324 kB: com.egame.tv (pid 2769)
                6418 kB: android.process.media (pid 2513)
                6223 kB: com.android.smart.tv.cloud189:upnp (pid 2639)
                6169 kB: android.process.acore (pid 2475)
                4289 kB: com.android.gallery3d (pid 2977)
                3899 kB: com.hisilicon.android.music (pid 2899)
                2888 kB: com.android.keychain (pid 2992)
                2880 kB: com.fh.iptablespatch (pid 2583)
                2860 kB: com.android.defcontainer (pid 2927)
                2855 kB: com.android.musicfx (pid 2954)
                2748 kB: com.android.printspooler (pid 2462)
                2506 kB: com.svox.pico (pid 3010)
                2341 kB: com.hisilicon.uvmos.monitor (pid 2699)

Total PSS by category:
   118323 kB: Dalvik
    45865 kB: .so mmap
    40515 kB: Dalvik Other
    34678 kB: Unknown
    33415 kB: .dex mmap
     6279 kB: .apk mmap
     4224 kB: Stack
     3530 kB: Other mmap
     2039 kB: Native
     1986 kB: .ttf mmap
      340 kB: Other dev
      164 kB: .jar mmap
      152 kB: Ashmem
        4 kB: Cursor
        0 kB: code mmap
        0 kB: image mmap
        0 kB: Graphics
        0 kB: GL
        0 kB: Memtrack

Total RAM: 1015868 kB
 Free RAM: 638914 kB (105418 cached pss + 180168 cached + 353328 free)
 Used RAM: 211428 kB (186096 used pss + 8008 buffers + 520 shmem + 16804 slab)
 Lost RAM: 165526 kB
   Tuning: 96 (large 256), oom 20480 kB, restore limit 6826 kB (high-end-gfx)

该命令打印的最后一部分,反应系统级别的内存状况:
Total RAM: 1015868 kB
Free RAM: 638914 kB (105418 cached pss + 180168 cached + 353328 free)
Used RAM: 211428 kB (186096 used pss + 8008 buffers + 520 shmem + 16804 slab)
Lost RAM: 165526 kB
Tuning: 96 (large 256), oom 20480 kB, restore limit 6826 kB (high-end-gfx)
里面某些字段的意义需要注意:
dumpsys meminfo

  1. Lost = driver reserve + GPU + ION
  2. Totol = Physic mem – MMZ(40M+) - kernel reserve(30M+)
  3. 实际可用内存 = Free

二、LowMemoryKiller和OOM Killer机制

OOM Killer(Out Of Memory Killer) 是Linux当中,内存保护机制的一种。当物理内存几乎耗尽而又需要分配新内存时,会杀掉一些优先级低的进程,释放内存。
LowMemoryKiller 是Android的内存保护机制。当物理内存低于阈值,就会杀掉一些优先级低的进程,释放内存。

联系:LowMemoryKiller 用到了 OOM Killer 的评分机制
区别:LowMemoryKiller 是通过阈值触发,OOM Killer 是分配内存失败时触发

评分原理:
oom_adj,代表进程的优先级, 数值越大,优先级越低,越容易被杀。系统分16个级别(取值范围[-16, 15]整数,不连续)
通过 cat /proc/xxx/oom_adj 查看,其中xxx是进程号
oom_score_adj: 在 oom_adj 基础上的评分,取值范围[-1000, 1000]
通过 cat /proc/xxx/oom_score_adj 查看,其中xxx是进程号

阈值查看,以98mv100为例:
cat /sys/module/lowmemorykiller/parameters/minfree
1024,1536,2048,3072,3584,4096
cat /sys/module/lowmemorykiller/parameters/adj
0,58,117,176,529,1000
上诉数值表示:可用内存低于 40964K 时,杀掉 oom_score_adj>=1000 的应用;可用内存低于 35844K 时,杀掉 oom_score_adj>=529 的应用,以此类推。

因此,客户可以通过调整 minfree 的阈值来触发 LowMemoryKiller 更频繁地杀应用,从而为高优先级应用省下内存。

在 dumpsys meminfo 中,GPU内存被统计到了 Lost RAM 里面了。

因此,当应用占用GPU内存过高时,不会体现在 Used RAM 里面,而是体现在 Lost RAM 中。反过来,如果发现有问题的时候 Lost RAM 很高,就需要看看GPU内存使用情况了。用以下命令:�
mount -t debugfs debugfs /sys/kernel/debug/
cat /sys/kernel/debug/mali/gpu_memory

130|root@MR820:/ # cat /sys/kernel/debug/mali/gpu_memory
Name (:bytes) pid mali_mem max_mali_mem external_mem ump_mem dma_mem

==============================================================================================================
  erhome.fhnotice            3065        3706880     3706880          0                0           1056768   
  .yx.itvhelpself            2883        6307840     6307840          0                0           13074432  
  ndroid.settings            2660        3670016     17088512         0                0           0         
  im.yixin.tv                2300        2875392     9961472          0                0           0         
  art.tv.cloud189            2618        3145728     8675328          0                0           0         
  e.wd.mylauncher            3035        2621440     14663680         0                0           0         
  ndroid.systemui            2139        524288      12054528         0                0           16384     
  .fiberhome.iptv            2356        1048576     6393856          0                0           0         
  surfaceflinger             1408        3948544     4210688          22118400         0           24367104  
Mali mem usage: 27848704
Mali mem limit: 536870912

其中mali_mem列就是应用占用的GPU内存

相关文章

网友评论

      本文标题:Android内存的相关排查方法

      本文链接:https://www.haomeiwen.com/subject/sjzlwxtx.html