Android startup optimization ideas and directions

Optimization plan:

1. Code optimization (long-term)
  • Code logic optimization, optimization direction
  1. Avoid long methods, preferably no more than 50 lines
  2. Avoid multiple if-else statements, try to use ternary operations and guard statements
  3. Avoid using iterators, iterators will generate new Iterators, and the overhead will increase, it is better to use the original for loop
  4. Avoid using the + sign to splice strings, simple splicing is available, and a large number of loop splicing still needs to use StringBuilder
  5. Use basic types as much as possible, basic types are stored in stack memory, and objects are stored in heap memory
  6. Avoid frequently creating "expensive" objects, it is best to use a singleton if you can use a singleton
  7. Avoid unnecessary log statements and incorrect log levels
  8. Reduce the number of loops, use a better algorithm
  9. Remember to close in time when you run out of resources to avoid memory leaks
  10. Using pool technology, you can use the thread pool as much as possible to use the thread pool
  11. Common memory leak scenarios in Android should be avoided
    . For example:
  • Memory leak caused by static variables
    Solution: Set the inner class as a static inner class or separate it; use context.getApplicationContext().

  • Memory leak caused by singleton mode
    Solution: pass parameter context.getApplicationContext().

  • Memory leak caused by property animation
    Solution: Call Animator.cancel() in Activity.onDestroy() to stop the animation.

  • Memory leak caused by Handler
    Solution: Use static inner class + WeakReference weak reference; clear the message queue when the outer class ends its life cycle.

  • Memory leaks caused by threads
    Solution: Set AsyncTask and Runnable as static inner classes or separate them; use weak references to save Context references inside threads.

  • Memory leak caused by unclosed resources
    Solution: Close or log off in time when the Activity is destroyed.
    For example: BroadcastReceiver: call unregisterReceiver() to logout;
    Cursor, Stream, File: call close() to close;

  1. Bitmap: Do appropriate compression, use a suitable drawable directory, select a suitable screen adaptation resolution directory, and
    properly reduce the pixel format of the image loaded, such as RGB_565 to replace the default ARGB_8888.
  2. Streamline invalid logs, unnecessary logs do not need to be typed
    Check the logs that appear in the code, and you will find a lot of randomly printed logs
2. Install the code checking plug-in, code scanning tool, and check the standardization of coding in real time (long-term)

codedex plugin, findbugs plugin installation

3. Optimize the startup app project (if appropriate, the effect may be the most obvious, short-term)
  • (1) Try to delay the loading timing of the Google suite as much as possible. In fact, it is possible to delay the loading of non-our apk, because many service nestings in the system will cause the gms service to be started all the time, so this part has to continue to analyze the startup in framwork logic
  • (2) The Google suite repeatedly reported an error optimization, and gms kept reporting an error. It may be because it was packaged twice and could not be loaded, so do not re-sign
  • (3) Verify the impact of odexization on loading speed. The benefit of odexization is preloading, which can speed up startup. If the system application must be started, this option can be considered; however, odex may cause loading exceptions to third-party applications In some cases, gms may not be applicable, so field verification is required
  • (4) Study the comparison between the startup animation and luancher startup time, and check whether there is a time difference between the two. If the startup animation takes too long, the launcher has already started at this time, and you can consider optimizing the startup animation time

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
insert image description here
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Full set of video materials:

1. Interview collection

insert image description here
2. Source code analysis collection
insert image description here

3. The collection of open source frameworks
insert image description here
welcomes everyone to support with one click and three links. If you need the information in the article, directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

Guess you like

Origin blog.csdn.net/weixin_43440181/article/details/130157867