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

news/2024/4/27 18:27:48

文章目录

  • 源码
    • Runtime.getRuntime()
    • /proc/self/status
    • /proc/meminfo
  • 总结

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

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

下面正文开始:

源码

   File("/proc/self/status").forEachLineQuietly { line ->if (procStatus.vssInKb != 0 && procStatus.rssInKb != 0&& procStatus.thread != 0) return@forEachLineQuietlywhen {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.totalInKbMonitorLog.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:        0Speculation_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:        0Speculation_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
:/ # procrankPID       Vss      Rss      Pss      Uss  cmdline1529  3848968K  194612K   87827K   77496K  system_server1673  3296392K  167556K   63256K   52388K  com.android.systemui2888  3247024K  115544K   39744K   32996K  com.kwai.koom.demo2176  3250116K  139600K   39249K   29464K  com.android.launcher31859  3245816K  114868K   27183K   20520K  com.android.settings1696  1386324K   46092K   23500K   12600K  webview_zygote1843  3226548K  104512K   19487K   13796K  com.android.phone2251  3612560K  107956K   19465K   10796K  com.android.flysilkworm1659  3230128K   93368K   15295K   10788K  com.android.bluetooth1363  1380040K   51524K   13215K    1048K  zygote2298  3594528K   89688K   12022K    4932K  com.android.flysilkworm:LdPushService2309  3183496K   84676K   11479K    7676K  android.process.acore2466  3186916K   80976K   10735K    7396K  com.android.messaging2164  3187012K   84496K   10149K    5936K  android.process.media2349  3590068K   83284K    9305K    3456K  com.android.flysilkworm:filedownloader1362  3497064K   94596K    8836K    2008K  zygote642122  3201828K   78048K    8263K    4664K  com.android.coreservice2403  3182732K   77792K    7110K    3452K  com.android.inputmethod.pinyin2491  3182640K   68984K    7041K    4496K  com.android.packageinstaller2550  3182916K   70348K    6993K    4352K  com.android.traceur2516  3180036K   74636K    6848K    3764K  com.android.providers.calendar2422  3179424K   68632K    6399K    3832K  com.android.managedprovisioning2140  3180556K   69872K    6121K    3412K  com.android.printspooler1907  3180500K   71608K    5962K    2968K  android.ext.services1391    88240K   13488K    5378K    4684K  /system/bin/surfaceflinger2108  3181736K   67684K    5261K    2648K  com.android.se2848  3179104K   66600K    5177K    2600K  com.android.keychain2198  3178328K   66728K    5140K    2456K  com.android.smspush1389    58304K   10272K    5116K    4300K  /system/bin/audioserver1406    66712K   10912K    5024K    3976K  /system/bin/mediaserver1404  2169688K   15624K    4993K    3888K  media.extractor1410    44792K    7780K    3718K    3252K  media.codec1398    41808K    8316K    3220K    2440K  /system/bin/cameraserver1405  2138768K   11496K    2899K    2164K  media.metrics1306    19344K    5356K    2616K    2572K  /system/bin/logd1399    28636K    6772K    2157K    1532K  /system/bin/drmserver1361    32884K    7900K    2152K    1892K  /system/bin/netd1379    66128K    7728K    2039K    1724K  /vendor/bin/hw/android.hardware.graphics.composer@2.1-service1377    36548K    6532K    1919K    1768K  /vendor/bin/hw/android.hardware.gnss@1.0-service1311    22268K    6924K    1889K    1752K  /system/bin/vold2590    33372K    1964K    1867K    1860K  /system/bin/adbd1    19188K    2996K    1845K    1468K  /init1401    20044K    6152K    1842K    1732K  /system/bin/installd1368    41692K    4932K    1839K    1452K  /vendor/bin/hw/android.hardware.audio@2.0-service1411    19168K    5320K    1758K    1696K  /vendor/bin/hw/rild1371    38212K    5232K    1698K    1272K  /vendor/bin/hw/android.hardware.camera.provider@2.4-service1408    20764K    6244K    1646K    1464K  /system/bin/storaged1387    17820K    5208K    1634K    1572K  /vendor/bin/hw/android.hardware.wifi@1.0-service1407    20448K    6136K    1495K    1380K  /system/bin/statsd1409    19872K    6084K    1469K    1360K  /system/bin/wificond1367    23308K    7096K    1400K    1176K  /system/bin/vr_hwc1402    21536K    6648K    1389K    1244K  /system/bin/keystore1308    16620K    5884K    1381K    1192K  /system/bin/hwservicemanager1403    18776K    4428K    1196K     832K  /system/bin/mediadrmserver1378    35308K    5720K    1164K    1020K  /vendor/bin/hw/android.hardware.graphics.allocator@2.0-service1400    18120K    5608K    1144K    1048K  /system/bin/incidentd1412    17428K    6208K    1136K    1004K  /system/bin/gatekeeperd1375    17124K    6108K    1115K     952K  /vendor/bin/hw/android.hardware.drm@1.1-service.clearkey3778    13040K    3488K    1077K    1044K  procrank1374    18524K    4060K    1066K     780K  /vendor/bin/hw/android.hardware.drm@1.0-service1369    16828K    4728K    1061K     844K  /vendor/bin/hw/android.hardware.bluetooth@1.0-service-qti1011     7192K    2032K    1056K     732K  /sbin/ueventd1490     9552K    3384K    1048K    1016K  /system/bin/ip6tables-restore1376    15100K    5616K     981K     876K  /vendor/bin/hw/android.hardware.gatekeeper@1.0-service1384    33900K    4972K     944K     844K  /vendor/bin/hw/android.hardware.sensors@1.0-service1489     9536K    3124K     929K     900K  /system/bin/iptables-restore1413    17792K    4492K     919K     848K  /system/bin/perfprofd1392    14920K    5272K     908K     824K  /system/bin/thermalserviced1372    11840K    3728K     903K     628K  /vendor/bin/hw/android.hardware.cas@1.0-service1310    13932K    4760K     861K     780K  /vendor/bin/hw/android.hardware.keymaster@3.0-service1373    15192K    4516K     844K     764K  /vendor/bin/hw/android.hardware.configstore@1.1-service1009     7064K    1804K     842K     528K  /init1380    11988K    4388K     834K     696K  /vendor/bin/hw/android.hardware.health@2.0-service.default1307    11096K    3760K     826K     764K  /system/bin/servicemanager1388    31500K    4084K     825K     768K  /system/bin/ldinit2830     9228K    3188K     799K     764K  /data/local/tmp/.studio/process-tracker2834     9228K    3256K     798K     764K  /data/local/tmp/.studio/process-tracker1423     5760K     816K     776K     776K  /system/bin/mdnsd1366    11996K    4244K     759K     700K  /system/bin/healthd1309    11096K    3784K     743K     672K  /vendor/bin/vndservicemanager1370    12416K    4228K     724K     668K  /vendor/bin/hw/android.hardware.broadcastradio@1.1-service1010     6936K    1632K     722K     432K  /init1396    11336K    3400K     712K     592K  /system/bin/traced_probes1385    12876K    4088K     700K     644K  /vendor/bin/hw/android.hardware.usb@1.0-service1383    11844K    4160K     694K     632K  /vendor/bin/hw/android.hardware.power@1.1-service.default1381    11764K    4196K     692K     636K  /vendor/bin/hw/android.hardware.light@2.0-service1365    11724K    4304K     687K     616K  /system/bin/hw/android.hidl.allocator@1.0-service1386    11752K    4024K     680K     624K  /vendor/bin/hw/android.hardware.vibrator@1.0-service.default1382    11728K    4200K     666K     608K  /vendor/bin/hw/android.hardware.oemlock@1.0-service.default2824     9388K    3452K     659K     612K  logcat1395    11336K    3316K     656K     536K  /system/bin/traced3764     9024K    3212K     647K     516K  sh3739     9024K    3068K     644K     516K  /system/bin/sh1393     9024K    3236K     643K     512K  /system/bin/sh1390     9624K    3012K     518K     484K  /system/bin/lmkd1414     9388K    2864K     472K     440K  /system/bin/tombstoned3746     8028K     772K     288K     132K  su3758     5916K     708K     212K      60K  /system/xbin/su3745     5916K     696K     208K      64K  su3750     5916K     704K     208K      56K  /system/xbin/su1426     5916K     612K     177K      68K  /system/xbin/su------   ------  ------582656K  418368K  TOTALRAM: 1015336K total, 257140K free, 3196K buffers, 323996K cached, 828K shmem, 54780K slab

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

:/ # procrank | grep koom2888  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层内存监控拿到的内存指标。有了这些指标,可以比较清晰地知道当前机器和进程内存的情况。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/11024.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

基于SSM非遗视域下喀什旅游网站

ssm非遗视域下喀什旅游网站的设计与实现 摘要 我们的生活水平正在不断的提高,然而提高的一个重要的侧面表现就是更加注重我们的娱乐生活。旅行是我们都喜欢的一种娱乐方式,各式各样的旅行经历给我们带来的喜悦也是大不相同的。带来快乐的同时也因为其复…

CSRF一-WEB攻防-CSRF请求伪造Referer同源置空配合XSSToken值校验复用删除

演示案例: CSRF-无检测防护-检测&生成&利用CSRF-Referer同源-规则&上传&XSSCSRF-Token校验-值删除&复用&留空 #CSRF-无检测防护-检测&生成&利用 检测:黑盒手工利用测试,白盒看代码检验(有无token…

springboot项目中如何实现邮件告警功能(监控平台服务模拟)

介绍 模拟服务器故障&#xff0c;然后实现邮件告警 一、首先配置邮件的maven依赖 代码如下&#xff1a; <!--邮件告警--><!-- Spring Boot的邮件发送依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spri…

[数据结构]二叉树与递归OJ

上回我们手撕了一棵二叉树,并且通过递归完成了遍历,这回我们将深入理解用递归解决相关的二叉树问题,数量使用分治的思想. 上回的代码: #include<stdio.h> #include<stdlib.h> typedef struct BinTreeNode {struct BinTreeNode* left;struct BinTreeNode* right;i…

计算机软件安全

一、软件安全涉及的范围 1.1软件本身的安全保密 软件的本质与特征&#xff1a; 可移植性 寄生性 再生性 可激发性 攻击性 破坏性 …… 知识产权与软件盗版 软件商品交易形式不透明&#xff0c;方式多样&#xff0c;传统商标标识方法不适用&#xff1b; 盗版方法简捷…

Mac电脑高清媒体播放器:Movist Pro for mac下载

Movist Pro for mac是一款专为Mac操作系统设计的高清媒体播放器&#xff0c;支持多种常见的媒体格式&#xff0c;包括MKV、AVI、MP4等&#xff0c;能够流畅播放高清视频和音频文件。Movist Pro具有强大的解码能力和优化的渲染引擎&#xff0c;让您享受到更清晰、更流畅的观影体…