Detailed analysis of cold start optimization of Android performance

1. Preface

App cold start is relatively slow, users need to wait a long time to click the desktop image, and the experience is poor.

2. App startup mode

Cold start

Scenario: cold start means that the app runs for the first time after the mobile phone is started, or the app process is restarted after being killed.

It can be seen that the necessary condition for cold start is that the app process does not exist, which means that the system needs to create a process and the app needs to be initialized. Among the three startup modes, cold startup takes the longest time, and the optimization of cold startup is also the most challenging. Therefore, this paper focuses on the optimization of cold start.

Life cycle: process. Start - > Application creation - > attachbasecontext - > oncreate - > OnStart - > onresume - > activity life cycle startup speed: it is the slowest among several startup types, and it is also the biggest obstacle for us to optimize the startup speed

Warm start

Scenario: the app process exists. At that time, the activity may be recycled due to insufficient memory. At this time, the process does not need to be re created to start the app, but the oncrate of the activity still needs to be re executed. The scene is similar to opening Taobao for a walk, then cutting to wechat to chat, and returning to Taobao again after half an hour. At this time, the Taobao process exists, but the activity may be recycled. At this time, you only need to reload the activity.

Lifecycle: oncreate - > OnStart - > onresume - > activity lifecycle

Starting speed: faster

Hot start

Scenario: the app process exists, and the activity object still exists in memory and is not recycled. It can avoid repeated object initialization, layout analysis and drawing.

The scene is similar to when you open wechat and chat for a while. At this time, you go out and look at the calendar. When you open wechat, wechat startup belongs to hot startup.

Lifecycle: onresume - > activity lifecycle

Starting speed: fast

3. How to count the startup time of android app (using the command line)

adb shell am start -W [packageName]/[packageName.***Activity]

For example:

adb shell am start -W com.dateyou.test/com.datayou.test.ui.home.PageLoadingActivity

Of course, in order to make the results more accurate, you can perform multiple averaging.

adb shell am start -S -R 10 -W com.dateyou.test/com.datayou.test.ui.home.PageLoadingActivity

Where - s means to stop forcibly before each start, and - R means the number of repeated tests. The output of each time is as follows.

Where totaltime represents the start time of the current activity

4. Cold start process

Cold Start refers to the process from the application process does not exist in the system to the system to create the application running process space. Cold start usually occurs in the following two situations:

1) Start the application for the first time since the device started

2) After the system kills the application, start the application again

At the beginning of cold start, the system is responsible for three things:

1) Load and start app

2) A blank preview window is displayed immediately after the app starts

3) Create app process

Once the system completes creating the app process, the app process will then be responsible for completing the following work:

1) Create application object

2) Create and start the main thread activitythread

3) Create and start the first activity

4)Inflating views

5) Layout screen

6) Perform the first drawing

Once the app process completes the first drawing, the system process will replace the preview window displayed earlier with main activity. At this time, the user can officially start interacting with the app.

From the cold start process, we cannot intervene in system operations such as app process creation. We can intervene in the following:

1) Preview window

2) Application lifecycle callback

3) Activity lifecycle callback

5. Cold start optimization

1) Preview window

Why is there a short black or white screen when starting? During the period from the moment the user clicks your app to the system call activity. Oncreate(), WindowManager will first load the windowbackground in the app theme style as the preview element of the app, and then really load the layout of the activity. Obviously, if your application or activity starts too slowly and the backgroundwindow of the system is not replaced in time, there will be a white screen or a black screen at startup (depending on whether your theme is dark or light).

Solution: customize a theme in style, put a background picture or advertising page in it, and let users see the default picture first.

2) Application oncrate() optimization

1. Time consuming operations are performed in sub threads

2. Unnecessary initialization lazy loading

3) Activity oncreate() optimization

1. Time consuming operations are performed in sub threads

2. Reduced layout levels

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>