Foreword: Recently, xiaobian is watching the technology sharing of Teacher Daiming, feeling harvest a lot. Based on recent learning, xiaobian summarizes some knowledge points on App startup optimization, and plans to implement a series of App startup optimization articles. IOS App startup optimization (1) — Understand the App startup process. IOS App startup optimization (2) — Use the “Time Profiler” tool to monitor the App startup Time. IOS App startup optimization (3) — Make your own tool to monitor the App startup Time


This article will introduce two startup methods of the App: “cold/hot startup”, the complete startup process of the App, and “Optimization thinking”.

I. “Cold start” and “Hot Start”

First, let’s distinguish between two concepts of priming.

  • Cold start: Before App is clicked to start, the process of App is not in the system yet. The system needs to create a new process and assign it to App. (This is a complete App startup process)

  • Hot startup: After cold startup, the user returns the App to the background, while the process of the App is still in the system. The process by which the user returns to the App. (Hot starts do less.)

Main differences:

The name of the The difference between
Cold start App process at startupNot inIn the system.Need to beStart a new process.
Warm start App process at startupAlso in theIn the system.Don’t needStart a new process.

Ii. Complete startup process of App (cold startup process)

It is mainly divided into three stages:

  1. main()Before function execution (pre-main phase)
  2. main()After the function executes (frommainThe function executes to the Settingsself.window.rootViewControllerExecution completed)
  3. After rendering the first screen (fromself.window.rootViewControllerExecution completed todidFinishLaunchWithOptionsEnd of method scope)

(1) Before executing the main function, the system will do the following:
  • Load the executable file. (All the.o files in the App)

  • Load dynamic link library, rebase pointer adjustment and bind symbol.

  • ObjC runtime initialization. Including: ObjC related Class registration, category registration, selector uniqueness check, etc.

  • Initialization. This includes executing the +load() method, calling functions decorated with attribute((((())), creating C++ static global variables, and so on.

To put it simply, after the App starts, the Kernel first creates a process. Next, load the executable file. (An executable is a mach-O file, which is a collection of all the.o files in your App.) At this point, you get the path to dyld (Dyld is Apple’s dynamic linker). Then, load dyLD, mainly divided into 4 steps: 1. Load dylibs: At this stage dyLD analyzes the dylib on which the application depends, finds its Mach-o files, opens and reads these files and verifies their validity, then finds the code signature to register with the kernel, and finally calls mmap() for each segment of dylib. 2. Rebase /bind: Rebase pointer adjustment and bind symbol. 3. ObjC Setup: Runtime initialization. Including registration of ObjC related classes, category registration, selector uniqueness check, and so on. Initializers: call the +load method of each ObjC class and class, call attribute((((constructor)) modified functions, and create C++ static global variables.


(2) After the execution of main function:

Upon implementation of the main function of phase, refers to: starting from the main function to perform, to the appDelegate didFinishLaunchingWithOptions method first screen rendering completes relevant methods. That is, from the main function to set the self. The window. The rootViewController execution stage.

  • Initialize the read and write operations of the configuration file on the first screen.

  • Read the big data of the first screen list;

  • Heavy calculations for the first screen rendering;


(3) After the first screen rendering is completed:

After the completion of the first screen rendering stage, refers to: didFinishLaunchingWithOptions method within the scope of all methods of execution after the first screen rendering. From set up the self. The window. The rootViewController to didFinishLaunchWithOptions method scope end.

At this stage, the first screen has been rendered.

Things to do:

  • Initialize some functions that are not required for the first screen display.

  • Optimize the main thread, first processing will be stuck in the main thread method, can not affect the user’s subsequent operations.


Third, specific optimization ideas

The startup time perceived by the user is mainly in the stage of “before main function execution” and “after main function execution to first screen rendering completion”.

Before the main function is executed, the optimization idea is as follows:

(1) Reduce use+load()methods
  • Option 1: If possible, put the +load content after rendering.

  • Use “+initialize()” instead of “+load()”. Use “dispatch_once()” instead of “+initialize()”.

How does +load() differ from +initialize()? The +load() method is called before the main() function is called, while +initialize() is called when the class is first used. +load method call priority: parent > subclass > class, and will not be overridden, will be called. The +load method is called before main(), and all class files are loaded, including the classes. + Initialize method priority: class > subclass, parent > subclass. The +initialize method overrides the +initialize method of the parent class.

(2) Merge multiple dynamic libraries

Apple recommends using fewer dynamic libraries and merging as many as possible when there are a large number of dynamic libraries in use. In terms of numbers, Apple can support up to six non-system dynamic libraries to be merged into one.

(3) Optimize classes, methods and global variables

Reduce the number of classes or methods that will not be used after loading starts; Reduce the use of C++ global variables;

After the main function is executed, the optimization scheme is as follows:

(4) Optimized function initialization before the first screen rendering

After the main function is executed, only services related to the first screen rendering are processed until the first screen rendering is complete. Other functions other than the first screen rendering should be initialized after the first screen rendering is complete.

(5) Optimize the main thread time-consuming operation to prevent screen lag.

First check the time-consuming operation on the main thread before rendering the first screen. It will take time for the operation to be delayed or processed asynchronously. Common time-consuming operations include network loading, editing, and storing images and files. For time-consuming operations to do the corresponding optimization.

Finally, I finished the App startup optimization (I), (II) and (III) standing on the shoulders of giants in the iOS industry. Thanks to Mr. Daming for his wonderful technology sharing. Attached is the link of Mr. Daiming’s course: iOS Development Master Course.