【Android 内存优化】Koom核心内存指标分析


获取内存的指标有很多,假如我们要写一个用于监控APP内存泄漏的框架的话,主要获取哪些指标呢?

这篇文章来研究下KOOM里面获取到是哪些指标。

下面正文开始:

源码

   File("/proc/self/status").forEachLineQuietly { line ->
            if (procStatus.vssInKb != 0 && procStatus.rssInKb != 0
                && procStatus.thread != 0
            ) return@forEachLineQuietly

            when {
                line.startsWith("VmSize") -> {
                    procStatus.vssInKb = VSS_REGEX.matchValue(line)
                }

                line.startsWith("VmRSS") -> {
                    procStatus.rssInKb = RSS_REGEX.matchValue(line)
                }

                line.startsWith("Threads") -> {
                    procStatus.thread = THREADS_REGEX.matchValue(line)
                }
            }
        }

        File("/proc/meminfo").forEachLineQuietly { line ->
            when {
                line.startsWith("MemTotal") -> {
                    memInfo.totalInKb = MEM_TOTAL_REGEX.matchValue(line)
                }

                line.startsWith("MemFree") -> {
                    memInfo.freeInKb = MEM_FREE_REGEX.matchValue(line)
                }

                line.startsWith("MemAvailable") -> {
                    memInfo.availableInKb = MEM_AVA_REGEX.matchValue(line)
                }

                line.startsWith("CmaTotal") -> {
                    memInfo.cmaTotal = MEM_CMA_REGEX.matchValue(line)
                }

                /**
                 * IONHeap 是 Android 系统中用于内存管理的一种机制,特别是在图形缓冲区和其他需要高效物理内存分配和映射的场合。ION (I/O Non-Contiguous Memory Allocator)
                 * 是一个跨内核与用户空间设计的内存管理器,它支持多种类型的内存分配,包括但不限于系统堆(system heap)、连续内存块(contiguous memory)以及从特定预留区域分配的内存(carveout heap)。
                 * 在Android系统中,ION通过统一接口处理各种类型的内存分配,允许更灵活地满足不同硬件组件(如GPU、摄像头、显示子系统等)对内存的需求,同时优化内存使用效率,减少碎片,并且支持高效的DMA操作。
                 * 上述代码片段是Kotlin程序的一部分,其中memInfo.IONHeap是用来存储从日志或者其他文本信息中解析出来的ION内存相关信息的变量。当检测到某一行字符串以"ION_heap"开头时,说明这一行可能包含
                 * ION堆的内存使用量数据,于是调用正则表达式MEM_ION_REGEX来提取出具体的内存值并将其存储在memInfo.IONHeap中。
                 */
                line.startsWith("ION_heap") -> {
                    memInfo.IONHeap = MEM_ION_REGEX.matchValue(line)
                }
            }
        }

        memInfo.rate = 1.0f * memInfo.availableInKb / memInfo.totalInKb

        MonitorLog.i(TAG, "----OOM Monitor Memory----")
        MonitorLog.i(TAG, "[java] max:${javaHeap.max} used ratio:${(javaHeap.rate * 100).toInt()}%")
        MonitorLog.i(
            TAG,
            "[proc] VmSize:${procStatus.vssInKb}kB VmRss:${procStatus.rssInKb}kB " + "Threads:${procStatus.thread}"
        )
        MonitorLog.i(
            TAG,
            "[meminfo] MemTotal:${memInfo.totalInKb}kB MemFree:${memInfo.freeInKb}kB " + "MemAvailable:${memInfo.availableInKb}kB"
        )
        MonitorLog.i(
            TAG,
            "avaliable ratio:${(memInfo.rate * 100).toInt()}% CmaTotal:${memInfo.cmaTotal}kB ION_heap:${memInfo.IONHeap}kB"
        )
    }

这里直接copy这部分的源码出来,可以看到里面只拿到了几个指标。为什么拿到的是这些指标呢?我们通过命令行拿到对应的数据研究下。

