In the daily development process of iOS, we often encounter APP startup is too slow. How do we optimize and solve it? To solve this problem, we first need to understand what is done during the startup of the APP. After understanding the whole process, we should look for solutions at each step of the startup.

Virtual memory

Early computer data access is access to physical memory, direct access to physical addresses. But this scheme has two obvious drawbacks:

  • Data insecurity;
  • Insufficient memory;

Apple proposed ASLR technology to solve the problem of data insecurity. ASLR concept: Address Space Layout Randomization is a security protection technology against buffer overflow. By randomizing the linear area Layout of heap, stack and shared library mapping, it increases the difficulty for the attacker to predict the destination Address. It is a technique to prevent the attacker from locating the attack code directly and prevent overflow attack. The purpose is to configure the data address space in a random way, so that some sensitive data can be configured to an address that the malicious program cannot know in advance, so that the attacker is difficult to attack. Due to the existence of ASLR, the loading address of executable files and dynamic linked libraries in virtual memory is not fixed every time they are started, so the resource pointer in the image needs to be fixed at compile time to point to the correct address. The correct memory address = ASLR address + offset value. Apple has added an intermediate layer between processes and physical memory. This intermediate layer is called virtual memory, which is used to manage physical memory when multiple processes exist at the same time. Improved CPU utilization, enabling multiple processes to load simultaneously and on demand. So virtual memory is essentially a mapping between virtual addresses and physical addresses. Virtual memory has the following features: 1, each process has an independent virtual memory, the address is from 0, size is 4 g fixed, each virtual memory is divided into a one page (page size is 16 k in iOS, the other is 4 k), is loaded every time a page to load, is unable to visit each other between processes, ensure the security of data between processes. 2, when the CPU needs to access data, the first is to access the virtual memory, and then through the virtual memory address, which can be understood as in the table to find the corresponding physical address, and then on a visit to the corresponding physical address 3, if during a visit to the content of the virtual address is not loaded into physical memory, page fault happens unusually (Pagefault), Blocks the current process by loading data into physical memory before addressing it for reading. In this way, memory waste is avoided. 4. Only some functions are active in a process, so you only need to put the active parts of the process into physical memory to avoid wasting physical memory

The APP launched

APP startup can be divided into cold startup and hot startup cold startup: when the phone memory does not contain any APP data, the first startup is cold startup. Restart the phone and start the app to achieve a cold boot effect. Hot start: Cold start accident The startup optimization we studied was actually for cold start. The process is divided into two stages:

  • pre-main, also known as the DYLD load process, in which the system loads the executable into memory and performs the linking and loading process.
  • Start with the main function and end with the didFinishLaunching load until the first page is loaded.

Pre-main load optimization

Add environment variables by configuring the Scheme projectDYLD_PRINT_STATISTICSThe console can print the time of each stage of pre-main loading processThe console prints the followingNow let’s discuss the significance of each stage of loading

dylib loadingThis process mainly loads the dynamic library

rebaseAll methods and function calls within the executable file have an address, which is actually an offset address. Once it is run in memory, each time the system randomly assigns an ASLR address value, the actual address of the function is the ASLR address + cheap address

binding: symbol binding, binding is the process of assigning values to symbols

ObjC setup: Process of loading OC objects

initializer: loads the load method and constructor

Conclusion:

1. Use dynamic libraries as little as possible in the project, and try to merge more than 6 dynamic libraries

2. Avoid creating unnecessary OC classes

3. Avoid doing unnecessary work in the load method

4. Use C++ constructors as little as possible

The main function is later loaded with optimizations

In the didFinishLaunching method of Appdelegate we usually put some sort of database initialization, environment configuration, theme configuration, third party configuration, setting the root attempt controller, etc. 1. In order to improve the startup speed, we need to optimize the business code at this stage. Non-essential business, no call or delayed call. 2. Use multithreading to place business processes that do not affect the startup process in child threads. Initialize the process using lazy loading. 3. Build the page using pure code and use less story version.