Summary of Android performance optimization

1. Reasonable management of memory
1. Use the service sparingly: If the application needs to use the Service to perform background tasks, please be sure to let the Service run only when the task is being executed. In addition, when the service is stopped after the task is executed, be careful of the memory leak caused by the failure of the service to stop. In order to be able to control the life cycle of the Service, the best solution officially recommended by Android is to use IntentService. The biggest feature of this Service is that it will automatically stop when the background task is executed, thus avoiding the possibility of Service memory leaks to a great extent. .
2. Release memory when the interface is not visible
3. Avoid wasting memory on the bitmap: When reading a Bitmap image, one thing must be paid attention to, that is, do not load unnecessary resolutions.
4. Use optimized data sets: such as SparseArray, SparseBooleanArray, and LongSparseArray, etc. Using these APIs can make our programs more efficient.
5. Use object-oriented programming sparingly: Because the abstract programming method is more object-oriented, and the maintenance and scalability of the code will be improved. However, using abstraction on Android will bring additional memory overhead, because the abstract programming method needs to write additional code, although these codes cannot be executed at all, but they are also mapped into memory, which not only takes up more memory , the performance efficiency will also be reduced.
6. Try to avoid the use of dependency injection frameworks: In order to search for annotations in the code, the framework usually needs to go through a long initialization process, and may also load some objects that you do not use into memory.
7. Simplify your code with ProGuard
8. Use multiple processes
2. High-performance encoding optimization
1. Avoid creating unnecessary objects: Because creating an object means that the garbage collector needs to recycle an object, and these two steps are time-consuming.
  1. Static is better than abstract:
  2. Use static final modifier for constants
  3. Use enhanced for loop syntax
3. Layout optimization
  1. Reuse layout files: <include>
  2. <merge>: The <merge> tag is used instead, which means that when there is any place to include the layout, the content contained in the <merge> tag will be directly filled into the include position, and no additional extras will be added. layout structure.
  3. Load layouts only when needed
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324621213&siteId=291194637