Runtime.getRuntime()

  javaHeap.max = Runtime.getRuntime().maxMemory()
  javaHeap.total = Runtime.getRuntime().totalMemory()
  javaHeap.free = Runtime.getRuntime().freeMemory()

通过运行时获取到Java虚拟机堆的信息。

/proc/self/status

首先拿到进程对应的pid。

PS C:\Users\LinShujie> adb shell ps | grep koom
PS C:\Users\LinShujie> adb shell ps | grep koom
u0_a50        2888  1362 3219764  95460 0                   0 S com.kwai.koom.demo

手动触发memory leak前:

PS C:\Users\LinShujie> adb shell cat /proc/2888/status
Name:   .kwai.koom.demo
State:  S (sleeping)
Tgid:   2888
Ngid:   0
Pid:    2888
PPid:   1362
TracerPid:      0
Uid:    10050   10050   10050   10050
Gid:    10050   10050   10050   10050
FDSize: 64
Groups: 9997 20050 50050
NStgid: 2888
NSpid:  2888
NSpgid: 1362
NSsid:  0
VmPeak:  3580660 kB
VmSize:  3219892 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:     95460 kB
VmRSS:     95460 kB
VmData:  2311600 kB
VmStk:      8192 kB
VmExe:        20 kB
VmLib:    117940 kB
VmPTE:       632 kB
VmPMD:        24 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
Threads:        13
SigQ:   0/3957
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000001204
SigIgn: 0000000000000001
SigCgt: 00000006400084f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
CapAmb: 0000000000000000
Seccomp:        0

Speculation_Store_Bypass:       vulnerable
Cpus_allowed:   3
Cpus_allowed_list:      0-1
voluntary_ctxt_switches:        197
nonvoluntary_ctxt_switches:     100

手动触发memory leak后:

PS C:\Users\LinShujie> adb shell cat /proc/2888/status
Name:   .kwai.koom.demo
State:  S (sleeping)
Tgid:   2888
Ngid:   0
Pid:    2888
PPid:   1362
TracerPid:      0
Uid:    10050   10050   10050   10050
Gid:    10050   10050   10050   10050
FDSize: 64
Groups: 9997 20050 50050
NStgid: 2888
NSpid:  2888
NSpgid: 1362
NSsid:  0
VmPeak:  4015804 kB
VmSize:  4014224 kB
VmLck:         0 kB
VmPin:         0 kB
VmHWM:    114012 kB
VmRSS:    114004 kB
VmData:  3093864 kB
VmStk:      8192 kB
VmExe:        20 kB
VmLib:    119268 kB
VmPTE:      2212 kB
VmPMD:        28 kB
VmSwap:        0 kB
HugetlbPages:          0 kB
Threads:        715
SigQ:   0/3957
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000001204
SigIgn: 0000000000000001
SigCgt: 00000006400084f8
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000000000000000
CapAmb: 0000000000000000
Seccomp:        0

Speculation_Store_Bypass:       vulnerable
Cpus_allowed:   3
Cpus_allowed_list:      0-1
voluntary_ctxt_switches:        1964
nonvoluntary_ctxt_switches:     229

代码里面拿到的是VmSize、VmRSS、和线程数。

这里的VmSize和VmRss和Vss 、Rss是否为同一个指标?

我们procrank命令来看下结果:

