Android application startup optimization notes sorting

Application startup related process and optimization

 

Application startup mainly involves the SystemServer process and the app process.

The SystemServer process is responsible for app process creation and management, window creation and management (StartingWindow and AppWindow), application startup process scheduling, etc.

After the App process is created, a series of process initialization, component initialization (Activity, Service, ContentProvider, Broadcast), main page construction, and View loading are performed.

1. App startup method

Start mode: hot start, cold start, warm start three.

 

Cold start

Cold startup takes the most time. Cold startup is a common scenario where the APP starts for the first time or the APP is completely killed.

warm start

When the application is started, there is already a process of the application in the background, and the Activity is recycled due to insufficient memory. The system will start the Activity from the existing process.

Warm startup scenario: The first is that the user first presses the return key to exit the app, and finally restarts the app. The second is that the system first recovers the memory of the app, and finally restarts the app.

Hot Start

On a warm start, the system brings the activity back to the foreground. If all of the application's activities exist in memory, the application can avoid repeated object initialization, rendering, and drawing operations.

The common scenarios of hot start are: when we press the Home button or the app is switched to the background in other situations, the process of starting the app again.

2. Application cold start key process

 

  1. The user Click event triggers the IPC operation, and the start method of Process will be executed to create the process.
  2. ActivityThread executes the main method, which is the entry point of the App process, which is equivalent to the main method of the Java process, in which the creation of the message loop and the creation of the main thread Handler will be executed.
  3. After the Handler is created, it will execute the bindApplication method, where reflection is used to create the Application and call the Application-related life cycle.
  4. After the Application life cycle, the Activity life cycle will be executed. After the Activity Life Cycle ends, it will be executed to ViewRootImpl, and only then will a real page be drawn.

Optimization direction

After creating the Application, starting the main thread, creating HomeActivity, loading the layout, arranging the screen and drawing the first frame of the interface, the startup is complete.

Related processes that can be optimized at the APP layer: Application attachBaseContext, Appication onCreate and Activity LifeCycle three processes.

3. Attribution Analysis

The most fundamental thing for a program to run is to obtain CPU time slices. If a task requires more CPU time to execute, it will affect the execution of other tasks, thereby affecting the operation of the overall task queue;

Thread switching involves CPU scheduling, and CPU scheduling has the overhead of system resources, so frequent switching of a large number of threads will also cause huge performance loss;

Waiting for IO and locks will directly block the execution of tasks, and system resources such as CPU cannot be fully utilized.

 

 

 

4. Launch indicators

The first is bootstart, which is the time the process was created

The second is the time when the start-up is completed and the rendering of the first screen of the home page is completed;

The third is the start time, which refers to the time stamp of the end of the start minus the time stamp of the start of the start;

5. Start Optimizing Value

The increase in startup time may reduce the retention of App users.

The goal of start-up performance optimization is to focus on low-end phones and radiate mid-to-high-end phones. Through in-depth optimization of technology and products, the user experience can be perceivably improved, and the scale of users can be expanded, retention and revenue can be increased.

6. Optimizing plan arrangement

1.Application optimization

The Application phase is usually used to initialize core business libraries. In the early stage of application development, we did not manage and control the startup tasks at this stage, resulting in the accumulation of a large number of strongly business-related codes. The pruning is based on the principles:

  1. Tasks in Application should be global basic tasks
  2. Application creation should minimize network request operations
  3. Strong business-related tasks are not allowed when the Application is created
  4. Minimize the work of Json parsing and IO operations when creating an Application

The startup tasks in Application should be as few as possible, mainly divided into three categories: basic library initialization, function configuration and global configuration. The basic class library is mainly to initialize and configure basic libraries such as network library and log library. Except for the main process, other processes also depend on these tasks. Removing them will affect the overall stability. They are also the largest and most time-consuming tasks in the startup tasks, so reducing their time consumption is also the focus of continuous optimization later. Function configuration is mainly the pre-configuration of some globally related business functions, such as preloading of business caches, pre-loading of specific services, etc. Removing them will cause business damage. In this case, we need to find business demands and the balance between functional configuration. The global configuration is mainly for global UI configuration and file path processing operations, which account for a small proportion and take less time. They are pre-tasks for creating the home page, so they will not be processed for now.

The core of task arrangement is to deal with the front and rear dependencies of tasks.

1. It is necessary to arrange tasks based on processes, divide Application tasks in detail, and fine-tune the operation of tasks to the process level, so as to avoid unnecessary tasks being executed in non-main processes.

2. Lazy loading. This is mainly to transform some basic tasks, separate task initialization and task startup, move the startup work out of the Application creation process, and streamline it at the same time to remove redundant logic. When creating an object, you can delay the creation of its member objects, and flexibly use keywords such as by lazy to make the object lightweight.

