Android内存优化-了解内存篇

查看系统内存

文件

shell@aries:/ $ cat /proc/meminfo
MemTotal:        1970216 kB
MemFree:           83756 kB
Buffers:          156020 kB
Cached:           702516 kB
SwapCached:            0 kB
Active:          1160284 kB
Inactive:         397932 kB
Active(anon):     778932 kB
Inactive(anon):     2228 kB
Active(file):     381352 kB
Inactive(file):   395704 kB
Unevictable:       79176 kB
Mlocked:           74812 kB
HighTotal:       1297404 kB
HighFree:          11192 kB
LowTotal:         672812 kB
LowFree:           72564 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        778816 kB
Mapped:           313368 kB
Shmem:              2336 kB
Slab:              87660 kB
SReclaimable:      49088 kB
SUnreclaim:        38572 kB
KernelStack:       13616 kB
PageTables:        28852 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      985108 kB
Committed_AS:   34864176 kB
VmallocTotal:     245760 kB
VmallocUsed:       95460 kB
VmallocChunk:      69492 kB

命令

shell@aries:/ $ dumpsys meminfo
Applications Memory Usage (kB):
Uptime: 11383438 Realtime: 11576648

Total PSS SWAP by process:
   122031 kB:       0 kB: system (pid 876)
    74706 kB:       0 kB: com.android.systemui (pid 1107)
    68120 kB:       0 kB: com.tencent.mm:push (pid 4553)
    66872 kB:       0 kB: com.sohu.inputmethod.sogou (pid 1393)
    52084 kB:       0 kB: com.miui.home (pid 4724 / activities)
    32189 kB:       0 kB: com.tencent.mobileqq:MSF (pid 2858)
    31242 kB:       0 kB: com.xiaomi.gamecenter (pid 4337)
    29570 kB:       0 kB: android.process.media (pid 1081)
    28265 kB:       0 kB: com.android.phone (pid 1650)
    26193 kB:       0 kB: com.mi.liveassistant (pid 4378)
    25666 kB:       0 kB: com.miui.systemAdSolution (pid 2338)
    22753 kB:       0 kB: com.miui.powerkeeper:service (pid 1345)
    21066 kB:       0 kB: com.miui.securitycenter.remote (pid 1334)
    19893 kB:       0 kB: com.xiaomi.market (pid 17770)
    19694 kB:       0 kB: com.xiaomi.finddevice (pid 1540)
    19625 kB:       0 kB: com.xiaomi.xmsf (pid 1255)
    19192 kB:       0 kB: com.miui.daemon (pid 1621)
    18758 kB:       0 kB: com.miui.whetstone (pid 1526)
    18307 kB:       0 kB: com.android.thememanager (pid 17630)
    17393 kB:       0 kB: com.lbe.security.miui (pid 2136)
    17018 kB:       0 kB: com.miui.analytics (pid 1980)
    16661 kB:       0 kB: com.omottec.demoapp (pid 22772 / activities)
    15325 kB:       0 kB: com.sohu.inputmethod.sogou:push_service (pid 11351)
    14512 kB:       0 kB: com.xiaomi.metoknlp (pid 1444)
    13765 kB:       0 kB: com.xiaomi.gamecenter:migameRemote (pid 4297)
    13664 kB:       0 kB: com.android.settings:remote (pid 1897)
    12461 kB:       0 kB: com.miui.networkassistant.deamon (pid 2634)
    11850 kB:       0 kB: com.sohu.inputmethod.sogou:classic (pid 10650)
    11410 kB:       0 kB: perfd (pid 22753)
    10631 kB:       0 kB: com.miui.hybrid:entrance (pid 4160)
     9767 kB:       0 kB: com.android.settings (pid 22845)
     8963 kB:       0 kB: mediaserver (pid 308)
     8255 kB:       0 kB: com.miui.wmsvc (pid 4022)
     8251 kB:       0 kB: com.miui.cloudservice (pid 3928)
     8068 kB:       0 kB: surfaceflinger (pid 281)
     7704 kB:       0 kB: com.miui.klo.bugreport (pid 2083)
     7696 kB:       0 kB: com.miui.core (pid 1373)
     7525 kB:       0 kB: com.android.deskclock (pid 18052)
     7407 kB:       0 kB: com.xiaomi.simactivate.service (pid 17399)
     7189 kB:       0 kB: com.miui.sysbase (pid 2052)
     7149 kB:       0 kB: com.dolby (pid 1568)
     6701 kB:       0 kB: com.android.server.telecom (pid 1605)
     6676 kB:       0 kB: com.android.incallui (pid 16441)
     6658 kB:       0 kB: zygote (pid 319)
     6485 kB:       0 kB: com.quicinc.cne.CNEService (pid 1668)
     5348 kB:       0 kB: com.miui.systemAdSolution:ui (pid 19809)
     4537 kB:       0 kB: com.xiaomi.providers.appindex (pid 16598)
     4160 kB:       0 kB: com.android.mms.service (pid 1515)
     3882 kB:       0 kB: rild (pid 306)
     3847 kB:       0 kB: com.xiaomi.mitunes (pid 1631)
     3796 kB:       0 kB: sdcard (pid 346)
     3771 kB:       0 kB: com.android.smspush (pid 2114)
     3139 kB:       0 kB: mm-qcamera-daemon (pid 287)
     3119 kB:       0 kB: shelld (pid 320)
     1660 kB:       0 kB: wpa_supplicant (pid 1178)
     1489 kB:       0 kB: mcd (pid 2675)
     1487 kB:       0 kB: netd (pid 304)
     1322 kB:       0 kB: drmserver (pid 307)
     1086 kB:       0 kB: ks (pid 940)
     1068 kB:       0 kB: lowi-server (pid 360)
      961 kB:       0 kB: vold (pid 280)
      907 kB:       0 kB: thermald (pid 2763)
      885 kB:       0 kB: xtwifi-inet-agent (pid 361)
      807 kB:       0 kB: logd (pid 276)
      798 kB:       0 kB: sensors.qcom (pid 531)
      776 kB:       0 kB: cnd (pid 326)
      756 kB:       0 kB: /init (pid 1)
      726 kB:       0 kB: netmgrd (pid 487)
      698 kB:       0 kB: qmuxd (pid 486)
      670 kB:       0 kB: keystore (pid 311)
      658 kB:       0 kB: zygote (pid 324)
      644 kB:       0 kB: mdbd (pid 364)
      600 kB:       0 kB: adbd (pid 355)
      594 kB:       0 kB: time_daemon (pid 344)
      588 kB:       0 kB: ATFWD-daemon (pid 19848)
      524 kB:       0 kB: ueventd (pid 156)
      515 kB:       0 kB: otad (pid 312)
      493 kB:       0 kB: fdpp (pid 322)
      468 kB:       0 kB: mpdecision (pid 2768)
      442 kB:       0 kB: installd (pid 309)
      412 kB:       0 kB: dhcpcd (pid 9815)
      389 kB:       0 kB: logcat (pid 22734)
      387 kB:       0 kB: namespaceCreateSvc (pid 325)
      384 kB:       0 kB: sh (pid 22965)
      375 kB:       0 kB: lmkd (pid 278)
      372 kB:       0 kB: location-mq (pid 359)
      371 kB:       0 kB: dumpsys (pid 23128)
      368 kB:       0 kB: sh (pid 9553)
      364 kB:       0 kB: qseecomd (pid 370)
      363 kB:       0 kB: servicemanager (pid 279)
      352 kB:       0 kB: sh (pid 22751)
      346 kB:       0 kB: loc_launcher (pid 328)
      345 kB:       0 kB: qcks (pid 303)
      336 kB:       0 kB: healthd (pid 277)
      334 kB:       0 kB: ssr_diag (pid 342)
      304 kB:       0 kB: debuggerd (pid 305)
      301 kB:       0 kB: wcnss_service (pid 345)
      297 kB:       0 kB: efsks (pid 900)
      223 kB:       0 kB: qseecomd (pid 343)
      155 kB:       0 kB: su (pid 9548)
      153 kB:       0 kB: su (pid 321)
      144 kB:       0 kB: su (pid 9551)
        0 kB:       0 kB: dumpsys (pid 23156)

