Memory management portal 🦋🦋🦋

Memory management analysis (a) – MANUAL memory management in the MRC era

Memory management analysis (two) – timer problem

Memory management analysis (four) — Autorelease principle analysis

Memory management analysis (5) — weak pointer implementation principle

After iOS applications are installed, they are saved as mach-O files on the iOS device. When the application is started, the corresponding Mach-O files are loaded into memory. Here, to introduce the iOS program memory layout. First look at the picture below

If you have knowledge of iOS reverse engineering, this topic should be like chopping a melon or chopping a vegetable for you. If you don’t, there is another way to verify the above diagram. Look at the test code below

//*********************main.m**********************

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int a = 10;                   // Global variables are initialized

int b;                        // Global variables are not initialized

int main(int argc, char * argv[]) {
    @autoreleasepool {
        static int c = 20;    // Static variables are initialized
        
        static int d;         // Static variables are not initialized
        
        int e;                // Uninitialized local variables

        int f = 20;           // Local variables are initialized

        NSString *str = @"123";// String constants
        
        NSObject *obj = [[NSObject alloc] init];// Dynamically allocate (instance objects) by alloc
        
        NSLog(@"\n&a=%p\n&b=%p\n&c=%p\n&d=%p\n&e=%p\n&f=%p\nstr=%p\nobj=%p\n",
              &a, &b, &c, &d, &e, &f, str, obj);
        
        returnUIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}Copy the code

In the above example, there are a variety of variables, we print to observe their memory status, the result is as follows

2019- 0826 - 16:20:25.294729+0800Interview03- Memory Layout [5467:359025] 
&a=0x10fad3db8
&b=0x10fad3e84
&c=0x10fad3dbc
&d=0x10fad3e80
&e=0x7ffee012e1fc
&f=0x7ffee012e1f8
str=0x10fad3068
obj=0x600002d20160
Copy the code

These variables are arranged in ascending order of memory location and compared to the memory layout diagram as shown belowThis validates the conclusion thrown out in the first place.

Memory management portal 🦋🦋🦋

Memory management analysis (a) – MANUAL memory management in the MRC era

Memory management analysis (two) – timer problem

Memory management analysis (four) — Autorelease principle analysis

Memory management analysis (5) — weak pointer implementation principle