Performance optimization This block is divided into UI performance optimization, memory optimization, database optimization, network optimization, power consumption optimization and so on. can be from 1. How to find the problem, 2. How to solve the problem, 3. The solution to the effect of the comparison, these several aspects to describe. To give a simple example of--ui optimization, what problems can arise from the UI (the lag is not smooth), how to find the problem (mobile developer permissions >gpu over-draw discovery hierarchy problem, TraceView CPU usage analysis), how to solve the problem (lower levels, custom view drawing problems, etc.) ), the performance is again compared after resolving the problem.
First, UI optimization
A. Reasonable selection of Relativelayout, LinearLayout, Framelayout,relativelayout will let the child view call 2 times onmeasure, and the layout is relatively complex, onmeasure relatively complex, The efficiency is low, linearlayout in weight>0 also let the child View call 2 times onmeasure. LinearLayout weight measurement allocation principle.
B. Using labels
C. Reduce the layout level, can be viewed through the Mobile developer Options >gpu Transition, general level control within 4 levels, more than 5 layers need to consider whether to re-layout.
D. When customizing the view, override the OnDraw () method, do not create a new object in the method, or it is easy to trigger the GC, resulting in degraded performance
E. Use the ListView to reuse Contentview and use holder to reduce Findviewbyid load view.
F. Removing unnecessary backgrounds, GetWindow (). setbackgrounddrawable (NULL)
G. Using TextView leftdrawabel/rightdrawable instead of Imageview+textview layout
Second, memory optimization
Mainly to avoid the performance degradation caused by oom and frequent triggering to the GC
A.bitmap.recycle (), Cursor.close,inputstream.close ()
B. When loading bitmap, load bitmap According to view size, choose insamplesize,rgb_565 encoding method reasonably; use LRUCache cache
C. Use static inner class +weakreference instead of inner classes, such as handler, threads, Asynctask
D. Use thread pooling to manage threads and avoid new threads
E. Use a singleton to hold the context, need to remember to release, or use the global context
F. Static collection objects note release
G. Property animations cause memory leaks
H. Use of WebView, activity.ondestory need to be removed and destroyed, Webview.removeallviews () and Webview.destory ()
Standby: Use Leakcanary to detect memory leaks
Third, the response speed optimization
If the activity fails to respond to screen touch events and keyboard input events within 5 seconds, the ANR will appear, and Broadcastreceiver will appear anr,serve20 seconds if no action has been performed within 10 seconds to avoid ANR, You can turn on child threads to perform time-consuming operations, but child threads cannot update the UI, so you need to handler messaging mechanisms, Asynctask, intentservice for thread communication.
Preparation: When ANR occurs, ADB pull data/anr/tarces.txt combines log analysis
Iv. Other performance optimizations
A. Constants using the static final decoration
B. Use Sparsearray instead of HashMap
C. Using thread pooling to manage threads
D.arraylist traversal uses the regular for loop, LinkedList uses the foreach
E. Do not overuse enumerations, enumerate large memory spaces than integers
F. Concatenation of strings takes precedence over StringBuilder and StringBuffer
G. Database storage using BULK INSERT + transaction
Overview of Android Performance optimization issues