Total PSS SWAP by OOM adjustment:
    77732 kB:       0 kB: Native
               11410 kB:       0 kB: perfd (pid 22753)
                8963 kB:       0 kB: mediaserver (pid 308)
                8068 kB:       0 kB: surfaceflinger (pid 281)
                6658 kB:       0 kB: zygote (pid 319)
                3882 kB:       0 kB: rild (pid 306)
                3796 kB:       0 kB: sdcard (pid 346)
                3139 kB:       0 kB: mm-qcamera-daemon (pid 287)
                3119 kB:       0 kB: shelld (pid 320)
                1660 kB:       0 kB: wpa_supplicant (pid 1178)
                1489 kB:       0 kB: mcd (pid 2675)
                1487 kB:       0 kB: netd (pid 304)
                1322 kB:       0 kB: drmserver (pid 307)
                1086 kB:       0 kB: ks (pid 940)
                1068 kB:       0 kB: lowi-server (pid 360)
                 961 kB:       0 kB: vold (pid 280)
                 907 kB:       0 kB: thermald (pid 2763)
                 885 kB:       0 kB: xtwifi-inet-agent (pid 361)
                 807 kB:       0 kB: logd (pid 276)
                 798 kB:       0 kB: sensors.qcom (pid 531)
                 776 kB:       0 kB: cnd (pid 326)
                 756 kB:       0 kB: /init (pid 1)
                 726 kB:       0 kB: netmgrd (pid 487)
                 698 kB:       0 kB: qmuxd (pid 486)
                 670 kB:       0 kB: keystore (pid 311)
                 658 kB:       0 kB: zygote (pid 324)
                 644 kB:       0 kB: mdbd (pid 364)
                 600 kB:       0 kB: adbd (pid 355)
                 594 kB:       0 kB: time_daemon (pid 344)
                 588 kB:       0 kB: ATFWD-daemon (pid 19848)
                 524 kB:       0 kB: ueventd (pid 156)
                 515 kB:       0 kB: otad (pid 312)
                 493 kB:       0 kB: fdpp (pid 322)
                 468 kB:       0 kB: mpdecision (pid 2768)
                 442 kB:       0 kB: installd (pid 309)
                 412 kB:       0 kB: dhcpcd (pid 9815)
                 389 kB:       0 kB: logcat (pid 22734)
                 387 kB:       0 kB: namespaceCreateSvc (pid 325)
                 384 kB:       0 kB: sh (pid 22965)
                 375 kB:       0 kB: lmkd (pid 278)
                 372 kB:       0 kB: location-mq (pid 359)
                 371 kB:       0 kB: dumpsys (pid 23128)
                 368 kB:       0 kB: sh (pid 9553)
                 364 kB:       0 kB: qseecomd (pid 370)
                 363 kB:       0 kB: servicemanager (pid 279)
                 352 kB:       0 kB: sh (pid 22751)
                 346 kB:       0 kB: loc_launcher (pid 328)
                 345 kB:       0 kB: qcks (pid 303)
                 336 kB:       0 kB: healthd (pid 277)
                 334 kB:       0 kB: ssr_diag (pid 342)
                 304 kB:       0 kB: debuggerd (pid 305)
                 301 kB:       0 kB: wcnss_service (pid 345)
                 297 kB:       0 kB: efsks (pid 900)
                 223 kB:       0 kB: qseecomd (pid 343)
                 155 kB:       0 kB: su (pid 9548)
                 153 kB:       0 kB: su (pid 321)
                 144 kB:       0 kB: su (pid 9551)
                   0 kB:       0 kB: dumpsys (pid 23156)
   326453 kB:       0 kB: Persistent
              122031 kB:       0 kB: system (pid 876)
               74706 kB:       0 kB: com.android.systemui (pid 1107)
               28265 kB:       0 kB: com.android.phone (pid 1650)
               19694 kB:       0 kB: com.xiaomi.finddevice (pid 1540)
               19625 kB:       0 kB: com.xiaomi.xmsf (pid 1255)
               19192 kB:       0 kB: com.miui.daemon (pid 1621)
               18758 kB:       0 kB: com.miui.whetstone (pid 1526)
                7149 kB:       0 kB: com.dolby (pid 1568)
                6701 kB:       0 kB: com.android.server.telecom (pid 1605)
                6485 kB:       0 kB: com.quicinc.cne.CNEService (pid 1668)
                3847 kB:       0 kB: com.xiaomi.mitunes (pid 1631)
    72941 kB:       0 kB: Foreground
               22753 kB:       0 kB: com.miui.powerkeeper:service (pid 1345)
               21066 kB:       0 kB: com.miui.securitycenter.remote (pid 1334)
               16661 kB:       0 kB: com.omottec.demoapp (pid 22772 / activities)
               12461 kB:       0 kB: com.miui.networkassistant.deamon (pid 2634)
    91202 kB:       0 kB: Visible
               25666 kB:       0 kB: com.miui.systemAdSolution (pid 2338)
               17018 kB:       0 kB: com.miui.analytics (pid 1980)
               14512 kB:       0 kB: com.xiaomi.metoknlp (pid 1444)
               10631 kB:       0 kB: com.miui.hybrid:entrance (pid 4160)
                8255 kB:       0 kB: com.miui.wmsvc (pid 4022)
                7189 kB:       0 kB: com.miui.sysbase (pid 2052)
                4160 kB:       0 kB: com.android.mms.service (pid 1515)
                3771 kB:       0 kB: com.android.smspush (pid 2114)
   136349 kB:       0 kB: Perceptible
               66872 kB:       0 kB: com.sohu.inputmethod.sogou (pid 1393)
               52084 kB:       0 kB: com.miui.home (pid 4724 / activities)
               17393 kB:       0 kB: com.lbe.security.miui (pid 2136)
    37266 kB:       0 kB: A Services
               29570 kB:       0 kB: android.process.media (pid 1081)
                7696 kB:       0 kB: com.miui.core (pid 1373)
   220417 kB:       0 kB: B Services
               68120 kB:       0 kB: com.tencent.mm:push (pid 4553)
               32189 kB:       0 kB: com.tencent.mobileqq:MSF (pid 2858)
               31242 kB:       0 kB: com.xiaomi.gamecenter (pid 4337)
               18307 kB:       0 kB: com.android.thememanager (pid 17630)
               15325 kB:       0 kB: com.sohu.inputmethod.sogou:push_service (pid 11351)
               13765 kB:       0 kB: com.xiaomi.gamecenter:migameRemote (pid 4297)
               13664 kB:       0 kB: com.android.settings:remote (pid 1897)
               11850 kB:       0 kB: com.sohu.inputmethod.sogou:classic (pid 10650)
                8251 kB:       0 kB: com.miui.cloudservice (pid 3928)
                7704 kB:       0 kB: com.miui.klo.bugreport (pid 2083)
    87346 kB:       0 kB: Cached
               26193 kB:       0 kB: com.mi.liveassistant (pid 4378)
               19893 kB:       0 kB: com.xiaomi.market (pid 17770)
                9767 kB:       0 kB: com.android.settings (pid 22845)
                7525 kB:       0 kB: com.android.deskclock (pid 18052)
                7407 kB:       0 kB: com.xiaomi.simactivate.service (pid 17399)
                6676 kB:       0 kB: com.android.incallui (pid 16441)
                5348 kB:       0 kB: com.miui.systemAdSolution:ui (pid 19809)
                4537 kB:       0 kB: com.xiaomi.providers.appindex (pid 16598)

