preface

Memory management is an important part of the development process, and many problems are related to memory. We all know the five regions of memory, so how it is laid out, this will explain it.

Memory partitioning and layout

  • In order to4GCell phones, for exampleMemory five areas and layoutAs shown in the figure:



The Stack area (Stack)

  • The stack area is a blockcontinuousMemory space, its structure isFrom a high address to a low addressStretch, followFirst in last Out (FILO)The principle of
  • The stack area storesA local variable.function.methods.parameter.Pointer to the
  • The address space of the stack area is generally defined as0x7At the beginning
  • The stack area byThe compilerAutomatically allocate and free memory

For example:

- (void)testStack{
    / / the 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));
}
Copy the code

The print result is as follows:



  • It can be observed that the stack area memory is continuous, and the memory is expanded from high address to low address, and the memory is0x7At the beginning

The Heap area (Heap)

  • The heap area is one piecediscontinuousMemory space, its structure isFrom a low address to a high addressextension
  • The heap store isobject, you need toOpen the spaceStack area memory is relatively small, heap area is relatively large, so open up space in the heap area
  • The heap address space is generally in0x6At the beginning

For 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);
}
Copy the code

The results are as follows:



  • It can be seen from the printed result that the heap memory is discontinuous, and0 x6 beginning

Global region (static region)

  • The global region is subdivided intoThe BSSand.data section, the memory address is generally determined by0x1At the beginning:
    • BssSection:uninitializedGlobal variables, static variables,
    • DataSection:initializedGlobal variable, static variable
  • For example:
    - (void)globalTest {
        / / the 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);
    }
    Copy the code
    • The running results are as follows:



Static safety test

  • It is often said that static zones are also called static security zones. Let’s verify why it is safe by creating 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);
    }
    Copy the code

    The call 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);
    }
    Copy the code

    The printout results are interesting:



    The memory address of ws_number is different from that of ws_number. The reason is that we need to check the C++ code of ws_number.



    You can observe from the source that each file has a ws_number global variable with the same initial value. In other words, if a static variable is used in multiple files, the system will generate static variables with the same initial value and different addresses. In this way, the static variables used in different files will not interfere with each other and the data is relatively secure.

The constant area (. Rodata)

  • Exists in constant regionCompile timeIt has been determined that the main storage has been used, and there is no pointingString constantBecause string constants may be used multiple times in a program, theBefore the program runsMemory is allocated ahead of time). Constants in the constant area are released by the system at the end of the program

Code area. (text)

  • Stored program code that is loaded into memory at compile time and compiled intoBinary formFor storage

other

    1. We can see from the figure that the memory at the bottom of the stack is zero0c0000000, todecimalAfter equal to3GB, as well as1GMemory allocated toThe kernel area.
    • The kernel area: MainlyMessage processingAnd multithreaded operations
    1. The address of the code area is0x00400000Start? Why not start0x0To start? because0x0isnil, fromnilThere is a problem with starting allocation, so there is a retention area before the code area
    • The reservations: Reserved for system processingnilEtc.

Related interview questions

    1. Why do you sayStaticDoes the modified variable take up no memory?
    • Answer: becauseStaticDecorated variable The decorated variable is in the global area and does not occupy its allocated memory
    1. How is stack area memory located based on experience?
    • A: The stack area is located through the SP register, and the heap area is located through the register to this address that contains memory. The location of memory is determined by the address, so the stack area and the heap area speed.Stack speedIt’s very fast