current position:Home>Android system view memory command

Android system view memory command

2022-01-27 08:42:56 Yin Jie enjoy your code

introduction
In the process of system tuning or troubleshooting system memory leakage , We need to check the memory of the system , Here is a detailed list of memory viewing methods , You can compare their differences horizontally

View by / command :

dumpsys meminfo 
dumpsys meminfo packagename
procrank
procmem
free / free -m
cat /proc/meminfo
top、ps[-A]
Basic concepts
USS:  Process exclusive memory , Does not contain the memory where the shared library is used ( Physical memory )
PSS: USS +  The shared library used is divided equally into the memory of each process that uses it ( Physical memory )
RSS:PSS +  Memory occupied by shared libraries ( Physical memory )
VSS: Virtual memory occupied by process , It is rarely used to analyze memory 
VSS >= RSS >= PSS >= USS

dumpsys meminfo
dumpsys meminfo There are two uses for commands , You can use it directly , You can also use the following process name , Such as dumpsys meminfo tv.fun.test

130|shell@LCD_42SFMTCCA_B:/ # dumpsys meminfo                                  
Applications Memory Usage (kB):
Uptime: 8248855 Realtime: 8248855
Total PSS by process: // List all processes PSS Memory value 
    33744 kB: system (pid 3424)
    28585 kB: surfaceflinger (pid 3022)
    24204 kB: com.sharp.launcher3 (pid 4153)
    19026 kB: com.sharp.fxc.mor.tv (pid 4433)
    18384 kB: com.baidu.input_baidutv (pid 3770)
  ...

Total PSS by OOM adjustment:// according to oom List of categories PSS Memory value 
    75762 kB: Native
               28585 kB: surfaceflinger (pid 3022)
               12649 kB: mediaserver (pid 3041)
             ...
    53982 kB: Persistent
               33744 kB: system (pid 3424)
               15626 kB: com.android.systemui (pid 3605)
                ...
     9855 kB: Persistent Service
                9855 kB: com.android.bluetooth (pid 4246)
    41115 kB: Foreground
               38646 kB: com.sharp.fxc.fxcscreensaver (pid 4925 / activities)
                2469 kB: com.funshion.android.providers.tv (pid 3754)
     7893 kB: Visible
                5014 kB: com.droidlogic.tvinput (pid 3738)
                2879 kB: com.sharp.fxc.upgrade:settings (pid 4360)
    40963 kB: Perceptible
               ...
    16225 kB: A Services
               ...
    13663 kB: Previous
               ...
    84005 kB: B Services
               ...
    60362 kB: Cached
               ...
Total PSS by category:
    97586 kB: Dalvik
    75449 kB: .dex mmap
    53487 kB: Native
    47927 kB: .so mmap
    27484 kB: .oat mmap
    27113 kB: .art mmap
    24300 kB: EGL mtrack
    11208 kB: GL mtrack
     8475 kB: .apk mmap
     7624 kB: Dalvik Other
     6180 kB: Stack
     5600 kB: Unknown
     5020 kB: Other dev
     4608 kB: Other mmap
     1484 kB: .ttf mmap
      268 kB: Ashmem
        8 kB: .jar mmap
        4 kB: Cursor
        0 kB: Gfx dev
        0 kB: Other mtrack

Total RAM: 939320 kB (status normal)
 Free RAM: 495770 kB (60362 cached pss + 335716 cached kernel + 99692 free)
 Used RAM: 528291 kB (343463 used pss + 184828 kernel)
 Lost RAM: -84741 kB
     ZRAM: 72 kB physical used for 0 kB in swap (511996 kB total swap)
   Tuning: 128 (large 192), oom 106496 kB, restore limit 108333 kB (low-ram)
shell@LCD_42SFMTCCA_B:/ # 
Sequence type Sort explain
1 process PSS In progress PSS Sort the display from large to small , Each line shows a process ;
2 OOM adj PSS Native/System/Persistent/Foreground/Visible/Perceptible/A Services/B Services/Cached, Show the progress of each type separately
3 category PSS With Dalvik/Native/.art mmap/.dex map And so on PSS situation

List the detailed memory usage information of a process

shell@LCD_42SFMTCCA_B:/ # dumpsys meminfo tv.fun.test                          
Applications Memory Usage (kB):
Uptime: 8928752 Realtime: 8928752