1|star2qltechn:/ $ su
:/ # procrank
  PID       Vss      Rss      Pss      Uss  cmdline
 1529  3848968K  194612K   87827K   77496K  system_server
 1673  3296392K  167556K   63256K   52388K  com.android.systemui
 2888  3247024K  115544K   39744K   32996K  com.kwai.koom.demo
 2176  3250116K  139600K   39249K   29464K  com.android.launcher3
 1859  3245816K  114868K   27183K   20520K  com.android.settings
 1696  1386324K   46092K   23500K   12600K  webview_zygote
 1843  3226548K  104512K   19487K   13796K  com.android.phone
 2251  3612560K  107956K   19465K   10796K  com.android.flysilkworm
 1659  3230128K   93368K   15295K   10788K  com.android.bluetooth
 1363  1380040K   51524K   13215K    1048K  zygote
 2298  3594528K   89688K   12022K    4932K  com.android.flysilkworm:LdPushService
 2309  3183496K   84676K   11479K    7676K  android.process.acore
 2466  3186916K   80976K   10735K    7396K  com.android.messaging
 2164  3187012K   84496K   10149K    5936K  android.process.media
 2349  3590068K   83284K    9305K    3456K  com.android.flysilkworm:filedownloader
 1362  3497064K   94596K    8836K    2008K  zygote64
 2122  3201828K   78048K    8263K    4664K  com.android.coreservice
 2403  3182732K   77792K    7110K    3452K  com.android.inputmethod.pinyin
 2491  3182640K   68984K    7041K    4496K  com.android.packageinstaller
 2550  3182916K   70348K    6993K    4352K  com.android.traceur
 2516  3180036K   74636K    6848K    3764K  com.android.providers.calendar
 2422  3179424K   68632K    6399K    3832K  com.android.managedprovisioning
 2140  3180556K   69872K    6121K    3412K  com.android.printspooler
 1907  3180500K   71608K    5962K    2968K  android.ext.services
 1391    88240K   13488K    5378K    4684K  /system/bin/surfaceflinger
 2108  3181736K   67684K    5261K    2648K  com.android.se
 2848  3179104K   66600K    5177K    2600K  com.android.keychain
 2198  3178328K   66728K    5140K    2456K  com.android.smspush
 1389    58304K   10272K    5116K    4300K  /system/bin/audioserver
 1406    66712K   10912K    5024K    3976K  /system/bin/mediaserver
 1404  2169688K   15624K    4993K    3888K  media.extractor
 1410    44792K    7780K    3718K    3252K  media.codec
 1398    41808K    8316K    3220K    2440K  /system/bin/cameraserver
 1405  2138768K   11496K    2899K    2164K  media.metrics
 1306    19344K    5356K    2616K    2572K  /system/bin/logd
 1399    28636K    6772K    2157K    1532K  /system/bin/drmserver
 1361    32884K    7900K    2152K    1892K  /system/bin/netd
 1379    66128K    7728K    2039K    1724K  /vendor/bin/hw/android.hardware.graphics.composer@2.1-service
 1377    36548K    6532K    1919K    1768K  /vendor/bin/hw/android.hardware.gnss@1.0-service
 1311    22268K    6924K    1889K    1752K  /system/bin/vold
 2590    33372K    1964K    1867K    1860K  /system/bin/adbd
    1    19188K    2996K    1845K    1468K  /init
 1401    20044K    6152K    1842K    1732K  /system/bin/installd
 1368    41692K    4932K    1839K    1452K  /vendor/bin/hw/android.hardware.audio@2.0-service
 1411    19168K    5320K    1758K    1696K  /vendor/bin/hw/rild
 1371    38212K    5232K    1698K    1272K  /vendor/bin/hw/android.hardware.camera.provider@2.4-service
 1408    20764K    6244K    1646K    1464K  /system/bin/storaged
 1387    17820K    5208K    1634K    1572K  /vendor/bin/hw/android.hardware.wifi@1.0-service
 1407    20448K    6136K    1495K    1380K  /system/bin/statsd
 1409    19872K    6084K    1469K    1360K  /system/bin/wificond
 1367    23308K    7096K    1400K    1176K  /system/bin/vr_hwc
 1402    21536K    6648K    1389K    1244K  /system/bin/keystore
 1308    16620K    5884K    1381K    1192K  /system/bin/hwservicemanager
 1403    18776K    4428K    1196K     832K  /system/bin/mediadrmserver
 1378    35308K    5720K    1164K    1020K  /vendor/bin/hw/android.hardware.graphics.allocator@2.0-service
 1400    18120K    5608K    1144K    1048K  /system/bin/incidentd
 1412    17428K    6208K    1136K    1004K  /system/bin/gatekeeperd
 1375    17124K    6108K    1115K     952K  /vendor/bin/hw/android.hardware.drm@1.1-service.clearkey
 3778    13040K    3488K    1077K    1044K  procrank
 1374    18524K    4060K    1066K     780K  /vendor/bin/hw/android.hardware.drm@1.0-service
 1369    16828K    4728K    1061K     844K  /vendor/bin/hw/android.hardware.bluetooth@1.0-service-qti
 1011     7192K    2032K    1056K     732K  /sbin/ueventd
 1490     9552K    3384K    1048K    1016K  /system/bin/ip6tables-restore
 1376    15100K    5616K     981K     876K  /vendor/bin/hw/android.hardware.gatekeeper@1.0-service
 1384    33900K    4972K     944K     844K  /vendor/bin/hw/android.hardware.sensors@1.0-service
 1489     9536K    3124K     929K     900K  /system/bin/iptables-restore
 1413    17792K    4492K     919K     848K  /system/bin/perfprofd
 1392    14920K    5272K     908K     824K  /system/bin/thermalserviced
 1372    11840K    3728K     903K     628K  /vendor/bin/hw/android.hardware.cas@1.0-service
 1310    13932K    4760K     861K     780K  /vendor/bin/hw/android.hardware.keymaster@3.0-service
 1373    15192K    4516K     844K     764K  /vendor/bin/hw/android.hardware.configstore@1.1-service
 1009     7064K    1804K     842K     528K  /init
 1380    11988K    4388K     834K     696K  /vendor/bin/hw/android.hardware.health@2.0-service.default
 1307    11096K    3760K     826K     764K  /system/bin/servicemanager
 1388    31500K    4084K     825K     768K  /system/bin/ldinit
 2830     9228K    3188K     799K     764K  /data/local/tmp/.studio/process-tracker
 2834     9228K    3256K     798K     764K  /data/local/tmp/.studio/process-tracker
 1423     5760K     816K     776K     776K  /system/bin/mdnsd
 1366    11996K    4244K     759K     700K  /system/bin/healthd
 1309    11096K    3784K     743K     672K  /vendor/bin/vndservicemanager
 1370    12416K    4228K     724K     668K  /vendor/bin/hw/android.hardware.broadcastradio@1.1-service
 1010     6936K    1632K     722K     432K  /init
 1396    11336K    3400K     712K     592K  /system/bin/traced_probes
 1385    12876K    4088K     700K     644K  /vendor/bin/hw/android.hardware.usb@1.0-service
 1383    11844K    4160K     694K     632K  /vendor/bin/hw/android.hardware.power@1.1-service.default
 1381    11764K    4196K     692K     636K  /vendor/bin/hw/android.hardware.light@2.0-service
 1365    11724K    4304K     687K     616K  /system/bin/hw/android.hidl.allocator@1.0-service
 1386    11752K    4024K     680K     624K  /vendor/bin/hw/android.hardware.vibrator@1.0-service.default
 1382    11728K    4200K     666K     608K  /vendor/bin/hw/android.hardware.oemlock@1.0-service.default
 2824     9388K    3452K     659K     612K  logcat
 1395    11336K    3316K     656K     536K  /system/bin/traced
 3764     9024K    3212K     647K     516K  sh
 3739     9024K    3068K     644K     516K  /system/bin/sh
 1393     9024K    3236K     643K     512K  /system/bin/sh
 1390     9624K    3012K     518K     484K  /system/bin/lmkd
 1414     9388K    2864K     472K     440K  /system/bin/tombstoned
 3746     8028K     772K     288K     132K  su
 3758     5916K     708K     212K      60K  /system/xbin/su
 3745     5916K     696K     208K      64K  su
 3750     5916K     704K     208K      56K  /system/xbin/su
 1426     5916K     612K     177K      68K  /system/xbin/su
                           ------   ------  ------
                          582656K  418368K  TOTAL

 RAM: 1015336K total, 257140K free, 3196K buffers, 323996K cached, 828K shmem, 54780K slab

