Android performance optimization

   

       When we develop APP, due to the limitations of device performance and user experience requirements, we must consider performance issues during the development process. Let’s talk about it from three aspects: response time, memory, and power.

 

     1 Response time

        The response steps can be divided into: user operation - logic processing - network interaction - page display.

 

        Logic processing:

                       Clean up program logic, reduce unnecessary operations, and do not allocate unnecessary memory.

                        1) Do not declare temporary variables and write try-catch in the for loop.

                        2) Appropriate use of data types, such as: StringBuilder or StringBuffer, less use of enum, less use of parent class declarations (list, map)

                        3) If you need frequent new Theard, use the thread pool.

                        4) Correct use of the singleton pattern

                        5) Use constants

                        6) Use the strategy pattern for decoupling

 

        Network interaction: use cache 1) use thread pool 2) image cache 3) message cache, use obtainMessage to achieve message multiplexing 4) listVIew cache 5) network cache 6) layout cache

                         Control the maximum number of requests, cancel requests in time, filter duplicate requests, set timeouts, and set priorities.

 

        Page presentation: Analyze layout with hierarchy viewer and lint

                          1) Reduce the layout level as much as possible, use <includ>, <viewstub>, <merge>. UI drawing process: measure - layout - draw; both measure and layout need to loop through all child controls. So the more child controls, the longer the consumption time.

                          2) The number of pager cached by ViewPager at the same time is preferably the minimum number of 3, otherwise it will be stuck. Each pager reloads network and database data when it is used, do not preload.

                          3) Use RelativeLayout instead of LinearLayout

                          4) Avoid overdrawing (OverDraw)

                          5) Prepare resources for all resolutions as much as possible, reduce hardware scaling, and speed up UI drawing

                          6) Use lrucache and disklrucache; use network + database, set a minimum request interval for each activity and fragment.

 

    2 memory

                traceview analysis performance

                1) Bitmap consumes memory. When the picture is relatively large, it is easy to generate OOM. You need to use BitMapFactory.options to zoom. After use, use BitMap.recycle() to recycle in time.

 

    3 battery

           Power consumption is divided into two parts: drawing pictures and placing layouts; reporting data, positioning, and regularly retrieving background data.

 

           Measures: 1) Reduce the number and time of waking up the screen, correctly perform the wake-up operation when using wakelock, and close the operation according to the settings.

                      2) Operations that do not have to be performed immediately can be performed after charging or when the battery is full. Such as: file upload, image processing.

                      3) Packing multiple network requests into one can avoid power consumption caused by multiple network requests.

 

Guess you like

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