** MEMINFO in pid 4744 [tv.fun.test] **
                   Pss  Private  Private  Swapped     Heap     Heap     Heap
                 Total    Dirty    Clean    Dirty     Size    Alloc     Free
                ------   ------   ------   ------   ------   ------   ------
  Native Heap      559      516        0        0     2028     1937       90
  Dalvik Heap      406      280        0        0     4684     3540     1144
 Dalvik Other      144      144        0        0                           
        Stack       64       64        0        0                           
    Other dev        4        0        4        0                           
     .so mmap      195       64        0        0                           
    .apk mmap       24        0       24        0                           
    .dex mmap      112        0      108        0                           
    .oat mmap      167        0        0        0                           
    .art mmap      552      312        0        0                           
   Other mmap        5        4        0        0                           
      Unknown       68       68        0        0                           
        TOTAL     2300     1452      136        0     6712     5477     1234
 
 Objects
               Views:        0         ViewRootImpl:        0
         AppContexts:        2           Activities:        0
              Assets:        2        AssetManagers:        2
       Local Binders:        3        Proxy Binders:        9
       Parcel memory:        2         Parcel count:        9
    Death Recipients:        0      OpenSSL Sockets:        0
 
 SQL
         MEMORY_USED:        0
  PAGECACHE_OVERFLOW:        0          MALLOC_SIZE:        0
 
shell@LCD_42SFMTCCA_B:/ #

To clearly see the detailed distribution of the memory occupied by the process . For example, you can see the so The amount of PSS Memory , Process occupied native The size of the heap , also Dalvik The size of the heap . When a memory leak occurs , This command can help us analyze the specific part of the memory leak .
notes :dumpsys meminfo Implementation principle reference Android Dumpsys command & example & principle
frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

public static void setSystemProcess() {
    
	 ServiceManager.addService(Context.ACTIVITY_SERVICE, m, true);
     ServiceManager.addService(ProcessStats.SERVICE_NAME, m.mProcessStats);
     ServiceManager.addService("meminfo", new MemBinder(m));
     ServiceManager.addService("gfxinfo", new GraphicsBinder(m));
     ServiceManager.addService("dbinfo", new DbBinder(m));
   	ServiceManager.addService("cpuinfo", new CpuBinder(m));
     ServiceManager.addService("permission", new PermissionController(m));
}

procrank
procrank The command can also output the memory usage information of all processes , And in accordance with the PSS Size sorting . But compared to the dumpsys meminfo,procrank The advantage is that , It can list the details of each process USS/RSS/PSS/VSS Information . One of the most valuable is USS Information , As I said before , It represents the process's exclusive physical memory , By observing its changes , You can find out whether the process has memory leaks .

1|shell@LCD_42SFMTCCA_B:/ # procrank                                           
  PID       Vss      Rss      Pss      Uss  cmdline
 3424   903556K   70368K   33549K   29660K  system_server
	...
 3754   812072K   26684K    2519K    1788K  com.funshion.android.providers.tv
 3039    13848K    5388K    2448K    1748K  /system/bin/drmserver
 4905   815172K   25216K    2393K    1528K  com.sharp.fxc.adservice:background
 4615   812080K   26588K    2261K    1552K  com.funshion
 3049    29428K    5960K    2130K    1440K  /system/bin/imageserver
 4557   799192K   24648K    2043K    1424K  com.sharp.fxc.fxctopactivity
    1      976K     660K     531K     456K  /init
 5131      828K     480K     476K     476K  /system/bin/mdnsd
 3019     2376K    1092K     383K     256K  /system/bin/lmkd
 9882     1068K     572K     294K     288K  procrank
 4136     1104K     616K     257K     244K  /system/bin/dhcpcd
 3042     1152K     568K     217K     208K  /system/bin/installd
 3020     1208K     512K     194K     184K  /system/bin/servicemanager
 2526      672K     264K     188K     116K  /sbin/ueventd
 3056     1560K     212K     184K     184K  /sbin/adbd
 3018     1576K     188K     184K     184K  /sbin/healthd
 4152      540K     268K     129K      84K  sh
 4140      536K     260K     121K      76K  sh
 4128      536K     260K     121K      76K  sh
 3036      536K     260K     121K      76K  /system/bin/sh
                           ------   ------  ------
                          377743K  304912K  TOTAL

RAM: 939320K total, 99404K free, 19160K buffers, 479084K cached, 5632K shmem, 42292K slab
shell@LCD_42SFMTCCA_B:/ # 

procmem pid
procrank The memory occupation of the whole system is given ,procmem Is to check the memory of a process . Be similar to cat /proc/pid/maps

1|shell@LCD_42SFMTCCA_B:/ # procmem 4744                                       
    Vss      Rss      Pss      Uss     ShCl     ShDi     PrCl     PrDi  Name
-------  -------  -------  -------  -------  -------  -------  -------  
  2052K     116K     116K     116K       0K       0K     116K       0K  /dev/ashmem/dalvik-main
