Realization of Caton Optimization of Android Optimization
- 2021-09-16 07:59:46
- OfStack
The Android system sends an VSYNC signal every 16ms to redraw the interface (Activity). The reason why it is 16ms is that the refresh rate set by Android is 60FPS (Frame Per Second), that is, the refresh rate of 60 frames per second, which is about 16ms refresh once.
This means that we need to complete the related operations of the next interface to be refreshed within 16ms, so that the interface can be refreshed and updated.
Suppose we need 24ms to update the background picture of the screen for this operation. When the system refreshes the interface at the first 16ms, the operation is not finished yet, so the picture cannot be drawn. When the system sends VSYNC information redrawing interface once every 16ms, the user will see the updated picture. That is to say, the refresh was seen after 32ms (not 24ms), which is the frame loss (ES23frame).
The feeling of losing frames to users is stuck, and if the operation is too complicated, more frames will be lost, resulting in the interface often in a stagnant state.
Catton cause
An overly complex layout
The interface performance depends on the rendering performance of UI, and the whole rendering process of UI is completed by CPU and GPU. CPU is responsible for Measure, Layout, Draw and other related operations of UI layout elements, while GPU is responsible for rasterization (rasterization) and drawing UI elements to the screen. If the layout level of UI is too deep, or there are complex operations in onDraw of custom controls, the related operations of CPU may be larger than 16ms, resulting in jamming.Over-drawing
Complex Operation of UI Thread
The complex operation of UI thread will cause UI to be unresponsive, resulting in ANR, but more often, it will cause UI to stagnate and jam, and ANR is the extreme of jam.Frequent GC
Optimization method
1. Reduce the number of refreshes
For example, if the progress is updated, it should be refreshed again if the progress changes, and the frequency should not be higher than the refresh frequency of the system.
2. Avoid unnecessary refreshes
If the control is not visible, there is no need to refresh it.
3. Avoid the influence of background threads
For example, list control, do not load pictures when sliding, you can stop loading pictures in sliding monitoring.
4. Local refresh
Such as DiffUtil of RecyclerView. You can customize View using the following two methods:
invalidate(Rect dirty);
invalidate(int left, int top, int right, int bottom);
5. Try to use attribute animation, which reduces its own redrawing. Finally, it is necessary to clear
StringBuilder, List, etc. pass in an appropriate parameter to specify the initial capacity at creation time to avoid the overhead of frequent expansion.
6. Turn on hardware acceleration
7. The principle can be seen in the brief introduction of Android hardware acceleration principle and implementation, and the small white text of understanding Android hardware acceleration principle
Application level
<application android:hardwareAccelerated="true" />
Activity Level
<activity android:hardwareAccelerated="true" />
Window Level
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
View level
// If it is software , will be View Draw to 1 A Bitmap ,
// Then still through hardware acceleration, Bitmap Draw to Canvas
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
LAYER_TYPE_NONE: Normal rendering mode, does not return 1 off-screen buffer, default.
LAYER_TYPE_HARDWARE: If the application uses hardware acceleration, the View will be rendered as a hardware texture in the hardware, and if the application is not hardware accelerated, the effect will be the same as LAYER_TYPE_SOFTWARE.
LAYER_TYPE_SOFTWARE: This View is rendered as an Bitmap by software.
Check whether hardware acceleration is turned on
view.isHardware-Accelerated();
Canvas.isHardwareAccelerated();
If you want to process long Chinese text in View, you need to turn off hardware acceleration. Because each Chinese code is not 1, the cache effect is not ideal.
Monitoring
Chapter 2.8 of "Best Practices for Performance Optimization of Android Applications" mainly utilizes Printer in MainLooper. BlockCanary open source library StrctModeANR
View for Activity: No response for 5 seconds
BroadcastReceiver: No response for 10 seconds
Service: No response for 20 seconds
ANR, the system will generate a file of traces. txt under/data/anr/. Export it locally with the adb command
$adb pull data/anr/traces.txt ~/Desktop
ANR can be generated by time-consuming operations by UI threads and insufficient memory due to memory leaks.