Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize start-up Optimization of Android Yong Chuang High-order performance Optimization

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain how to achieve startup optimization in Android high-level performance optimization. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Android bravely enter high-level performance optimization how to achieve startup optimization" bar!

? Start the internal mechanism

There are three startup states for an application:

Cold start

Warm start

Hot start.

? Cold start

Cold start refers to the application starting from scratch: cold start occurs when the application is started for the first time after the device is started (Zygote > fork > app), or after the system shuts down the application.

At the beginning of a cold boot, the system has three tasks. These tasks are:

Load and start the application.

A blank startup page of the application is displayed immediately after startup.

Create an application process.

Once the system has created the application process, the application process is responsible for the next phase:

Create the entity of the application.

Start the main thread.

Create the main page.

Draw the View on the page.

Layout page.

Perform the first drawing.

As shown below:

Displayed Time: initial display time

ReportFullyDrawn (): time of full display

Note: performance problems may occur during the creation of Application and Activity.

? Create Application

When the application starts, the blank startup page remains on the screen until the system finishes drawing the application for the first time.

If you override Application.onCreate (), the system will call the onCreate () method on Application. The application then generates the main thread, also known as the UI thread, and gives it the task of creating the main Activity.

? Create Activity

After the application process creates your Activity, Activity will perform the following actions:

Initialize the value.

Call the constructor.

Call the callback method of the current lifecycle state of Activity, such as Activity.onCreate ().

Note: the onCreate () method has the greatest impact on load time because it performs the most expensive tasks: loading the layout and rendering of UI, and initializing the objects needed for Activity to run.

? Hot start

During hot startup, the system pulls the application back to the foreground from the background, and the application's Activity is not destroyed in memory, so the application can avoid repeating object initialization, UI layout and rendering.

If the Activity is destroyed, it needs to be recreated.

The difference between cold startup and cold startup: there is no need to create an Application.

? Warm start

Warm start is somewhere between cold start and hot start. For example:

The user presses the return key to exit the application and then restarts. The process may not have been killed, but the application must recreate the Activity by calling onCreate ().

The system reclaims the memory of the application, and then the user rerun the application. Both the application process and Activity need to be restarted.

Let's see how long they spend together.

? When does the query start? Initial display time (Time to initial display)

In Android 4.4 (API level 19) and later, logcat contains an output line that contains a value named Displayed. This value represents the amount of time elapsed between starting the process and completing the drawing of the corresponding activity on the screen. The elapsed time consists of the following event sequence:

Start the process.

Initialize the object.

Create and initialize the Activity.

Load the layout.

Draw your application for the first time.

Note that you need to do the following to view the log here:

The log line class of the report, as shown below:

/ / Cold start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 1s355ms

/ / warm start (process killed)

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 1s46ms

/ / Hot start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 289ms

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 253ms

Illustration explains:

First time, cold start time: + 1s355ms

Then we kill the process in the background and start the application again.

Second time, warm start time: + 1s46ms

Here we kill the process in the background so: the application process and Activity need to be restarted.

Third time: hot start time: + 289ms and + 253ms

Press the return key to exit activity only. So it takes less time.

Of course, on the whole, the startup time of this application is not long, because Demo's Application and Activity do not do much.

? Full display time (Time to full display)

You can use the reportFullyDrawn () method to measure the elapsed time between application startup and the complete display of all resource and view hierarchies. This can be valuable in cases where the application performs deferred loading. In lazy loading, the application does not prevent the initial drawing of the window, but loads resources asynchronously and updates the view hierarchy.

Here I add a worker thread to Activity.onCreate (). And call the reportFullyDrawn () method inside. The code is as follows:

@ Overridepublic void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); Log.e (this.getClass (). GetName (), "onCreate"); setContentView (R.layout.activity_main); New Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (3000); reportFullyDrawn ();} catch (InterruptedException e) {e.printStackTrace ();}}) .start ();}

The log line class of the report, as shown below:

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s970ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s836ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s107ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s149ms