Total PSS by category:
   358206 kB: Unknown
   272790 kB: Dalvik
   137434 kB: .dex mmap
    78718 kB: code mmap
    60073 kB: image mmap
    55213 kB: .so mmap
    29328 kB: Dalvik Other
    22307 kB: Other dev
    11759 kB: .apk mmap
    11507 kB: Other mmap
    10670 kB: Stack
     1351 kB: .ttf mmap
      236 kB: Ashmem
       52 kB: Cursor
       38 kB: .jar mmap
       24 kB: Native
        0 kB: Graphics
        0 kB: GL
        0 kB: Memtrack

Total RAM: 1970216 kB (status moderate)
 Free RAM: 933098 kB (87346 cached pss + 696288 cached + 149464 free)
 Used RAM: 1198660 kB (962360 used pss + 146524 buffers + 2336 shmem + 87440 slab)
 Lost RAM: -161542 kB
   Tuning: 96 (large 256), oom 122880 kB, restore limit 40960 kB (high-end-gfx)

API

系统内存-API

监控

系统内存-监控-ComponentCallbacks

系统内存-监控-ComponentCallbacks2

查看进程内存

命令

shell@aries:/ $ dumpsys meminfo com.omottec.demoapp
Applications Memory Usage (kB):
Uptime: 10755982 Realtime: 10949191

