SoFunction
Updated on 2025-04-09

A brief discussion on the startup process of Android performance optimization (cold start and hot start)

This article introduces a brief discussion on the startup process (cold startup and hot startup) of Android performance optimization. It is shared with you. The details are as follows:

1. How to start the application

Generally speaking, there are two types of startup methods: cold start and hot start.

1. Cold startup: When starting the application, there is no process for 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 cold startup.

2. Hot start: When starting the application, there is already a process of the application in the background (for example: press the back key and home key. Although the application will exit, the process of the application will still be retained in the background and can be viewed in the task list). Therefore, in the case of existing processes, this startup will start the application from the existing processes. This method is called hot start.

Features

1. Cold start: Cold start Because the system will recreate a new process and assign it to it, the Application class will be created and initialized, then the MainActivity class (including a series of measurements, layouts, and drawings), and finally displayed on the interface.

2. Hot start: Since hot start will start from an existing process, hot start will not go to the Application step, but will go directly to MainActivity (including a series of measurements, layouts, and drawings). Therefore, the hot start process only requires creating and initializing a MainActivity, without creating and initializing an Application.

Because an application will only be initialized once from the creation of a new process to the destruction of a process.

2. Application startup process

Cold startup process: When clicking the app's startup icon, the Android system will create a new process from the Zygote process and assign it to the application. Then it will create and initialize the Application class, create the MainActivity class, and load the theme style in the Theme.

The windowBackground and other properties are set to MainActivity and configure some properties at the Activity level, then inflate layout, and after the onCreate/onStart/onResume method are all completed, the contentView's measure/layout/draw is displayed on the interface, so until here,

The first startup of the application is considered to be completed, and the interface we see at this time is what we call the first frame. So, to summarize, the application startup process is as follows:

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

The general process is as follows:

1. Click the desktop icon, Launcher will start the program's default activity, and then start various activities according to the program's logic.

2. Starting Activity requires the help of ActivityManagerService service process at the application framework layer (Service is also started by the ActivityManagerService process); in the Android application framework layer, ActivityManagerService is a very important interface.

It is not only responsible for starting the Activity and Service, but also for managing the Activity and Service.

Step 1. Whether it is to start the Activity through Launcher or to start a new Activity through the Activity internal call to the startActivity interface, it enters the ActivityManagerService process through Binder inter-process communication and calls the interface;

Step 2. ActivityManagerService call prepares the relevant information about the Activity to be started;

Step 3. ActivityStack notifies ApplicationThread that it needs to start the activity. The ApplicationThread here represents the process that calls the interface. For the scenario where you click on the application icon, this process is Launcher.

For the scenario where startActivity is called inside the Activity, this process is the process where this Activity is located;

Step 4. ApplicationThread does not perform real startup operations. It enters the ActivityManagerService process by calling the interface to see if it needs to create a new process to start the Activity;

Step 5. For the scenario where Activity is started by clicking the application icon, ActivityManagerService will call startProcessLocked to create a new process in this step, and for the new Activity by calling startActivity inside the Activity, this step does not need to be executed.

Because the new Activity is started in the process where the original Activity is located;

Step 6. ActivityManagerServic calls the interface and notifies the corresponding process to execute the operation of starting the Activity;

Step 7. ApplicationThread forwards the operation to start the Activity to ActivityThread. ActivityThread imports the corresponding Activity class through ClassLoader and then starts it.

3. White screen and black screen encountered during cold start and optimized startup time

1. White screen problem:

After Android studio upgrade 2.0, Instant Run is added. In order to allow us to quickly deploy code, Instant Run actually has a very complex set of logic behind it, such as establishing a server in the APK to communicate with Android Studio, as well as code differences comparison and replacement, etc., there may be white screen problems during the research and development process.

Generally, release version programs will not have this phenomenon;

If there will be a white screen problem next, you can view the style file

<style name="AppTheme" parent="">
......
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style> 

Two attributes are added, windowIsTranslucent and windowNoTitle. Set both attributes to true, so that the window is transparent when the program is initialized, and the main interface of the program will be displayed after the initialization is completed, so that the white screen interface will be completely lost.

2. Optimization of startup time

First measure the startup time of the activity ------------ Activity's reportFullyDrawn() method

You need to call the Activity's reportFullyDrawn(). It will report in the log how long the reportFullyDrawn() method is called from the initialization of the apk (the same time as the previous Displayed) to how long it took.

The log displayed by the reportFullyDrawn() method is similar to this:

ActivityManager: Displayed /.StartupTiming: +768ms

Calling the reportFullyDrawn() method on 4.4 will crash (but the log can still print normally), prompting that the UPDATE_DEVICE_STATS permission is required, but this permission can only be authorized by the system app. The solution is to adjust it like this

 try{

  reportFullyDrawn();

}catch(SecurityException e){

}

There is another method to measure the startup time, which is also worth mentioning, that is, the screenrecord command

First start the screenrecord command with the --bugreport option (it can add a timestamp in frames - it should be a feature in L):

$ adb shell screenrecord --bugreport /sdcard/launch.mp4

Then click the app icon, wait for the app to display, ctrl-C screenrecord, and use the adb pull command to export the file to the computer.

$ adb pull /sdcard/launch.mp4

Now you can turn on the recorded video to see what's going on. You need a video player that can view frame by frame (Quicktime on Mac is fine, it is not clear which player on other Os functions is the best). Now play frame by frame, notice that there is a frame timestamp above the video.

Keep going until you find that the app icon is highlighted. At this time, the system has processed the click event on the icon, started to start the app, and recorded the time of this frame. Continue to play the frame until you see the first frame of the entire UI of the app. Depending on the situation (whether there is a startup window, whether there is a startup screen, etc.),

The actual order in which events and windows occur may vary. For a simple app, you will first see the startup window and then gradually transform the app's real UI. After you see anything on the UI, you should record the first frame, and then the app has finished layout and drawing and is ready to start displaying. At the same time, it also records the time when this frame occurs.

Now subtract these two times ((UI displayed) - (icon tapped)); to get all the time the app has from clicking to drawing ready. Although this time includes the time before the process starts, at least it can be used to compare with other apps.

Android cold boot time optimization

Cold startup time refers to the time period between the moment the user clicks on your app and the system call (). During this period of time, WindowManager will first load windowBackground in the app theme style as the preview element of the app, and then actually load the layout of the activity

Cold start time optimization

After knowing the principle of Android cold start time, you can use some tips to optimize the cold start time, so that your app loading becomes "faster" (fast in visual experience). We can create a .9 picture with the background style that starts the Activity, and then use this .9 picture as a windowBackground.

After the image is created, we can use it as a preview element for the cold start stage of the app, as follows:

Customize a Theme for the startup Activity

<style name="">

  <item name="android:windowBackground">@drawable/window_background_statusbar_toolbar_tab</item>

</style> 

Apply the new Theme to the settings

<activity

  android:name=".MainActivity"

  android:theme="@style/">

 

  <intent-filter>

    <action android:name="" />

    <category android:name="" />

  </intent-filter>

</activity> 

Since a new Theme is set to MainActivity, doing so will overwrite the original Theme, so you need to set back to the original Theme in MainActivity

public class MainActivity extends AppCompatActivity {
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
 
    // Make sure this line comes before calling ().
    setTheme();
 
    (savedInstanceState);
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.