The Android system will issue a VSYNC signal every 16ms. The reason is 16ms is that the refresh rate set by Android is 60FPS (Frame Per Second), which is 60 frames per second, which is about 16ms refresh.
This means that we need to complete the relevant operations of the interface to be refreshed within 16ms so that the interface can be refreshed and updated.
Suppose we need 24ms to update the background image of the screen to do this operation. When the system refreshes the interface at the first 16ms, since the operation has not yet ended, the image cannot be drawn. Only when the system sends the VSYNC information redraw interface again after 16ms, the user will see the updated picture. In other words, I saw this refresh after 32ms (not 24ms), which is the dropped frame.
The feeling of frame dropping gives the user is that it is stuttering, and if the operation is too complicated, there will be more frame drops, resulting in the interface being often in a stagnant state.
Causes of lag
Too complicated layout
- The interface performance depends on the rendering performance of the UI. The entire process of UI rendering is completed by the CPU and GPU. The CPU is responsible for the execution of Measure, Layout, Draw and other related operations of UI layout elements, and the GPU is responsible for rasterization and draw UI elements on the screen.
- If the UI layout level is too deep, or there are complex operations in the onDraw of the custom control, the CPU's related operations may be greater than 16ms, resulting in lag.
Overdraw
Complex operations of UI threads
- Complex operations of UI threads will cause the UI to be unresponsive and cause ANR, but it will cause UI responses to stagnate and stutter. ANR is the ultimate lag.
Frequent GC
Optimization method
1. Reduce the number of refreshes
For example, when the progress is updated, the progress must be refreshed before the progress changes, and ensure that the frequency must not be higher than the system refresh frequency.
2. Avoid non-essential refreshes
If the control is not visible, no refresh is required.
3. Avoid the influence of background threads
For example, if the list control is used, do not load the image when sliding, you can stop the image loading in the slide monitor.
4. Partial refresh
Such as DiffUtil of RecyclerView. You can use the following two methods for customizing the View:
invalidate(Rect dirty); invalidate(int left, int top, int right, int bottom);
5. Try to use attribute animation, which reduces its own redraw. Finally clear
StringBuilder, List, etc. pass in an appropriate parameter when creating to specify the initial capacity to avoid the overhead of frequent expansion.
6. Turn on hardware acceleration
7. The principle is visibleIntroduction to Android hardware acceleration principle and implementation,A newbie article about understanding the principle of Android hardware acceleration
Application Level
<application android:hardwareAccelerated="true" />
Activity level
<activity android:hardwareAccelerated="true" />
Window Level
getWindow().setFlags(.FLAG_HARDWARE_ACCELERATED, .FLAG_HARDWARE_ACCELERATED);
View Level
// If it is software, the View will be drawn to a Bitmap.// Then, it is still drawing Bitmap to Canvas through hardware acceleration(View.LAYER_TYPE_SOFTWARE, null);
- LAYER_TYPE_NONE: Normal rendering method, no off-screen buffer will be returned, default value.
- LAYER_TYPE_HARDWARE: If this application uses hardware acceleration, this View will be rendered as a hardware texture in the hardware. If the application is not accelerated by hardware, its effect is the same as LAYER_TYPE_SOFTWARE.
- LAYER_TYPE_SOFTWARE: This View is rendered as a Bitmap by software.
Check if hardware acceleration is enabled
-Accelerated(); ();
If you want to process long Chinese text in the View, you need to turn off hardware acceleration. Because each Chinese code is different, the cache effect is not ideal.
monitor
- Chapter 2.8 of "Best Practice for Android Application Performance Optimization" mainly utilizes Printer in MainLooper.
- BlockCanary open source library
- StrctMode
ANR
Activity's View: 5 seconds no response
BroadcastReceiver: 10 seconds no response
Service: No response in 20 seconds
When ANR, the system will generate a file and place it under /data/anr/. Export it locally via the adb command
$adb pull data/anr/ ~/Desktop
UI threads can perform time-consuming operations and memory leakage may result in insufficient memory.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.