3. Process convergence. Multi-process can realize module isolation and avoid the upper limit of memory caused by the high memory ratio of a single process. The disadvantage is that multi-process will cause the overall memory usage of the application to be too large, and the probability of triggering low memory is higher. In addition, if the memory ratio is too high when the application starts, it may cause the mobile phone to reclaim memory and occupy a large amount of CPU resources. This is reflected in the user experience as slow startup and application freezes. Comparing the advantages and disadvantages of multiple processes, the strategy adopted is to delay the start of processes other than the main process as much as possible, and at the same time reduce the number of processes through process merging.

4. Thread convergence. For multi-core CPUs, an appropriate number of threads can improve efficiency, but if the number of threads floods, the CPU load will be overloaded. Multi-thread concurrency is essentially the process in which multiple threads take turns to obtain CPU usage rights. In the case of heavy load, too many threads compete for time slices. In addition to reducing the startup speed, it will also cause the main thread to freeze and affect the user experience. When optimizing this aspect, you need to ensure that a unified thread pool is used globally. At the same time, many second-party and third-party SDKs are also big users of creating sub-threads. At this time, it is necessary to communicate with relevant technical departments to eliminate unreasonable thread creation. On the other hand, avoiding network requests during the startup phase is also the key to reducing the number of threads.

Application optimization is the key to the entire startup process. Reasonable task arrangement can not only reduce the creation time of Application, but also have a great optimization effect on subsequent home page creation.

2. Start the link

After executing the Application creation, the main work of the application process is to create the Activity. It should be noted here that there are many post tasks hidden in the main thread from Application to Activity, as well as callback monitoring of registered ActivityLifecycleCallbacks, which will secretly increase the time gap from Application to Activity. ActivityLifecycleCallbacks registration is usually related to business, and its registration is relatively hidden.

Regarding the time consumption of main thread messages, many problems can be found when using Profiler and Systrace to locate the time consumption of the startup process. For various reasons, tasks in the Application will post time-consuming work to the main thread. On the surface, the creation time of the Application is shortened, but the overall startup time is expanded. For the time-consuming point, we should locate the root cause and solve it, instead of just posting it blindly, so as to treat the symptoms but not the root cause.

Shortening the link from startup to the home page is the focus of our optimization.

 

 

In the general startup process, the loading page starts the page and undertakes two tasks of routing and permission request.

1. Under normal circumstances, the user starts the App, and judges whether to log in on the loading page. If not logged in, enter the login page.

2. If the user has already logged in, judge whether it is necessary to display the opening page, if necessary, enter the opening page, wait until the opening is completed, jump back to the loading interface, and then enter the home page.

Even if there is no opening page, the user starts the APP to display the home page, at least two activities must be started. The core of shortening the startup link is to combine the loading, main and opening pages into one page. Doing so can not only reduce the startup of the Activity at least once, but also process other tasks in parallel when displaying the opening page.

The code logic to realize this function is to set the main page as the startup page, and encapsulate the home page and opening screen page into two fragments, which are displayed according to the business logic. The user clicks the icon to enter the homepage. If it is judged to be logged in, the front page pre-task and homepage UI rendering will be performed, and at the same time, it will be judged whether to load the opening page fragment.

3. Home page optimization

lazy loading

The general situation of the home page is as follows:

 

The APP will load the four TabFragments of the home page, which is time-consuming.

Delay the creation and loading of the other 3 Fragments such as dynamics. Consider that only the first fragment is visible when the home page is displayed. Lazy load the home page. The home page uses the common ViewPager2+tabLayout architecture. ViewPager2 naturally supports lazy loading operations. In order to avoid the existing fragments being recycled when the page is switched, increase the cache pool size of the recyclerView inside viewPager2.

 ((RecyclerView)mViewPager.getChildAt(0)).setItemViewCacheSize(mFragments.size());

This solution is to postpone the creation and rendering of other pages until the switching. If the page is heavy and the performance of the mobile phone is poor, there will be obvious freezes and white screens when switching, which is also unacceptable.

Therefore, the home page and each fragment have to be transformed.

4. ViewStub implements lazy loading.

The creation of View is a big time-consuming home page rendering. Use LayoutInflater to load xml files, which involves xml parsing, and then the process of generating instances through reflection is generally time-consuming. For the relatively simple xml, code is used for construction, but for complex layout files, code construction is time-consuming and unmaintainable. It is recommended to delay loading in the form of ViewStub.

5.Json parsing and processing

The Json parsing operation is also a point that needs to be optimized. The reason why Json parsing is time-consuming is essentially that when parsing, the creation from Json data to objects is generated and assigned through reflection operations. The more complex the object, the longer it will take. For the main interface of the homepage, the time-consuming of parsing the returned object may exceed the time-consuming of UI rendering on low-end machines. The solution that can be adopted is to reconstruct the data objects on the home page using Kotlin, and mark the related objects with @JsonClass(generateAdapter = true), which will generate corresponding parsing adapters for the marked objects during compilation, thereby shortening the parsing time .

 

 6. XML parsing optimization

1.XML asynchronous parsing.

2. Compose scheme.

Guess you like

Origin blog.csdn.net/zhangying1994/article/details/129296832