[iOS memory management-several major areas of memory]

Preface

The first chapter of iOS memory management, learn about the five major partitions of iOS memory.

Overview

In iOS, memory is mainly divided into five major areas: stack area, heap area, global area/static area, constant area and code area. The overview diagram is as follows. Please add image description
As shown in the figure above, the code area is stored in the low address segment, while the stack area is stored in the high address segment, and the partitions are not continuous.

1. Stack

1.1 Introduction

  • The stack is a continuous memory area stored from high address to low address . The characteristics are:先进后出(FILO)
  • The address space of the stack in iOS is 0X7/ 0X16the beginning
  • The stack area is generally there 运行时分配内存,内存空间由系统管理, that is, the variables will be released after they exceed their own scope.
  • Including 函数内部定义的局部变量以及方法参数(方法的默认参数 self,cmd)and so on are also stored in the stack area.

1.2 Advantages and Disadvantages

  • Note that the memory in the stack area is allocated and managed by the system, so it will be allocated and released by the system itself without causing memory fragmentation, making it faster and more efficient.
  • However, the memory size of the stack is limited by the system, which makes it not very flexible. The size of the iOS main thread stack is 1MB, other threads are 512KB, and Mac is 8M.Please add image description
- (void)testStack {
    
    
    int a = 10;

    NSLog(@"a == %p size == %lu",&a,sizeof(a));
    NSLog(@"方法参数 self:%p",&self);
    NSLog(@"方法参数 cmd:%p",&_cmd);
}

Please add image description

2. Heap area

  • The heap is a discontinuous memory area from low address to high address. It is very similar to the structure of a linked list (easy to add and delete but not easy to query). It is characterized by first-in, first-out FIFO.
  • The heap address starts with 0X6 and dynamically allocates space.
  • Things stored in the heap need to be managed and released manually. If not released in time, memory leaks will occur.
  • In OC, alloc and new will open up space for objects on the heap.
- (void)testHeap {
    
    
    NSObject *object1 = [NSObject new];
    NSObject *object2 = [NSObject new];
    NSLog(@"object1 = %@",object1);
    NSLog(@"object2 = %@",object2);
}

Please add image description

The difference between stack and heap

1. What are the advantages and disadvantages of each?

  • Stack: Automatically allocated and released by the compiler, it is fast and does not produce memory fragmentation . The advantage is that it is fast and efficient, but the disadvantage is that it is sometimes limited and the data is inflexible.
  • Heap: It is allocated and released by the programmer. It is slower and prone to memory fragmentation, but it is the most convenient to use. The advantages are flexibility and convenience, and wide data adaptability, but the efficiency is somewhat reduced.

2. How does the system respond after application?

  • Stack: Storage: Each function will ask the operating system for resources when it is executed. The stack area is the memory when the function is running . The variables in the stack area are allocated and released by the compiler. The memory is allocated as the function runs. The release is completed automatically by the system. As long as the remaining space of the stack is greater than the requested space, the system will provide memory for the program, otherwise an exception will be reported to prompt stack overflow.
  • Heap: The operating system has a linked list that records free memory addresses. When the system receives the application from the program, it will traverse the linked list, look for the first heap node with a space larger than the requested space, then delete the node from the free node linked list, and allocate the space of the node to program. Since the size of the found heap node may not be exactly equal to the requested size, the system will automatically put the excess part back into the free linked list.

3. Are there any restrictions on application size?

  • Stack: The stack is a data structure that extends to lower addresses and is a continuous area of ​​memory. The address of the top of the stack and the maximum capacity of the stack are predetermined by the system. The size of the stack is 2M (some say 1M, in short it is a constant determined at compile time). If the applied space exceeds the remaining space of the stack , will prompt overflow. Therefore, the space available from the stack is smaller.
  • Heap: The heap is a data structure that extends to high addresses and is a discontinuous memory area. This is because the system uses a linked list to store free memory addresses, which are naturally discontinuous, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap is limited by the virtual memory available in the computer system. It can be seen that the space obtained by the heap is more flexible and larger.

3. Global/static area

  • This area is a memory space allocated during compilation . In iOS, it generally starts 0x1with. During the running of the program, the data in this memory always exists and is released by the system after the program ends.
  • uninitialized global and static variables,即BSS区(.bss)。
  • Initialized global variables and static variables,即数据区(.data)。

Among them, global variables refer to variable values ​​that can be dynamically modified at runtime, while static variables are variables modified by static, including static local variables and static global variables. I have learned about the
static keyword before and have an in-depth understanding of the Static keyword.

int clB;
static int bssB;
int initClB = 10;
static int initBssB = 11;
- (void)testStatic {
    
    
    NSLog(@"clA = %p", &clB);
    NSLog(@"bssB = %p", &bssB);
    NSLog(@"initClB = %p", &initClB);
    NSLog(@"initBssB = %p", &initBssB);
   

}

Please add image description
Both clB and bssB are uninitialized and have consecutive addresses in memory, with a difference of 4.
Both initClB and initBssB are initialized data, and the memory addresses are also consecutive.

4. Constant area

  • This area is a memory space allocated during compilation . During the running of the program, the data in this memory always exists and is released by the system after the program ends.
  • Store constants:整型、字符型、浮点、字符串等。
  • The constant area is a memory space allocated during compilation. It is released by the system after the program ends. It mainly stores:
    ■ String constants that have been used and do not point to
    ■ String constants may be used multiple times in the program, so in Memory is allocated in advance before the program is run.

5. Code area

  • This area is a memory space allocated during compilation. During the running of the program, the data in this memory always exists and is released by the system after the program ends .
  • The code when the program is running will be compiled into binary and stored in the code area of ​​​​the memory.

Summarize

We have a preliminary understanding of the memory area of ​​iOS, mainly the difference and connection between stack and heap that we need to understand clearly.

Guess you like

Origin blog.csdn.net/weixin_61639290/article/details/131730226