How is the memory partition of the source code of the marriage and dating system laid out

Keywords: iOS

preface

Memory management is an important part of the source code development process of marriage and dating system. Many problems are related to memory. We all know the five areas of memory, so how is it arranged? This will explain it next.

Memory partition and layout

  • Taking 4G mobile phone as an example, the five memory areas and layout are shown in the figure:

Stack area

  • Stack area is a continuous memory space. Its structure is stretched from high address to low address, following the principle of first in, last out (FILO)
  • The stack area stores local variables, functions, methods, parameters and pointers
  • The address space of stack area generally starts with 0x7
  • The stack area is automatically allocated and released by the compiler

give an example:

- (void)testStack{
    // Stack area
    int a = 10;
    int b = 20;
    NSObject *object = [NSObject new];
    NSLog(@"a == %p",&a);
    NSLog(@"b == %p",&b);
    NSLog(@"object == %p",&object);
    NSLog(@"%lu",sizeof(&object));
    NSLog(@"%lu",sizeof(a));
}

The printing results are as follows:

  • It can be observed that the memory in the stack area is continuous, and the memory expands from high address to low address, and the memory starts with 0x7

Heap

  • Heap area is a discontinuous memory space, and its structure extends from low address to high address
  • The heap area stores objects and needs to open up space. The memory in the stack area is relatively small and the heap area is relatively large, so open up space in the heap area
  • Heap address space generally starts with 0x6

give an example:

- (void)testHeap{
    // Heap area
    NSObject *object1 = [NSObject new];
    NSObject *object2 = [NSObject new];
    NSObject *object3 = [NSObject new];
    NSObject *object4 = [NSObject new];
    NSObject *object5 = [NSObject new];
    NSObject *object6 = [NSObject new];
    NSObject *object7 = [NSObject new];
    NSObject *object8 = [NSObject new];
    NSObject *object9 = [NSObject new];
    NSLog(@"object1 = %@",object1);
    NSLog(@"object2 = %@",object2);
    NSLog(@"object3 = %@",object3);
    NSLog(@"object4 = %@",object4);
    NSLog(@"object5 = %@",object5);
    NSLog(@"object6 = %@",object6);
    NSLog(@"object7 = %@",object7);
    NSLog(@"object8 = %@",object8);
    NSLog(@"object9 = %@",object9);
}

The results are as follows:

  • It can be seen from the print result that the memory in the heap area is discontinuous and starts with 0x6

Global area (static area)

  • The global area is divided into. bss segment and. data segment. The memory address generally starts with 0x1:
    • Bss segment: uninitialized global variable, static variable,
    • Data segment: initialized global variable, static variable
  • give an example:
    - (void)globalTest {
        // Global area
        NSLog(@"************ bss ************");
        NSLog(@"bssA == \t%p",&bssA);
        NSLog(@"bssB == \t%p",&bssB);
        NSLog(@"bssStr == \t%p",&bssStr);
      
        NSLog(@"************ data ************");
        NSLog(@"dataA == \t%p",&dataA);
        NSLog(@"dataB == \t%p",&dataB);
        NSLog(@"dataStr == \t%p",&dataStr);
    }
    • The operation results are as follows:

Static security test

  • It is often said that a static zone is also called a static security zone. Let's verify why it is safe and create a WSPerson class and its classification:

    // WSPerson.h
    static int ws_number = 100;
    
    @interface WSPerson : NSObject
    - (void)eat;
    + (void)sleep; 
    @end
    
    // WSPerson.m
    - (void)eat {
        ws_number++;
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
    }
    + (void)sleep {
        ws_number++;
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
    }
    
    // WSPerson+App.h
    @interface WSPerson (App)
    - (void)cate_test;
    @end
    
    // WSPerson+App.m
    - (void)cate_test {
        ws_number++;
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
    }

    The calling code in ViewController is as follows:

    - (void)constTest {
        [[WSPerson alloc] eat];
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
        ws_number = 10000;
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
        [[WSPerson alloc] eat];
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
        [WSPerson sleep];
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
        [[WSPerson alloc] cate_test];
        NSLog(@"%s __ %p __ %d", __func__, &ws_number, ws_number);
    }

    The printed results are interesting:

    Three files WS_ The memory address of number is different because you need to use Clang to view the C + + code of three files:

    From the source code of the marriage and dating system, it can be observed that each file has a ws_number, and the initial values are the same. In other words, if the same static variable is used in multiple files, the system will generate a static variable with the same initial value and different addresses, so that it will not interfere with each other and the data is relatively safe.

Constant area (. rodata)

  • The memory of the constant area has been determined at compile time. It mainly stores used string constants that are not pointed to (because string constants may be used many times in the program, memory will be allocated in advance before the program runs). Constants in the constant area are released by the system after the program ends

Code area (. text)

  • The stored program code is loaded into memory during compilation, and the code will be compiled into binary form for storage

other

    1. We can see from the figure that the memory at the bottom of the stack is 0c0000000, which is equal to 3GB after being converted to decimal system, and 1G of memory is allocated to the kernel area.
    • Kernel area: mainly used for message processing and multi-threaded operation
    1. The address of the code area starts from 0x00400000. Why doesn't it start from 0x0? Because 0x0 is nil, there will be problems with allocation from nil, so there is a reserved area before the code area
    • Reserved area: reserved for system processing nil, etc

The above is the content of the memory partition and layout of the source code of the marriage and dating system.
Statement: This article is forwarded by cloudleopard technology from matchless 3 blog. If there is infringement, please contact the author to delete it

Posted by assgar on Mon, 01 Nov 2021 02:01:07 -0700