IOS: memory management details, ios memory management details
I. Overview
In simple terms, internal management is the management of the computer's internal storage. Von noriman's structure points out that the computer is composed of the host, controller, memory, input and output devices. Taking the iPhone 8 as an example, the memory and controller are combined with the CPU (central processor) and the running memory is 3 GB LPDDR4 RAM. What we usually call 64 GB or 128 GB memory is the storage ROM.
The von norimman structure also points out what memory is used to store. For our developers, commands are basically code logic, and data is basically variables and constants.
II. General memory Principle
In simple terms, there are two major parts: commands and data. Further subdivided into five parts: Code (command), initialization data zone, uninitialized data zone, heap, and stack.
Code (command, text) is needless to say. The most static is read-only;
Initialize data. It is a variable or constant with an initial value;
If no data is initialized, only the unassigned variables are declared. All the variables are 0 before running. The reason for splitting them separately is estimated to be performance considerations, because these variables are all 0 and do not need to be placed in the program package, copy is not required;
Stack, program running record, each thread, that is, each execution sequence has one (it is easiest to see crash log), which can be determined during compilation, another feature is that the data here can be lost without pointers;
Heap, the most flexible memory zone, a lot of uses, Dynamic Allocation and release, cannot be determined in advance during compilation, our Objective-C object is like this, all exist here, objects in the heap are usually accessed by pointers. Pointers come from the thread stack, but they do not belong to a specific thread. The heap is also the basic support for complex runtime processing, there is also that ARC, MRC, and "who assigns and who releases" are all about managing stacked objects.
Iii. iOS Memory Management
In fact, iOS memory management is similar to other operating systems. As described in the Apple documentation, we will focus on the heap memory allocation.
IOS memory management is divided into several layers. From the system to the libmalloc and ARC environments, the compiler will also help developers optimize the processing as much as they can.
First, like other systems, the operating system kernel manages the ing of virtual storage to physical memory, and implements memory paging, 4 K per page. Multiple pages form a unified management of memory blocks. The objects to be managed are VM objects, including pager, size, and resident pages. All memory allocations will be processed by the system (such as vm_allocate/mach_vm_allocate ).
In development, iOS uses libmalloc based on the system kernel. Whether Objective-C's [NSObject alloc] or C code's memory allocation, the task will all fall into the malloc library, so will the release. In the end, all will use the free () in the malloc library (). Many same-family functions in the malloc library can dynamically allocate memory. The malloc library defines the concept of zone and implements different zones (such as nano zone and scalable zone ), memory Allocation and release management of n ano, tiny, small, and large memory based on memory requirements. By default, when malloc is called for the first time, the system generates a d efault zone, which will be allocated here by default. For example, the malloc_zone_xxx () function allocates specific zones and executes zone-> xxx (). Each zone performs cache processing on allocated memory in the form of a linked list to avoid frequent requests to the kernel system. The internal implementation of malloc is open-source. If you are interested, you can check it out.
When the memory is tight, read-only data that can be reloaded is directly cleared. For writable data, you can only manage and maintain it by yourself through the App. When the memory is insufficient, iOS will initiate memory warning to the App, and will directly kill the program if it does not release enough memory.