1. What does App startup do? There are two types of iOS startup: hot startup and cold startup
- Cold start: before the App is clicked, it is not in the process system and needs to create a process for it to start, which is a complete start process
- Hot startup: After a cold startup, the App goes back to the background. At this time, the App process is still in the system. During the process of restarting the App, very few things are done
Ii. The startup of App mainly includes three stages
- Before main() (pre-main phase)
- Dylib loading: loads executable files
- Rebase /binding: Adjusts the rebase pointer to bind to the bind symbol
- Objc Setup: Objc Runtime initialization, Class registration, Category registration, Selector detection, etc
- Initializer: initialization, +load() method execution, C++ global variable initialization, etc
- After main() is executed (main is executed before setting rootViewController)
- TabbarController load
- The first page Controller is loaded, and its children are loaded
- After the completion of the first screen rendering (set after the rootViewController to didFinishLaunchWithOptions)
- Initialize some additional functionality (third-party libraries, etc.)
Three, start time measurement
-
Screen recording tool: To record the App startup process, use video clip tool to unframe and calculate. In this way, it is most intuitive to see the total time from clicking the icon of the App to appearing on the home page. However, it is only from sensory experience to App opening quickly or slowly, lacking concrete and stronger evidence
-
Pre-main: Add DYLD_PRINT_STATISTICS to Scheme -> Environment Variables and set it to YES to print the pre-main time. And what you do in the pre-main phase
- Instruments: Use the App Launch tool for Instruments to see the startup time after pre-main
- Switch to the type of App Life Cycle, and you can see the overall time consumption of each stage of App, as shown in the figure:
- Switch to the Profile type and you can see the elapsed time of each thread/symbol (function) of the App, as shown in the figure below:
- Use Hook objc_msgSend, specific implementation you can refer to Daming big guy’s article. The original link
Four, how to optimize?
- Before main() (pre-main phase)
- dylib loading:
- Rebase /binding: Reduce or merge dynamic libraries, replacing dynamic libraries with static ones
- Objc Setup: Reduce the use of invalid classes and methods and merge categories
- Initializer: Reduces overrides of the +load() method and can be placed in +initialize()
- After main() is executed (main is executed before setting rootViewController)
- Methods to reduce the viewDidload method on the home page
- Create VC recursively as little as possible on your home page
- After the completion of the first screen rendering (set after the rootViewController to didFinishLaunchWithOptions)
- Third party libraries, version control, etc
5. Advanced has been updated with portal attached
IOS Startup Optimization – Advanced