Performance optimizations: Layout optimization, drawing optimization, memory leak optimization, response speed optimization, listview optimization, bitmap optimization, threading optimization, and more1. Layout optimizationThought: Minimize the level of layout, draw less work, high performance operation: 1. Delete unused controls and levels in the Layout 2. Choose the use of low performance viewgroup, such as LinearLayout, framelayout,relativelayout function complex, priority to use the first two 3. With <include> tags, <merge> tags and V Iewstub. <include> tags are mainly used for layout reuse,<merge> labels and <include> tags, it can reduce the level of layout viewstub provides on-demand load function, inherit the view, Very lightweight and wide-height is 0, itself does not participate in any layout and drawing process.
2. Drawing optimizationThought: Drawing optimization refers to the OnDraw method of view to avoid performing a large number of operations. Action: Do not create a new local object in 1.onDraw, because the OnDraw method may be called frequently, resulting in a large number of temporary objects in a flash, which not only takes up too much memory but also causes the system to be more frequent GC, which reduces the execution efficiency of the program. The 2.onDraw method does not take time-consuming tasks, nor does it perform thousands of cyclic operations.
3. Memory Leak optimization1. Avoid writing code with memory leaks 2. Some analysis tools such as mat are available to identify potential memory leaks and resolve them. Scenario: 1. Memory leaks due to static variables 2. Memory leaks caused by a singleton mode 3. Property animations cause memory leaks: Property animations after android3.0, infinite loop playback needs to call Animator.cancel () in OnDestroy Stop an animated Android memory leak:1. Memory leaks due to resource object not shutting down2. When constructing adapter, no cached Convertview is used3.Bitmap object is not in use call recycle () to free memory4. Try to use the context of application to replace the context associated with activity5. Memory leaks due to registration not canceled6. Memory leaks caused by objects not cleaned up in the collection
4. Response speed optimization and ANR log analysisThe core idea of response speed optimization is to avoid time-consuming operations in the main thread and to perform time-consuming operations asynchronously.For example, in the main thread to do a very time-consuming operation, such as download, IO exception, etc. potentially time-consuming operations, such asNetwork or database operations, or high time-consuming calculations such as changing the bitmap size, it should be done in the thread of the strand (or in the case of a database operation, by way of an asynchronous request)
Activity If you cannot respond to screen touch events or keyboard input events within 5 s, the Anrbroadcastreceiver 10s will appear after the operation has not completed the ANR
After a process has been ANR, a file is created in the/DATA/ANR directory Traces.txt
second: How to avoid ANR? 1, run in the main line thread any method to do as little as possible to do things. In particular, activity should create as little as possible in its critical life-cycle methods, such as OnCreate () and Onresume (). (You can do something by re-opening the child thread and then using handler+message, such as updating the UI in the main thread, etc.)
2, the application should avoid doing time-consuming operation or calculation in Broadcastreceiver. Instead of doing these tasks in a sub-thread (because of the short life cycle of broadcastreceiver), the application should start a Service if responding to intent broadcasts requires a time-consuming action. (note here that you can start the service in the broadcast recipient, but you cannot start broadcasereciver in the service, for reasons that follow, this is not the focus of this article)
3. Avoid starting an activity in intent receiver because it creates a new screen and grabs the focus from the program that the current user is running. If your application needs to show the user what to do in response to intent broadcasts, you should use Notification Manager to implement
Summary: The ANR exception is also in the program itself often encountered problems, the main solution to their own most commonly used is not to do time-consuming operations in the main thread, but should be placed in sub-threads to achieve, such as the use of Handler+mesage way, Or sometimes you need to do some time-consuming operation that interacts with the network to take the Asyntask asynchronous task (its underlying handler+mesage is actually a thread pool) and so on, updating the UI in the main thread.
5.ListView and bitmap optimizationListView Optimization: First adopt viewholder and avoid the getViewperform time-consuming operations insecond, depending on the sliding state of the list to control how often the task executes.Finally, you can try to turn on hardware acceleration to make the ListView slide smoother
Bitmap optimization: Mainly through the bitmapfactory.options to sample the picture as needed.
6. Threading Optimizationuse a thread pool to avoid the existence of a large number of threads in the program
7. Some Performance tuning recommendationsavoid creating too many objectsDo not use enumerations too much, the enumeration consumes more memory space than integer large constants using static final to decorate with some Android-specific data structures, such as sparsearray and pair, they have better performance. Proper use of soft references and weak drinking use memory caches and disk caches as much as possible with static internal classes, which avoids potential memory leaks due to internal classes
The Mat tool for memory leak analysis analyzes leak issues
Android Performance optimization