数据有点多,我们做个筛选:

:/ # procrank | grep koom
 2888  3247024K  115544K   39743K   32996K  com.kwai.koom.demo

这里

vss=3247024K

rss=115544K

而上面

VmSize: 4014224 kB

VmRSS: 114004 kB

数据总体差不多,但是细节有点差异,推测是同一概念,因为虚拟内存作为一种内存管理技术会在进程运行不同阶段发生动态变化。

/proc/meminfo

我这边运行是在虚拟机上面跑到Android系统,给机器分配了1G的内存。

运行命令:

127|:/ # cat /proc/meminfo
MemTotal:        1015336 kB
MemFree:          277452 kB
MemAvailable:     573328 kB
Buffers:            3392 kB
Cached:           322680 kB
SwapCached:            0 kB
Active:           511760 kB
Inactive:         130752 kB
Active(anon):     319152 kB
Inactive(anon):      384 kB
Active(file):     192608 kB
Inactive(file):   130368 kB
Unevictable:        2824 kB
Mlocked:            2824 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        319368 kB
Mapped:           242092 kB
Shmem:               828 kB
Slab:              53360 kB
SReclaimable:      17724 kB
SUnreclaim:        35636 kB
KernelStack:       12144 kB
PageTables:        19208 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      507668 kB
Committed_AS:   20653316 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       34752 kB
DirectMap2M:     1013760 kB

