First, layout optimization

Second, rendering optimization

Third, memory leak optimization

Response speed optimization and ANR log analysis

Five, ListView optimization (GridView)

Sixth, Bitmap optimization

Seventh, thread optimization

Other performance optimization suggestions

First, layout optimization

1. Reduce the hierarchy of the layout file

1. The < include > tag

2. The < merge > tag

3.ViewStub: Inherits the View, which is 0 in width and height and does not participate in any layout or drawing process.

Meaning: load the required layout files on demand, some pages do not need to be loaded when the entire interface initialization, improve the performance of program initialization.

Second, rendering optimization

1.View’s onDraw method avoids doing a lot of work

One is that in onDraw, don’t create new local objects

The second is in onDraw, do not do time-consuming operations, otherwise it will cause the drawing process is not smooth.

Third, memory leak optimization

1. A static inner class holds an external class object, causing a memory leak. A static variable holds an object of the current class, causing a memory leak. (Static members of objects that hold external classes cause memory leaks.)

Workaround: Set static variables to null when the external class is destroyed.

2. Memory leaks caused by singletons: An Activity is passed into a singletons method, but is still referenced by the singletons when the Activity is closed, resulting in memory leaks.

Solution: You can use the Application context, etc.

3. Memory leak caused by property animation

Solution: Call the Cancel method.

4. Analysis tool: MAT


Response speed optimization and ANR log analysis

1. When ANR occurs in a process, a testament. TXT file is generated in the /daga/ ANR directory.

adb pull /data/anr/traces.txt .


Five, ListView optimization (GridView)

1. Use ViewHolder to reuse the control, not new an object each time.

2. Do not do time-consuming operations in getView, including other callback methods, and do not create objects, such as getSize.

3. Control the update frequency according to the sliding state of the list

Hardware acceleration.


Sixth, Bitmap optimization

1. Bitmapfactory. Options to sample according to the image, mainly using the inSampleSize parameter of Options in the sampling process.


Seventh, thread optimization

1. If you need to create more threads, use thread pools to reduce the performance overhead of thread creation and destruction.

2. Thread pools also control the maximum number of concurrent requests.


Other performance optimization suggestions

Avoid creating too many objects

2. Do not use enumerations too much. Enumerations take up more memory space than integers

3. Use static fianl for constants.

4. Use android-specific data structures like SpareArray, Pair, and Parclable

5. Use memory cache and disk cache

6. Use static inner classes whenever possible


Resources: Exploring the Art of Android Development