Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

In the last article we briefly introduced the structure of the executable file Mach-O. In this article we’ll look at some memory theory, starting with the era of physical memory management.

Direct physical memory management

This loading method is relatively simple and violent. When we open an application, the entire application will be loaded into physical memory. The application accesses the actual physical memory address, as shown in the following diagram:

This management style is understandable, but there are two problems:

  • Insufficient memory: As shown in the figure above, application A, B, and C have been loaded into memory. If we want to load application E into memory now, the remaining physical memory space is insufficient, which will cause application E to fail to open. We must close one of the applications A, B, and C to open application E properly.
  • Application security problem: because now the application accesses physical memory directly, if B is a malicious software, it can find the memory address accessed by application C through the offset of its own application’s memory address, so that the security of application C cannot be guaranteed.

In order to solve the problem of insufficient memory, the concept of paging managed memory is put forward, that is, the memory used by each application is managed in pages, and only the pages used are loaded into the memory each time, which greatly increases the utilization of memory, as shown in the diagram below:

As you can see, using paging management can solve the problem of running out of memory. More applications can be active at the same time, but this approach can cause a new problem, that is, the memory address of a single application access is not continuous, which increases the complexity of management, application security problems still exist. Hence the virtual memory management approach we now use.

Virtual Memory Management

Once each application runs, it will have its own virtual address space, the size of which is determined by the computer hardware platform, specifically by the number of CPU bits. The hardware determines the theoretical upper limit of the address space. Note that in theory, a 64-bit iPhone can have a virtual memory address of 2^64 bytes, but models after the iPhone 6S have a virtual memory size of 4GB(1GB for the operating system and 3GB for applications).

Virtual memory management is sketched as follows:

You can see that each application has its own independent and continuous virtual memory space. Virtual addresses are mapped through the MMU hardware to find physical addresses, enabling access to physical addresses. There are three things that happen from the point of view of the program clicking to start up to running the operating system:

  • Creating a separate virtual address space is really just assigning onePage directoryThe mapping relationship can be set later when a page error occurs in the program.
  • Read the executable header and map the virtual space to the executable, as we’ll discuss laterMissing pageError, when a missing page error occurs, the operating system will allocate a physical page from physical memory, and then put theMissing pageRead from disk to memory, in setting the mapping between the missing virtual page and the physical page, so as to run normally, this is to use the mapping between the virtual space address and the executable file.
  • Set the CPU instruction register to the executable file entry address, start running.

It can be seen that virtual memory solves the problem of insufficient memory and application security at the same time, because each application is independent virtual memory, can not access the memory of other programs will not be the purpose of attack.

Missing page error

We know that when paging is used, the application is not loaded into memory all at once. When the data or instruction we access is not in memory, we need to load the page on which the data or instruction is located, which is a Page fault.

It is mentioned in the Principle of iOS startup optimization of Douyin team that it takes milliseconds for a PageFault, but there will be a large number of PageFaults when our application is started. In this way, we can use binary rearrangement to reduce the number of pagefaults, so as to achieve the goal of startup optimization. Those of you who are interested can look into it.

Application virtual memory space distribution

The following figure shows the virtual memory distribution of applications:

You can see that the application can access five areas:

  • Code area (.text): code loaded into memory through an image file, read-only and executable.
  • Initialized data (.data): initialized data (global variables, static variables) loaded into memory through an image file. The permission can be read, written, and executed.
  • Uninitialized data (.bass): Uninitialized data (global variables, static variables) loaded into memory through an image file with read, write, and execute permissions.
  • The area where objects are created by alloc, etcARCMemory management is the management of this area, permissions can be read and written, executable, but cannot be restored through image file loading, address expansion up.
  • Stack area (stack): the main storage is the function, method and some parameters, permissions can be read and write, cannot be executed, cannot be restored through the image file, address expansion downward.

Note: A retention area is not a single memory area, but a general name for memory areas that are protected from access. For example, in most operating systems, extremely small addresses are usually accessed out of order, such as NULL.

conclusion

This paper we mainly introduced the memory of some theory knowledge, the current mainstream memory management scheme of virtual memory and application is loaded into the memory of the distribution of five regions had certain understanding, it in our understanding of the application of load, and the content of the memory management are of great help, iOS application loading process and memory management scheme in a later article continues to explore.