Now small program hot abnormal, all kinds of demo endless, I also do not mundane, has been concerned, although not the death of loyalty, but have to admire the ambition. The development difficulty is not high plus the ecology, feel this thing formally enters the market after must have the certain influence to the native development, especially some start-up company because considers the native development high cost, the small procedure is an efficient way. It is recommended that you also keep your attention and make some basic attempts.
Okay, here we are. Recently the company's product should be the start-up time is too long, so did a certain optimization, and the author was fortunate to participate, and now the main record of the practice process.
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.
The optimization referred to in this article is for cold start. Simply explain the app's startup process:
1. Click Launcher, launch the program, notify Activitymanagerservice
2.ActivityManagerService notification Zygote process hatching application process, allocating memory space, etc.
3. The main () method that executes the application Activitythread
4. Application notification Activitymanagerservice It has been started, Activitymanagerservice save a proxy object for the application, Activitymanagerservice can control the application process through it
5.ActivityManagerService notifies the application process to create an activity instance of the portal to perform its life cycle
Thelife cycle methods of appl ication and ingress activity during startup are called in the following order:
1.Application Construction Method
2.attachBaseContext ()
3.onCreate ()
4. Object construction of the entry activity
5.setTheme () Setting up topics and other information
6. OnCreate of the entry activity ()
7. OnStart of the entry activity ()
8. Onresume of the entry activity ()
9. Onattachtowindow of the entry activity ()
10. Onwindowfocuschanged of the entry activity ()
In theory, when the callback to the Onresume () method of the entry activity, the app is officially launched, but in this sense is actually not rigorous, because at this point in time
Only the theme information and the View tree establishment are actually completed. The real process of view drawing is not measure layout draw, so it is not "visible" in the real sense. The onwindowfocuschanged () mediation is closer to the truth. So we confirm that the start time can be recorded from the application construction method, and then to the entry activity of the onwindowfocuschanged () to record a time, the difference between the start time of the app. Of course, this is a more stupid method, the ADB command has a corresponding record command: ADB shell am START-W Package Name/Entry class full path name, the time will be three, respectively, is Thistime,totaltime,waittime, We need to take totaltime as a standard.
With this knowledge of readiness, we need to understand why app startup is slow. The author of the company's products through a number of records time difference, found the main reason is in the application and activity of the start life cycle of the operation of too many times, the hardest hit is Application Attachbasecontext () and
The OnCreate () method, because the business needs to integrate a large number of third-party SDKs and some of the company's packaged SDK, more time-consuming Okhttp module initialization (should business needs to be modified), the company package of a day hybrid development framework (which will resolve a local long configuration file, Unzip the local H5 page Resource pack, first identify the location information), there are many other operations. Re-examine code discovery Some operations do not have to do these two methods, can wait until the page comes out and then in the background to do, that can be put into the entry activity of the onwindowfocuschanged () method to do. Before and after the optimization in the Meizu MX4 comparison, the optimization before the average value is about 5.6s, the average value of the optimization is about 1.9s.
In fact, the reasons for the slow start of the app can be varied, but the idea is the same, the main thing is to master the process of launching the app, and then for their own product analysis, which initialization steps can be done after the page.
In addition, the application OnCreate () method was found in the optimization process, which resulted in multiple unnecessary initialization and consumption of memory. The reason is that in addition to the main process in the application two sub-processes are the push child process (third-party push SDK process space), the remote sub-process (Baidu Location SDK process space) after the main process starts, the two sub-processes are restarted, the OnCreate () method is re-executed. The actual initialization code in this case has nothing to do with them. The initialize process of the Baidu SDK, which is called in our main process). The solution is to OnCreate () by judging the current process name, if not the main process, without following the logic. Believe that many products may be integrated with the push SDK and map positioning of the SDK, these SDKs will generally run in the new process, so this problem may be a lot of products are worth attention.
Scan code to follow the public number
App Launch optimization