** MEMINFO in pid 17704 [com.omottec.demoapp] **
                   Pss  Private  Private  Swapped     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap        0        0        0        0    11659    11659    15988
  Dalvik Heap     2552     2408        0        0    10539     7936     2603
 Dalvik Other      388      388        0        0
        Stack      144      144        0        0
       Ashmem        2        0        0        0
    Other dev      881      872        8        0
     .so mmap      880      340       48        0
    .apk mmap       87        0       12        0
    .ttf mmap       10        0        0        0
    .dex mmap     4452        0     4448        0
    code mmap      778        0        0        0
   image mmap     1321     1060       24        0
   Other mmap       58        4        4        0
      Unknown     4808     4704        0        0
        TOTAL    16361     9920     4544        0    22198    19595    18591

 Objects
               Views:        8         ViewRootImpl:        1
         AppContexts:        3           Activities:        1
              Assets:        5        AssetManagers:        5
       Local Binders:        9        Proxy Binders:       17
    Death Recipients:        1
     OpenSSL Sockets:        0

 SQL
         MEMORY_USED:        0
  PAGECACHE_OVERFLOW:        0          MALLOC_SIZE:        0


 Asset Allocations
    zip:/system/framework/framework-res.apk:/resources.arsc: 1349K
    zip:/system/app/miui/miui.apk:/resources.arsc: 308K
    zip:/system/app/miuisystem/miuisystem.apk:/resources.arsc: 47K
    zip:/system/framework/framework-ext-res/framework-ext-res.apk:/resources.arsc: 99K

