Top command, ADB shell Dumpsys meminfo, Memory Profiler, LeakCanary, MAT
1. top
The top command is a commonly used performance analysis tool in Linux. It can display the resource usage of each process in the system in real time.
Check the usage of the top command
$ adb shell top --help
usage: top [-Hbq] [-k FIELD,] [-o FIELD,] [-s SORT] [-n NUMBER] [-m LINES] [-d SECONDS] [-p PID,] [-u USER,]
Show process activity in real time.
-H Show threads
-k Fallback sort FIELDS (default -S,-%CPU,-ETIME,-PID)
-o Show FIELDS (def PID,USER,PR,NI,VIRT,RES,SHR,S,%CPU,%MEM,TIME+,CMDLINE)
-O Add FIELDS (replacing PR,NI,VIRT,RES,SHR,S from default)
-s Sort by field number (1-X, default 9)
-b Batch mode (no tty)
-d Delay SECONDS between each cycle (default 3)
-m Maximum number of tasks to show
-n Exit after NUMBER iterations
-p Show these PIDs
-u Show these USERs
-q Quiet (no header lines)
Cursor LEFT/RIGHT to change sort, UP/DOWN move list, space to force
update, R to reverse sort, Q to exit.
Copy the code
Run the top command to display the process information to explain the meanings of each field in the process information
^[[41;173RTasks: 754 total, 1 running, 753 sleeping, 0 stopped, 0 zombie Mem: 6.5g total, 5.4g used, 145m free, 69m buffers Swap: 2.5g total, 789M used, 1.5g free, 2.4g cached 800% CPU 100% User 3% Nice 54% SYS 641% Idle 0% IOW 3% IRq 0% SIRq 0%host PID user PR NI VIRT RES SHR S[%CPU] %MEM TIME+ ARGS 15962 u0_a894 10-10 6.6g 187M 76M S 75.6 3.8:16.55 Asia.bluepay. cl+ 785 system-2-8 325M 13M 7.6m S 29.7 0.2 84:03.91 SurfaceFlinger 25255 shell 20 0 35M 2.6m 1.6m R 21.6 0.0:00.16 top-n 1 739 system-3-8 177M 3.6m 2.6m S 10.8 0.0 16:00.36 Android. Hardwar + 16154 u0_i9086 10-10 1.3g 40M 19M S 5.4 0.6 0:46.18 com.google.andr+ 13912 u0_a87 20 0 17G 75m 75m S 5.4 3.4 23:56.88 com.tceh.mm 24789 root rt-2 0 0 D 2.7 0.0 0:01.36 [mdss_fb0] 24704 root 20 0 [kworker/u16:12] 20096 u0_a94 30 10 6.1g 137M 53M S 2.32.3 0:31.45 com.xiaomi 18-2 8.7g 407M 267M S 2.7 7.1 191:11.32 System_server 744 SYSTEM RT 0 1.2g 1.6m 1.4m S 2.7 0.0 72:22.41 Android. Hardwar + 442 root RT 00 0 S 2.7 0.0 5:59.68 [cfinteractive] 291 root -3 00 0 S 2.7 0.0 5:00.17 [kgsl_worker_th+ 10 root 20 0 0 S 2.7 0.0 1:55.84 [rcuop/0] 7 root 20 0 0 S 2.7 0.0 2:46.82 [rcu_preempt] 25186 [kgsl_worker_th+ 10 root 20 0 0 S 2.7 0.0 1:55.84 [rcuop/0 Shell 20 0 34M 1.9m 1.4m S 0.00.000.71 logcat -v long + 25181 root 20 00 0 S 0.00.00.000.00 [kworker/2:3] 25137 Root 20 00 0 S 0.0 0.0 00.00 [kworker/1:3] 25118 system 20 0 5.2g 83M 54M S 0.0 1.4 0:01.05com.android.set + 24946 U0_a57 20 0 5.1g 60M 60M S 0.0 1.0 00.82 com.xiaomiCopy the code
Line 1: Process information
- Total: 754
- Running status: 1
- Sleeping: 753
- Stopped state: 0
- Zombie status: 0
Line 2: Memory information
- 5.5g Total: indicates the total physical memory capacity
- 5.4g used: Indicates the amount of memory in use
- 165M Free: Indicates the free memory capacity
- 76M buffers: Amount of memory in the cache
Line 3: Swap partition information
- 2.5g total: indicates the total number of switching areas
- 789M Used: Indicates the size of the used swap area
- 1.7GB free: indicates the free swap area size
- 2.4GB cached: Size of the cached swap area
During memory monitoring, you can monitor the used size of the SWAP partition. If the value changes continuously, the kernel is constantly exchanging data with swap, and the memory is insufficient.
Line 4: CPU information
- 800% CPU: 8-core CPU
- 100%user: CPU usage of user processes
- 3% NICE: the proportion of processes with a negative priority value
- Sys: CPU usage of kernel processes
- 641% Idle: Indicates the proportion of other waiting time except I/O waiting time
- 0% IOW: ratio of I/O wait time
- 3% IRQ: proportion of hard interrupt time
- 0% SIRQ: proportion of soft interrupt time
Line 5 and below: Monitoring the status of each process
- PID: indicates the ID of a process
- USER: indicates the USER to which the process belongs
- PR: indicates the process priority
- NI: nice value. A negative value indicates a high priority, and a positive value indicates a low priority
- VIRT: total virtual memory used by a process. VIRT=SWAP+RES
- RES: size of physical memory used by the process that has not been swapped out. RES=CODE+DATA
- SHR: indicates the size of shared memory
- S: indicates the process status
- %CPU: CPU usage ratio since last update
- %MEM: physical memory usage
- TIME+ : indicates the total CPU TIME of the process, expressed in 1/100 second
- ARGS: indicates the process name
2. dumpsys meminfo
Let’s start with the concept of the four most important memory metrics in Android
indicators | The full name | meaning | equivalent |
---|---|---|---|
USS | Unique Set Size | Exclusive physical memory | Process-exclusive memory |
PSS | Proportional Set Size | Physical memory is actually used | PSS = USS + contains shared library memory proportionally |
RSS | Resident Set Size | Physical memory is actually used | RSS = USS + contains shared library memory |
VSS | Virtual Set Size | Virtual memory consumption | VSS = Memory occupied by process (including virtual consumption) + shared library (including proportional allocation) |
We mainly use USS and PSS to measure the memory usage of a process
The dumpsys meminfo command shows the overall memory of the system, sorted by process
$ adb shell dumpsys meminfo Applications Memory Usage (in Kilobytes): Uptime: 168829244 Realtime: Total PSS by Process: 272,029K: System (PID 2272) 234,043k: Com.0700.mm (pid 13912 / activities) 185,914K: com.android.systemui (PID 13606) 185,914K: Com.tencent. Mm: AppBrand0 (pid 5563) 101,526K: com.tencent. Mm: Toolsmp (pid 9287) 96,645K: com.miui.home (pid 15116 / activities) ... Total PSS by OOM Adjustment: 411,619K: Native 62,553K: [email protected] (pid 730) : 21630 K logd (pid 579) : 16179 K surfaceflinger (pid 785)... 272,029K: System 272,029K: System (PID 2272) 361,942K: Persistent 185,914K: com.android.systemui (PID 13606) 37,917K: Com.android. Phone (PID 2836) 23,551 K: com.miui.Contentcatcher (PID 3717)... 36,142K: Persistent Service 36,142K: com.android. Bluetooth (PID 26472) 101,198K: Foreground 72,743K: Com. Beautiful miui. Securitycenter. Remote (pid 4125) : 28455 K com. Android. The Settings (pid 30919 / activities) 338088 K: The Visible 96645 K: com. Beautiful miui. Home 15116 / activities (pid) 46939 K: com. Beautiful miui. Personalassistant (pid 31043), 36491 (K) : com.xiaomi.xmsf (pid 4197) ... 47703 K: Perceptible 17826 K: com. Xiaomi. Metoknlp (pid 4477) : 10748 K com. Lbe. Security. The beautiful miui (pid 5097), 10528 (K) : Com. Xiaomi. Location. Fused (pid 4563) : 8601 K com. Beautiful miui. Mishare. Connectivity (pid 4227) : 13088 K Perceptible Low 13088 K: Com.miui.analytics (pid 19306) 234,043K: Backup 234,043k: com.tencent. Mm (pid 13912 / Activities) 22,028k: A Services 22,028K: com.miui.PowerKeeper (PID 29762) 198,785K: Previous 33,375K: Com. Android. Quicksearchbox (pid 31023), 23278 (K) : Com. Google. Android. Webview: sandboxed_process0: org. Chromium. Content. app. SandboxedProcessService0:0 (pid 16154), 171434 (K) : B Services 45,962K: com.tencent. Mm :push (pid 14095) 31,514K: com.tencent. Mobileqq :MSF (pid 12051) 22,691K: com.xiaomi.mi_connect_service (pid 22821) ... 538,062K: Cached 107,294K: com.tencent. Mm: AppBrand0 (pid 5563) 101,526K: com.tencent. Mm: Toolsmp (pid 9287) 72,112K: com.tencent.mm:tools (pid 9187) ... // Total PSS by category: 692,040K: Native 328,722k: Dalvik 199,826k:.art mmap 129,981K: .oat mmap 126,624K:.dex mmap 124,509K: Unknown 92,666K:.so mmap 68,189K: Dalvik Other 53,491K:.apk mmap 44,104K:.dex mmap 124,509K: Unknown 92,666K:.so mmap 68,189K: Dalvik Other 53,491K:.apk mmap 44,104K: Gfx dev 24,099 K: Other mmap 24,960K:.jar mmap 24,956 K: Ashmem 24,700 K: Stack 3,368K: Other dev 450K:.ttf mmap 4K: Cursor 0K: EGL Mtrack 0K: GL Mtrack 0K: Other Mtrack // Overall memory usage of the mobile phone Total RAM: 5,862,068K (Status normal) Free RAM: (538,062K cached PSS + 3189,244 K cached kernel + 0K cached ion + 67,340K free) Used RAM: 2,657,473K (2,208,101K used PSS + 449,372K kernel) Lost RAM: Tuning: 219,996K physical use for 826,852K in swap (total swap) 256 (large 512), oom 322,560K, restore limit 107,520K (high-end-gFX)Copy the code
To view the memory information of a single process, run the following command
adb shell dumpsys meminfo [pid | packageName]
Copy the code
Let’s check the memory information of wechat
$ adb shell dumpsys meminfo com.tencent.mm
Applications Memory Usage (in Kilobytes):
Uptime: 169473031 Realtime: 1466413783
** MEMINFO in pid 13912 [com.tencent.mm] **
Pss Private Private SwapPss Heap Heap Heap
Total Dirty Clean Dirty Size Alloc Free
------ ------ ------ ------ ------ ------ ------
Native Heap 51987 51924 0 61931 159044 139335 19708
Dalvik Heap 74302 74272 8 2633 209170 184594 24576
Dalvik Other 10136 10136 0 290
Stack 84 84 0 8
Ashmem 2 0 0 0
Gfx dev 8808 8808 0 0
Other dev 156 0 156 0
.so mmap 9984 984 7436 8493
.jar mmap 1428 0 560 0
.apk mmap 2942 0 1008 0
.ttf mmap 1221 0 1064 0
.dex mmap 31302 44 30004 528
.oat mmap 2688 0 232 0
.art mmap 2792 2352 40 3334
Other mmap 6932 2752 632 0
Unknown 4247 4232 4 7493
TOTAL 293721 155588 41144 84710 368214 323929 44284
App Summary
Pss(KB)
------
Java Heap: 76664
Native Heap: 51924
Code: 41332
Stack: 84
Graphics: 8808
Private Other: 17920
System: 96989
TOTAL: 293721 TOTAL SWAP PSS: 84710
Objects
Views: 623 ViewRootImpl: 1
AppContexts: 9 Activities: 1
Assets: 12 AssetManagers: 0
Local Binders: 198 Proxy Binders: 183
Parcel memory: 46 Parcel count: 185
Death Recipients: 125 OpenSSL Sockets: 1
WebViews: 0
SQL
MEMORY_USED: 156
PAGECACHE_OVERFLOW: 13 MALLOC_SIZE: 117
DATABASES
pgsz dbsz Lookaside(b) cache Dbname
4 28 46 721/26/4 /data/user/0/com.tencent.mm/databases/Scheduler.db
Asset Allocations
: 409K
: 12K
: 1031K
Copy the code
- Generally, we need to pay attention to the size of the Java Heap and Native Heap. If the size continues to rise, there may be memory leakage.
attribute | The memory of |
---|---|
Java Heap | Dalvik Heap的Private Dirty + .art mmap的Private Dirty&Private Clean |
Native Heap | Private Dirty of Native Heap |
Code | So mmap +.jar mmap +.apk mmap +.ttf.mmap +.dex.mmap +.oat Private Dirty&Private Clean of mmap |
Stack | The Stack of Private Dirty |
Graphics | Gfx dev + EGL mtrack + GL mtrack的Private Dirty&Private Clean |
- Exceptions of Views, Activities, AppContexts in Objects can be used to determine memory leakage. For example, if the Activites is 0 after exiting the application, then some Activities are not destroyed.
3. Memory Profiler
Memory Profiler is a component in the Android Profiler that charts application Memory usage in real time, identifies Memory leaks and jitter, and provides the ability to capture heap dumps, enforce GC, and track Memory allocation.
Android Profiler official documentation
4. Leak Canary
Very easy to use memory leak detection tool, for Activity/Fragment memory leak detection is very convenient.
Square open source official website address, the principle of a separate analysis.
5. MAT
MAT, short for Memory Analyzer Tool, is a very comprehensive analysis tool that is relatively complex to use. There are many good articles on installation and configuration that will not be covered here, but will be covered in specific cases.
Android Memory Optimization – Memory leak detection using the Profile and MAT tools
Use Android Studio and MAT for memory leak analysis
Efficient method for memory problem analysis
- Access LeakCanary, monitor the release of all activities and fragments, run all functions of the App to see if any memory leaks are found, analyze the reference chain to find and solve the problem, and repeat this until LeakCanary cannot be detected.
- Adb shell dumpsys meminfo command to check the number of Views and Activities of Objects after exiting the interface, especially after exiting the App.
- Open the Android Studio Memory Profiler, open and close the page several times, click GC, and if Memory is not restored to its previous value, a Memory leak may have occurred. Click the heap dump button next to the Profiler’s trash can icon to view the memory stack and find the Activity being tested by package name. If there are multiple instances, a memory leak is likely to occur.
- Dump memory snapshot files for suspicious pages and open them with MAT after conversion for targeted analysis.
- Observe the peak and jitter of each page in the Memory Profiler for specific analysis.
- Open “Do not retain background activity” in the developer option, App runs for a period of time back to the background, trigger GC, dump memory snapshot. MAT analyzes whether static content can be optimized, such as image cache, singleton, memory cache, etc.
The resources
Deep dive into Android Memory optimization (Purgatory level – Part 1)
Vomit blood to arrange! Dig deep into Android memory optimization
Android memory optimization summary & Practice