Android Performance Optimization-startup process cold start hot start

Source: Internet
Author: User

first, the application of the start Way

Typically, there are two types of start-up: Cold start and hot start.

1, cold start: When the application is launched, the background does not have the process of the app, then the system will re-create a new process assigned to the application, this startup is cold boot.

2, hot start: When the application started, the background already has the application of the process (example: Press the back key, the home button, although the app will exit, but the app's process is still left in the background, can go to the Task List to view), so in the case of existing processes, the launch will be from the existing process to launch the application This method is called hot start.

Characteristics

1, cold start: Cold start because the system re-creates a new process to assign it, the application class is created and initialized, and the Mainactivity class (including a series of measurements, layouts, drawings) is created and initialized, and finally displayed on the interface.

2, hot start: Hot start because it will start from the existing process, so the hot start will not go application this step, but directly go mainactivity (including a series of measurement, Layout, drawing), So the hot-start process simply needs to create and initialize a mainactivity, without having to create and initialize the application,

Because an application is created from the new process to the destruction of the process, application is initialized only once.

Second, the application of the start-up process

Cold start-up process: When you click on the app's startup icon, the Android system will fork from the zygote process to create a new process to assign to the app, then create and initialize the application class, create the Mainactivity class, load the theme style theme

Windowbackground properties are set to Mainactivity and configure some properties on the activity level, inflate layout, and when oncreate/onstart/ Onresume methods are finished after the last Contentview Measure/layout/draw display on the interface, so until here,

The first launch of the app is done, and the interface we see is the first frame we're talking about. So, to summarize, the application start-up process is as follows:

 The constructor method of the application-->attachbasecontext ()-->oncreate ()-->activity is constructed by-->oncreate () and the properties of the background in the configuration theme- ->onstart ()-->onresume () and the measurement layout is plotted on the interface.

 The approximate process is as follows:

1, click the desktop icon, Launcher will start the program default Acticity, and then follow the logic of the program to start a variety of activity

2, starting the activity requires the application framework layer of the Activitymanagerservice service process (service is also initiated by the activitymanagerservice process); In the Android application framework layer, Activitymanagerservice is a very important interface,

It is responsible not only for initiating activity and service, but also for managing activity and service.

Step 1. Start a new activity either through launcher, or by invoking the StartActivity interface inside the activity. All through the binder interprocess communication into the activitymanagerservice process, and call the Activitymanagerservice.startactivity interface;

Step 2. Activitymanagerservice calls Activitystack.startactivitymaywait to prepare information about the activity to be started;

Step 3. Activitystack notify Applicationthread To do activity start scheduling, The Applicationthread here represents the process of invoking the Activitymanagerservice.startactivity interface, which is launcher by clicking on the application icon.

This process is the process in which the activity is made by invoking startactivity within the activity;

Step 4. Applicationthread does not perform a real boot operation, It enters into the activitymanagerservice process by calling the Activitymanagerservice.activitypaused interface to see if a new process needs to be created to start the activity;

Step 5. For a story that launches an activity by tapping the application icon, Activitymanagerservice in this step calls startprocesslocked to create a new process, This step is not required to start a new activity by invoking startactivity within the activity.

Because the new activity starts in the same process as the original activity;

Step 6. Activitymanagerservic calls the Applicationthread.schedulelaunchactivity interface to notify the appropriate process to perform the activity operation;

Step 7. Applicationthread the action that initiates the activity to Activitythread,activitythread to import the corresponding activity class through ClassLoader, and then start it up.

Three, the cold start process encountered in the white screen black screen and optimize the start time

1, white screen problem:

Android Studio Upgrade 2.0 plus Instant Run,instant Run in order to allow us to quickly deploy the code, behind the fact there is a very complex logic, such as to build a server in the APK to communicate with Android studio, As well as code differences and substitutions, there may be white screen problems in the development process,

General release version of the program will not appear this phenomenon;

If there is a white screen problem, you can view the style file

<style name="apptheme" parent="Theme.AppCompat.Light.DarkActionBar  ">       <item name="android:windowistranslucent">True </item>   <item name="android:windownotitle">True </item> </style>

Added two properties, Windowistranslucent and Windownotitle, both of these properties are set to True, you can let the program in the initialization of the window is transparent, the initialization after the end of the program's main interface will be displayed, so it is completely invisible to the white screen interface

2. Optimization of start-up time

First measure the start time of activity-------Activity Reportfullydrawn () method

You will need to invoke the activity's Reportfullydrawn (). It will report in log for how long it took to initialize from the APK (the same time as the previous displayed) to the Reportfullydrawn () method.

The Reportfullydrawn () method displays a log similar to this:

activitymanager:displayed com. android.myexample/. Startuptiming: +768ms

The Reportfullydrawn () method on the 4.4 will crash (but log will print normally), prompting for update_device_stats permissions, but only the system app can authorize this permission. The solution is to adjust the way

Try {reportfullydrawn ();} Catch (SecurityException e) {}

There is also a way to measure the start-up time is also worth mentioning, that is Screenrecord command

Start by starting with the Screenrecord command with the-bugreport option (which can add a timestamp in frames-should be an attribute in L):

$ adb shell Screenrecord--bugreport/sdcard/launch.mp4
Then click on the app's icon, wait for the app to show, Ctrl-c Screenrecord, and use the ADB pull command to export the file to your computer. $ adb Pull/sdcard/launch.mp4

Now you can open the recorded video and see what happens. You need a video player that can be viewed frames by frame (QuickTime on Mac, it's not clear what player on the other OS is the best). Now play one frame at a time, notice that there is a frame timestamp above the video.

Go straight until you find the app icon is highlighted. This time the system has processed the Click event on the icon, started to start the app, and recorded the time of the frame. Continue playing the frame until you see the first frame of the app's entire UI. Depending on the situation (whether there is a startup window, whether there is a splash screen, etc.),

The actual order in which events and Windows occur can be different. For a simple app, you'll first see the startup window and then ramp out the app's real UI. After you see anything on the UI, you should record the first frame, when the app finishes the layout and draws, ready to start showing. It also records the time at which this frame occurs.

Now subtract these two times ((UI displayed)-(icon tapped)); Get the app all the time from click to draw ready. Although this time contains the time before the process was started, at least it can be used to compare with other apps.

Android Cold start time optimization

Cold start time is the time period between the moment the user clicks on your app and the system calls Activity.oncreate (). During this time period, WindowManager will load the app theme style Windowbackground as the app's preview element and then actually load the activity's layout

Cold Start Time Optimization

Once you know the fundamentals of Android Cold start time, there are a few tricks you can use to optimize the cold start time so that your app loads "faster" (visual experience faster). We can make a. 9 image of the background style that launches the activity, and then take this. 9 picture as Windowbackground.

  

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

① Customizing a theme for the activated activity

<style name="apptheme.launcher"> <item name="android: Windowbackground"> @drawable/window_background_statusbar_toolbar_tab</item></style >

② apply the new theme to the settings AndroidManifest.xml

<Activity Android:name=". Mainactivity"Android:theme="@style/apptheme.launcher">      <intent-filter>        <action android:name="Android.intent.action.MAIN"/>        <category android:name="Android.intent.category.LAUNCHER"/>    </intent-filter></activity>

③ due to the mainactivity set a new theme, this will overwrite the original theme, so in mainactivity need to set back to the original theme

 Public class Mainactivity extends Appcompatactivity {     @Override    protectedvoid  onCreate ( Bundle savedinstancestate) {         // Make sure this line comes before calling Super.oncreate ().         SetTheme (r.style.apptheme);         Super.oncreate (savedinstancestate);    }}

Android Performance Optimization-startup process cold start hot start

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.