Illustration explains:

Then you will find that the interface has been out for a while before typing this log. Seeing here, I think a lot of people already know how to optimize the startup speed.

? Performance retardation analysis

We can see that there are actually three startup situations in the above experiment, and the aspects affected by us are application and activity.

? Application initialization

Startup performance may be affected when your code overrides the Application object and performs heavy work or complex logic when initializing the object. The reasons include:

The initial onCreate () function of the application. For example, initialization is performed that does not need to be performed immediately.

Any global singleton object initialized by the application. Such as: some unnecessary objects.

Any disk Ibind O, deserialization, or tight loop that may occur.

Solution

Whether the problem is unnecessary initialization or disk IZP O, the solution is delayed initialization. In other words, you should initialize only the objects that are needed immediately. Instead of creating a global static object, switch to singleton mode, where the application initializes the object only the first time it is needed.

In addition, consider using dependency injection frameworks (such as Hilt)

? Activity initialization

Activity creation usually requires a lot of expensive work. In general, there is an opportunity to optimize this work to achieve performance improvements.

The reasons include:

Load large or complex layouts.

Prevent the screen from being drawn on disk or network Iplink O.

Load and decode Bitmap.

VectorDrawable object.

Activity initializes any global singleton object.

All resources initialized.

The solution is as follows.

? Layout optimization

Flatten the view hierarchy by reducing redundancy or nested layouts.

Layout reuse (

< include/>

And

< merge/>

)

With ViewStub, parts of UI that do not need to be visible during startup are not loaded.

? Code optimization

Unnecessary initialization or disk Ibank O, delayed initialization

Resources initialize the classification so that applications can delay execution on different threads.

Dynamically load resources and Bitmap

There will be separate articles to write about the optimization of these two pieces.

? Blocking experiment? Application blocking for 2 seconds, Activity blocking for 2 seconds? SccApp.classpublic class SccApp extends Application {@ RequiresApi (api = Build.VERSION_CODES.P) @ Override public void onCreate () {super.onCreate (); String name = getProcessName (); MLog.e ("ProcessName:" + name); getProcessName ("com.scc.demo"); try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace () }? MainActivity.classpublic class MainActivity extends ActivityBase implements View.OnClickListener {@ Override public void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); Log.e (this.getClass (). GetName (), "onCreate"); setContentView (R.layout.activity_main); Try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace ();} new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (3000); reportFullyDrawn () } catch (InterruptedException e) {e.printStackTrace ();}) .start ();}}

The log of the report is as follows:

/ / Cold start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 5s458ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 8s121ms

/ / warm start (process killed)

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 5s227ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 7s935ms

/ / Hot start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 2s304ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 5s189ms

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 2s322ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 5s169ms

? Put the 2 seconds blocked by Appliacation and Activity on the worker thread to operate

This is to put the code in the following code to execute, not all posted.

New Thread (new Runnable () {@ Override public void run () {...}}) .start ()

The running results are as follows:

/ / Cold start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 1s227ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s957ms

/ / warm start (process killed)

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 1s83ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s828ms

/ / Hot start

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 324ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s169ms

I/ActivityTaskManager: Displayed com.scc.demo/.actvitiy.MainActivity: + 358ms

I/ActivityTaskManager: Fully drawn com.scc.demo/.actvitiy.MainActivity: + 3s207ms

? APP starts black / white screen

When Android applications are launched, especially large applications, there is often a black or white screen for a few seconds, depending on the theme style of the main interface Activity.

? Elegant solution to black and white

When Android applications are launched, many large apps have an ad (picture and video) page or a flash screen (2-3s). This is not what the developer wants to put on it, but to avoid the poor user body caused by the above startup white screen. Of course, you can cherish these 2-3 seconds to make an asynchronous load or request.

That's it. The application of startup mode, startup time and startup speed optimization is done. Of course, if there is a better optimization plan, it will continue to be added.

At this point, I believe you have a deeper understanding of "how to achieve startup optimization in Android Yong Chuang high-level performance optimization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report