(25) Cold start and warm start

Foreword: I went to interview this week and the interviewer asked about cold start and hot start. Well, the previous supplementary course was to make up for hot update. React native was involved. I really didn’t know about cold start and hot start. I wrote a blog to learn about it.


Reference blog: Android cold boot warm boot test

demo: cold start and warm start demo


Replenish:

1. Test the startup time shell command: adb shell am start -W (package name/.class name)

比如:adb shell am start -W com.example.demo_21_custom_view/.MainActivity

2. After successful execution, three measured times will be returned: 

  • ThisTime: Generally the same as TotalTime, unless a transparent Activity is opened when the application starts to pre-process some things and then display the main Activity, which will be smaller than TotalTime. 
  • TotalTime: The startup time of the application, including the creation process + Application initialization + Activity initialization to interface display. From the point of view of my experiment, if the process and Application have been initialized, the creation process + Application initialization time in totaltime should be ignored.
  • WaitTime: Generally larger than TotalTime, including the time-consuming system impact.


1. Cold start and warm start concept

  • Cold start: When an application is started, there is no process of the application in the background. At this time, the system will recreate a new process and assign it to the application. This startup method is a cold start.
  • Hot start: When the application is started, the process of the application already exists in the background (for example, if you press the back key, the application will exit, but the process of the application will still remain in the background, which can be viewed in the task list), so when the application is already In the case of a process, this kind of startup will start the application from an existing process, which is called a hot start.


2. Test cold start and warm start time

2.1 Cold start

Application cold start time test: Remove the application from the recent task, and then use the shell command to call it, you can test the cold start time.

You can see the life cycle experienced by the cold start application:

1. The application to which the application belongs will go through the onCreate process

2. The main interface will go through the onCreate->onStart ->onResme process


2.2 Warm Start

To test the hot start time, you need to press the return key to exit the application and return to the desktop.

It can be seen that the life cycle experienced by the hot start application and the similarities and differences with the cold start:

1. The application to which the application belongs " will not go " the onCreate process

2. The main interface will also go through the onCreate->onStart->onResme process

3. The startup time is obviously much less than the cold start

This shows that a warm start takes less time than a cold start, mainly because the application does not take time to recreate. (In fact, it also saves the process creation time)


2.3 Ordinarily restore the application from the background

There is also an additional situation where clicking the home button hides the application in the background, and the main interface of the application does not go through the onDestroy process.

It can be seen that the life cycle experienced by cold and hot start is different, but it is very simple to follow the following process:

onPause->onStop->onStart->onResume流程

The startup time is the shortest, because the activity does not need to be recreated.


3. Summary (excerpted from the reference blog)

3.1 Application startup process

On the Android system, when the application does not have a process, the application startup is the following process: when the app's startup icon is clicked, the Android system will fork from the Zygote process to create a new process and assign it to the application. It will create and initialize the Application class, create the MainActivity class, load the windowBackground and other properties in the theme style Theme to MainActivity, configure some properties on the Activity level, and then inflate the layout. When the onCreate/onStart/onResume methods are all finished, the last The measure/layout/draw of the contentView is displayed on the interface, so the first startup of the application is not completed until here, and the interface we see at this time is the so-called first frame.

So, to sum up, the startup process of the application is as follows:

Constructor method of Application——>attachBaseContext()——>onCreate()——>Constructor method of Activity——>onCreate()——>Configure the background and other attributes in the theme——>onStart()——>onResume( )——>Measurement layout drawing is displayed on the interface.

This requires an overall understanding of activity startup.


3.2 Cold and hot start characteristics

1. Cold start: Because the system will recreate a new process and assign it to it, it will first create and initialize the Application class, then create and initialize the MainActivity class (including a series of measurements, layout, and drawing), and finally display in the on the interface.

2. Hot start: Since the hot start will be started from the existing process, the hot start will not go to the Application step, but directly to the MainActivity (including a series of measurement, layout, and drawing), so the hot start The process only needs to create and initialize a MainActivity, without having to create and initialize the Application, because an application is only initialized once from the creation of a new process to the destruction of the process.

The startup mentioned above is to click the startup icon of the app to start, and another way is to enter the recently used list interface to start the application. This should not be called startup, but should be called recovery.




Guess you like

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