App performance optimization


App performance optimization


1. Onbindviewholder runs on the UI thread, which is not suitable for time-consuming operations such as logic. It is only suitable for filling data into the view;

2. Use the under the support packageDiffUtilFor local refresh processing, diffutil (which also calls the local refresh method internally) can compare the difference of data and whether to change it;

DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(oldDatas, newDatas), true);

The calculation is in the main thread. If the amount of data needs to be placed in the thread, it will be updated through the handler.

3. Set the same holder for recyclerview that can reuse holderpool, in the same view, the holder will not be re created;

RecyclerView recyclerView = new RecyclerView(context);
//layoutManager. setRecycleChildrenOnDetach(true);// Linearlayoutmanager requires additional settings

4. Data cache: the delay of directly loading network data is heavy, and the data in the cache is loaded;

5. Optimize the code, minimize the creation of objects and reuse object resources, such as listening;

6、getExtraLayoutSpacePreload: recyclerview only caches visible views. The initial loading during sliding will lead to delayed jamming. You can use preload processing to change the sliding range of loading;

7. When nesting # recyclerview, thesetInitialPrefetchItemCountSet the number of displays for the first time, which is effective only when the list is nested and horizontal;

Black and white screen

When the system loads the app, a blank page will be displayed first. The background color of the blank page is obtained from the application theme. Generally, the default is white.

1. By giving the startup pagealoneSet the theme background or color to avoid, otherwise each activity will be set to the same startup image.

Set the background color to be transparent. If the start-up activity takes a long time, it will cause the start-up jam. It is necessary to avoid the time-consuming operation of the start-up activity;

To set the background picture, you need to introduce multiple pictures for screen adaptation, and you can’t adapt all screens. Therefore, layerdrawable is generally used, and then resource pictures are introduced to achieve the adaptation effect by setting parameters such as offset.

2. Reduce time-consuming tasks.

Initialize some unnecessary third-party operations separately, such as intentservice.

App startup page optimization

Apk volume optimization

Layout optimization

1. Reduce hierarchical nesting;

2. Reduce repeated drawing, which can be viewed by opening the show GPU overdraw setting in the system setting. Red indicates that it needs to be optimized;

3. Use # include (reuse layout), merge (reduce nesting), viewstub (load as required, can only be executed once).

Package volume optimization

1. Use tint to avoid importing resources multiple times;

2. Resource allocation, NDK, resconfigures, code confusion, etc;

3. Delete unreferenced resources.

Anr processing

When an anr occurs, the system will generate a traces in the / data / anr / directory Txt file, which can be exported to the local view through the ADB command. Generally, anr can locate specific classes.


The log shows the CPU usage, most of which is occupied by IO, indicating frequent read-write (database, file) operations;

2、dalvik threads free

If the display memory is insufficient, an oom exception will be triggered.

network optimization

1. Reduce the number of calls and the size of packets sent and received. Android studio has a built-in monitor tool to view the receiving and sending speed;

2. Network cache;

3. Seven cattle resources, pictures, etc. load suitable resources, such as compressed images.

Custom view

1. Avoid time-consuming operations such as memory allocation in the # OnDraw drawing method, because frequent execution is easy to cause oom;

2. Minimize the number of calls to {OnDraw.

WebView optimization

1. The WebView is preloaded to generate a webviewpool, and the mutablecontextwrapper is used to replace the reuse of different contexts to reduce the creation time;

WebView can only be initialized in the main thread. Put WebView into the handler message queue (looper. Myqueue() Addidlehandler()), although it is also in the UI thread, try to avoid jamming.

2. Listen to the request through shouldinterceptrequest, which is downloaded by the client and filled into the webresourceresponse;

        public WebResourceResponse shouldInterceptRequest(WebView view, String url) { 
                if (checkImageRequest(url)) {
                    File imageFile = Glide.with(view.getContext())

                    return WebResourceResponse(
                            new FileInputStream(imageFile)
            return super.shouldInterceptRequest(view, url);

Assuming that the interception is an image, the cache can be loaded through the local glide to avoid repeated loading.

3. Loading the local template and injecting data through JS method is a simple process, but it consumes more traffic.