194556K       0K       0K       0K       0K       0K       0K       0K  /dev/ashmem/dalvik-main
196608K       0K       0K       0K       0K       0K       0K       0K  /dev/ashmem/dalvik-main
  9608K    9500K     608K     360K    9140K       0K     360K       0K  /data/dalvik-cache/arm/system@framework@boot.art
 28268K    2848K     162K      12K    2836K       0K      12K       0K  /system/framework/arm/boot.oat
 23676K    1956K      79K      16K    1940K       0K      16K       0K  /system/framework/arm/boot.oat
     4K       0K       0K       0K       0K       0K       0K       0K  /system/framework/arm/boot.oat
  5020K    5020K     286K     160K    4860K       0K     160K       0K  /dev/ashmem/dalvik-zygote
     4K       4K       4K       4K       0K       0K       4K       0K  /dev/ashmem/dalvik-non
    52K      52K      52K      52K       0K       0K      52K       0K  /dev/ashmem/dalvik-non
 55344K       0K       0K       0K       0K       0K       0K       0K  /dev/ashmem/dalvik-non
  5116K       0K       0K       0K       0K       0K       0K       0K  /dev/ashmem/dalvik-non
    12K       8K       0K       0K       8K       0K       0K       0K  /system/bin/app_process32
     4K       4K       0K       0K       4K       0K       0K       0K  /system/bin/app_process32
    56K      44K       1K       0K      44K       0K       0K       0K  /system/bin/linker
     4K       4K       0K       0K       4K       0K       0K       0K  /system/bin/linker
     4K       4K       4K       4K       0K       0K       4K       0K  /system/bin/linker
  8188K      20K      20K      20K       0K       0K      20K       0K  [stack]
     4K       0K       0K       0K       0K       0K       0K       0K  [vectors]
-------  -------  -------  -------  -------  -------  -------  -------  
798860K   27320K    2592K    1772K   25508K      40K    2912K       0K  TOTAL

free /free -m
free The command can output the overall memory usage of the system ,free The output is as follows :

shell@LCD_42SFMTCCA_B:/ # free                                                 
             total         used         free       shared      buffers
Mem:        939320       835288       104032            0        19860
-/+ buffers:             815428       123892
Swap:       511996            0       511996
shell@LCD_42SFMTCCA_B:/ # free -m                                              
             total         used         free       shared      buffers
Mem:           917          819(used1)   97(free1)  0           19
-/+ buffers:                800 (used2) 117(free2)
Swap:          499      

The second and third lines are the memory information of the system , But there's a slight difference :

  1. The second line is from a system perspective , Physical memory and free memory currently in use , also shared and buffer Of memory . At this time total = used + free + shared + buffers
  2. The third line is from the perspective of application , Physical memory and free memory currently in use . Because for applications ,shared and buffer Of memory , All belong to available memory

cat /proc/meminfo
cat /proc/meminfo Command is compared to free, You can list the current memory division status of the system in more detail . Its output is as follows :


shell@LCD_42SFMTCCA_B:/ # cat /proc/meminfo
MemTotal:         939320 kB
MemFree:          103088 kB
MemAvailable:     518244 kB
Buffers:           20804 kB
Cached:           479120 kB
SwapCached:            0 kB
Active:           256840 kB
Inactive:         445056 kB
Active(anon):     145464 kB
Inactive(anon):    62868 kB
Active(file):     111376 kB
Inactive(file):   382188 kB
Unevictable:         728 kB
Mlocked:               0 kB
SwapTotal:        511996 kB
SwapFree:         511996 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        202700 kB
Mapped:           166100 kB
Shmem:              5632 kB
Slab:              42420 kB
SReclaimable:      16588 kB
SUnreclaim:        25832 kB
KernelStack:       15216 kB
PageTables:        16560 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      981656 kB
Committed_AS:   21394736 kB
VmallocTotal:    2097088 kB
VmallocUsed:      104360 kB
VmallocChunk:    1921892 kB
TotalCMA:         147456 kB
DriverCMA:          9744 kB
FreeCMA:              40 kB
shell@LCD_42SFMTCCA_B:/ # 

The real available memory of the system at this time is MemFree + Buffers + Cached

top
Same as PS , Check the memory usage of system processes in real time ,CPU Occupancy rate, etc

shell@LCD_42SFMTCCA_B:/ # top
User 1%, System 3%, IOW 0%, IRQ 0%
User 7 + Nice 0 + Sys 22 + Idle 561 + IOW 0 + IRQ 0 + SIRQ 4 = 594
  PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
10403  0   2% R     1   2492K    968K  fg root     top
 3424  1   1% S    80 903600K  67120K  fg system   system_server
 3025  1   0% S     6  52796K  10772K  fg root     /system/bin/tvserver
   22  1   0% S     1      0K      0K  fg root     suspend

copyright notice
author[Yin Jie enjoy your code],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270842498341.html