In Android platform development, how to analyze the problem of high CPU usage and device overheating?
This document aims to help Android developers using ZEGO SDK analyze CPU performance issues, as such issues often lead to App UI stuttering, mobile device overheating, audio and video not being real-time (large latency) and other issues, affecting customer experience.
To improve customer experience and service, this document analyzes the problem of high CPU usage, helping developers locate the root cause of the problem and confirm whether it is a performance issue caused by ZEGO SDK.
Process Analysis
Execute the top command to check whether the App's CPU usage is too high and the corresponding PID (Process Identification).
For example: the current App's CPU usage is 106%, and the PID is 27056.

Thread Analysis
Execute the top -H -p command, sort by %CPU usage, and find the CPU usage of each thread under the corresponding PID, which includes system-level, C/C++ (lib layer), and Java layer self-developed threads.
For example: the execution result of the top -H -p 30959 -O Tid command is as follows:

From this, you can see that the main thread in the com.zegodemo.dynamicfunction process consumes higher CPU.
Profiler Tool
The Profiler tool that comes with Android Studio can also directly view threads with high CPU usage.
-
Install the Debug version of Android Studio, connect to the mobile device, open the Profiler tool, and select the package name corresponding to the App to be analyzed.

-
Click CPU to enter the CPU analysis page.
- The upper part shows the current App's CPU usage and the CPU usage of other processes on the phone.

- The lower part shows all current threads and the CPU time slices occupied by the threads. CPU usage refers to the CPU time occupied by a thread during a period of time, that is, the ratio of the thread's running state time to the total time.
The length of the green bar represents the thread's running state time. If there are longer green bars or very frequent green bars appearing in a period of time, it indicates high CPU usage.

For example: the main thread's running state time shows a large number of green bars. When you move the mouse to the green bar, you can see that the running state time often exceeds 1s, which suggests that the main thread has time-consuming operations.

Function Details
After locating the specific time-consuming thread, further analyze the operations within the target thread, and analyze in detail according to the level of the thread that needs to be analyzed.
Java Layer
-
If you need to debug Java layer CPU issues in the main thread or other upper-level Java threads, select:
Sample Java Methods, click Record, and reproduce the problem of high CPU usage.
-
Then click Stop to stop. Profiler will automatically parse the thread Java method calls during this period.

-
According to the Java method name, view the thread usage rate. For example: the following figure shows that the onClick length bar in the main thread clearly occupies a longer CPU time.
Click the function in the main thread, and the right panel will display the longest execution times of the function within the Record time range. Function calls to system APIs are displayed in orange, function calls to the application's own functions are displayed in green (key inspection), and function calls to third-party APIs (including Java language APIs) are displayed in blue.


C/C++ Layer
If the C/C++ layer has high CPU usage, select Sample C/C++ Functions.

Query the CPU resource usage in the ZEGO SDK's so. Common threads in Zego lib.so are as follows:
| Thread name | Function |
|---|---|
| decoder | Decoding thread (audio/video) |
| CWelsTaskThread | H.264 hierarchical encoding thread |
| audio_encode | Audio encoding thread |
| Vcap_enc | Video capture encoding thread |
| avert-play-udp | Stream pulling thread |
| cap_thread | Capture thread |
| video_filter | External filter thread |
If developers encounter problems with the above threads, please save the C/C++ trace file, logcat information, export ZEGO SDK, and save the log in the SD card, and contact ZEGOCLOUD Technical Support for processing.