API

进程内存-API.png

重要的列

Private Clean
从某个持久性文件(例如正在执行的代码)映射的RAM页,如果一段时间不用,可以移出分页。

Private Dirty
开销最大,被进程独占,已被修改而必须保持在RAM中的RAM 页,Native堆分配和Dalvik堆分配都是private dirty RAM。

PSS(Proportional Set Size)
所有的应用进程都fork自zygote进程,zygote进程启动时会加载公共的类和资源,应用进程通过Binder机制与Zygote进程通信,这样可以达到共享内存的效果,所以一个进程占用的实际内存是自身独占的内存加按比例占用的共享内,系统设置里面对进程占用内存的统计就是取的这个值。

Private RAM

Memory Profiler顶部显示的就是Private RAM


PrivateRAM

Java:Java 或 Kotlin 代码分配的内存。
Native:native层的 so 中调用malloc或new创建的内存。
Graphics: GL 表面、GL 纹理等使用的内存。
Stack:线程栈。
Code:dex 字节码、so 库和字体占用的内存。

重要的行

Dalvik Heap
Java Heap,开发中打交道最多

.so mmap 和 .dex mmap
.so和.dex代码映射到内存后占用的 RAM

TOTAL
进程总的内存占用

Dalvik Heap

命令

shell@aries:/ $ getprop | grep 'dalvik'
[dalvik.vm.dex2oat-Xms]: [64m]
[dalvik.vm.dex2oat-Xmx]: [512m]
[dalvik.vm.heapgrowthlimit]: [96m]
[dalvik.vm.heapmaxfree]: [8m]
[dalvik.vm.heapminfree]: [2m]
[dalvik.vm.heapsize]: [256m]
[dalvik.vm.heapstartsize]: [8m]
[dalvik.vm.heaptargetutilization]: [0.75]
[dalvik.vm.image-dex2oat-Xms]: [64m]
[dalvik.vm.image-dex2oat-Xmx]: [64m]
[dalvik.vm.isa.arm.features]: [div]
[dalvik.vm.stack-trace-file]: [/data/anr/traces.txt]
[persist.sys.dalvik.vm.lib.2]: [libart.so]
[ro.dalvik.vm.native.bridge]: [0]

API

DalvikHeap-API-ActivityManager

DalvikHeap-API-Runtime

总结

内存-总结

推荐阅读更多精彩内容