SoFunction
Updated on 2025-03-11

Android development performance optimization summary

1. Loading

Preload: 1. Reflect the Reflect information of the injection framework, and preload it into the cache through multiple threads within the Application. 2. Resource preloading

Lazy loading: Lazy loading 2. Lazy loading of resources

2. Cache

1. Http cache, elimination time

2. Image cache, bitmap compression, Lru elimination, persistent secondary cache

3. Reflection injection framework Reflect information cache to prevent multiple reflection operations

3. Asynchronously prevent Anr

1. Avoid doing too many time-consuming operations in UI threads, IntentReceiver > 10s Anr

2. Use read and write locks more for concurrent operations and use synchronized less. Android virtual machine Art has not yet optimized CAS for synchronized until Android 6.0, and synchronized performance in hotspot is OK.

3. Use a thread pool and combine asynchronous loading of images to achieve thread reuse when a large number of child threads is required.

4. Cancel the task when it is not needed, such as canceling the loading task when the activity is ended.

4. Prevent OOM

1. Bitmap: lazy loading, LRU cache, Bitmap compression (according to ImageView size), and load large images in chunks.

2. Large files, such as txt, etc., are loaded in segments.

3. ListView&GridView is reused and optimized. Use ViewHolder, setTag

5. View optimization

1. Optimize layout hierarchy, layout reuse, and make good use of include, merge and other labels.

2. Avoid overdraw, reduce overlap of elements and layouts, enable DebugGPU overdraw debugging, and use Hierarchy Viewer. Simply put, it is the waste of performance of the main thread caused by multiple meaningless calls to onDraw, which may be the decrease in frame count.

3. OnDraw() optimization avoids time-consuming operations such as new Paint() in onDraw. Only required code should be retained in onDraw.

six. Memory leak

For memory analysis, you can also use the leakcanary library.

Avoid HashCode mutable keys

3. Handle Context objects with caution, because their life cycle is managed by Framework and try to avoid private ownership.

4. Be careful of static variables and clear discarded objects stored in static in time.

5. Release resource objects such as File, Cusor, Stream, Socket in real time, because the jni layer holds references to its java layer.

6. Non-static anonymous internal class. Commonly found in Runnable and Handler.

7. Use a Map with excellent performance

1. Thread-safe map, ConcurrentHashMap, uses segmented locks to optimize concurrency performance.

2. Using ArrayMap, the space performance is high. It is not a data structure that adapts to big data, and is slower than traditional HashMap, because the search method is a dichotomous method, and when you delete or add data, it will readjust the space. When using a large amount of data, the efficiency is not obvious, less than 50%.

3. SparseArray, dichotomy plus can only use int as key, which has higher performance.

The above is the summary of Android development performance optimization introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!