源码获取的指标有:

  1. MemTotal
  2. MemFree
  3. MemAvailable
  4. CmaTotal
  5. ION_heap

这里主要是拿到当前机器的物理内存。

这里解释下CmaTotal的概念:

在 Linux 内核中,CMA(Contiguous Memory Allocator,连续内存分配器)是一种内存管理机制,旨在为一些对连续内存有特定需求的设备提供分配连续内存的能力。

而ION_heap:

ION(Input-Output Memory Management)是Android系统中的一种内存管理框架,用于管理设备内存的分配和映射。

ION_heap是ION框架中用于分配内存的堆(heap)。它允许Android系统和设备驱动程序在需要时动态地分配和管理内存,以满足不同应用程序和系统组件的需求。

In general, memory footprint has the following rules: VSS >= RSS >= PSS >= USS

Overview

The aim of this post was to provide information that would assist in interpreting memory reports from various tools so the T Rue memory usage for Linux processes and the system can is determined.

Android have a tool called Procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order F Rom highest to lowest usage. The sizes reported per process is VSS, RSS, PSS, and USS.

For the sake of simplicity in this description, memory would be a expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 bytes pages at the lowest level.

VSS (reported as VSZ from PS) are the total accessible address space of a process. This is the size also includes memory that May is resident in RAM like Mallocs that has been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

RSS is the total memory actually held in RAM for a process. RSS can be misleading, because it reports the total all of the the the A-Libraries that process uses, even though a share D library is a loaded into memory once regardless of what many processes use it. RSS isn’t an accurate representation of the memory usage for a single process.

pss differs from RSS on that it reports the proportional size of its shared Librari ES, i.e. if three processes all use a shared libraries that have pages, that library would only contribute pages to the PSS is reported for each of the three processes. PSS is a very useful number because if the PSS for all processes in the system is summed together, which is a good repre Sentation for the total memory usage in the system. When a process was killed, the shared libraries that contributed to its PSS would be proportionally distributed to the PSS T Otals for the remaining processes still using that library. In this-a-slightly misleading, because when a process is killed, PSS does not accurately represent the memory Returned to the overall system.

USS is the total private memory for a process, i.e. that’s memory that’s completely unique to that process. USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process was killed, the USS is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaks in a process.

引用地址:Vss/rss/pss/uss of Android Memory (alibabacloud.com)

总结

----OOM Monitor Memory----
[java] max:201326592 used ratio:2%
[proc] VmSize:4010936kB VmRss:108604kB Threads:715
[meminfo] MemTotal:1015336kB MemFree:225820kB MemAvailable:515340kB
avaliable ratio:50% CmaTotal:0kB ION_heap:0kB
[meet condition] overThresholdCount:1, threadCount: 715

这就是KOOM在Java层内存监控拿到的内存指标。有了这些指标,可以比较清晰地知道当前机器和进程内存的情况。

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林树杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值