I. Introduction to App startup optimization
1. Background: 1) First experience; 2). Eight-second rule (70% of users will give up waiting if they do not open the application within eight seconds); 2. Startup Category: App Startup Time Click Event->IPC->Process.start->ActivityThread->bindApplication->LifeCycle->ViewRootImpl 2) Hot start: the fastest background -> foreground 3). Warm start: quickly restart the Activity's life cycle; Cold startup tasks: 1. Start the App. 2. Load a blank Window. 3. Create a process. 4. Create Application; 5. Start the main thread. 6. Create MainActivity; 7. Load layout; 8. Set up the screen; 9. First frame drawing; Optimization direction :Application and Activity lifecyclesCopy the code
Two. Starting time measurement method
Adb command :adb shell am start -w packagename/ first screen ActivityCopy the code
ThisTime: the last Activity start time; TotalTime: total Activity startup time; WaitTime: the total time it takes for AMS to start the Activity; Features: 1. Easy to use offline, can not be brought online; 2. Non-precise, precise time; 2. Manual typing: buried point at the start and buried point at the end of the start, the difference between the two is mistaken :onWindowFocusChanged is only the first frame time of the Activity, does not mean that the interface has been displayed; True answer: The real data is displayed, the first Feed is displayed, and the first frame time is still some distance from the real data. Starting point: Application - > attachBaseContext (); End point: the real first data is displayed; Features: accurate, can be carried online, recommendedCopy the code
3. Start optimization tool selection
TraceView 1). Graphical display of execution time, call stack, etc.; 2). Comprehensive information, including all threads; Method of use: 1. Debug. StartMethodTracing (" name "); 2.Debug.stopMethodTracing(); 3. The generated files in sd card: Android/data/packagename/files features: 1. Run time overhead is heavy and the whole thing slows down; 2. The direction of optimization may be biased; 2. Systrace features: 1. Lightweight, low overhead; 2. Intuitive reflection of CPU utilization; 3. Differences between walltime and CPUtime: Walltime: code execution time; Cputime: the time the code consumes CPU (key metric); Example: Lock conflictsCopy the code
Four. How to get method time gracefully
Background: It is necessary to know the time consuming implementation of all methods in the start-up stage: manual burying point conventional implementation: highly intrusive and heavy workload; AOP(section-oriented programming) implementation: features 1. Unified treatment for the same kind of problems; 2. Non-intrusive add code; 3. Easy to modify; Join points: execution points at program runtime that can be used as facets: 1. Function call, execute; 2. Obtain and set variables. 3. Class initialization; PointCut: conditional JoinPoints Advice: a Hook where code is inserted; 1.Before: execute Before PointCut; 2. Execute After:PointCut; 3. Execute before and after the PointCut.Copy the code
Five. Asynchronous optimization in detail
1.Theme switch: Feel fast, use blank Window;Copy the code
2. Core idea of asynchronous optimization: sub-threads share tasks of the main thread and reduce time in parallelCopy the code
Notes: 1). Does not meet asynchronous requirements; 2). It needs to be completed at a certain stage; 3). Distinguish BETWEEN CPU intensive and IO intensive tasks; 3. The optimal solution of asynchronous optimization: 1). The pain point of conventional asynchronous: a. B. The scenario is difficult to handle (dependency); C. High maintenance cost; 2). Initiator (key) Initiator process: 1. Task-oriented code, startup logic abstracted into Task; 2. A directed acyclic graph is generated by sorting all task dependencies; 3. Multithreading executes according to the sorted priority; 4. Resolve tasks that must be executed in the main thread and have dependencies;Copy the code
4. Pain points of conventional initialization of delayed initialization scheme: a. Inconvenient timing control; B. Lead to Feed lag; Core idea: Batch initialization of delayed tasks 1. IdleHandler is used for idle execution.Copy the code
Sixth, optimize the general policy
Asynchronous, lazy, lazy loading; 1. Difference between Wall time and CPU time: CPU time is the optimization direction; 2. Run all cpus according to systrace and CPU timeCopy the code
3. Improvement of monitoring: 1). Multi-stage online monitoring (Application life cycle interval) 2). 4. Convergence startup code modification permission: Review or notify 5 when modifying the startup code in combination with Ci. Other solutions 1. Load SharedPreferences in advance; 1).Multidex is loaded before, and CPU in this stage is utilized, but CPU in this stage is not fully utilized; 2). Override getApplicationContext() to return this; 1). The child processes share CPU resources, which causes CPU strain in the primary process. 2). Pay attention to the startup sequence: avoid starting four components before App onCreate, such as Service and ContentProvider; 1).class.forname () only loads reference classes of the Class itself and its static variables; 2).new class instance, can be added to load the class member variable reference class; 4. Suppress GC during startup phase 5.CPU frequency lock: pull up CPU frequencyCopy the code
7. To summarize
1. The acquisition method time-consuming: 1). The pain point of conventional implementation 2).AOP implementation solves the pain point of conventional implementation; 3). Note that there is no difference between wall time and CPU time. 3). Pay attention to the pain points and the understanding of the advantages of the starter. 3. Delayed initialization: 1). Pain point of conventional scheme implementation; 2). Combined with IdleHandler(executed when the system is idle); 4. Other optimization schemes: 1) asynchronous SharedPreferences in advance; 2). The child process is not started in the startup phase; 3). Asynchronous class loading in advance;Copy the code
Eight. Start optimizing the mock interview
1. How do you do startup optimization? 1). Analyze the current situation and identify problems; 2). Targeted optimization; 3). Long-term optimization effect; 2. How is asynchrony? Is asynchrony problematic? 1). Experience the evolution process; 2). Introduce the initiator in detail; 2. After startup optimization, what are the overlooked points? The difference between Wall time and CPU time Attention should be paid to the optimization of delayed initialization and the display of the first frame. Introduction of black technology: asynchronous class loading CPU is not running full increase the number of CPU cores and frequency; 3. What is the solution to slow startup caused by version iteration? 1). Initiator; 2). Combined with CI, code review; 3). Improve monitoring (Application activity lifecycle monitoring);Copy the code