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 to
4G
Cell phones, for exampleMemory five areas and layout
As shown in the figure:
The Stack area (Stack)
- The stack area is a block
continuous
Memory space, its structure isFrom a high address to a low address
Stretch, followFirst in last Out (FILO)
The principle of - The stack area stores
A local variable
.function
.methods
.parameter
.Pointer to the
- The address space of the stack area is generally defined as
0x7
At the beginning - The stack area by
The compiler
Automatically 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 is
0x7
At the beginning
The Heap area (Heap)
- The heap area is one piece
discontinuous
Memory space, its structure isFrom a low address to a high address
extension - The heap store is
object
, you need toOpen the space
Stack area memory is relatively small, heap area is relatively large, so open up space in the heap area - The heap address space is generally in
0x6
At 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, and
0 x6 beginning
Global region (static region)
- The global region is subdivided into
The BSS
and.data section
, the memory address is generally determined by0x1
At the beginning:Bss
Section:uninitialized
Global variables, static variables,Data
Section:initialized
Global 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 region
Compile time
It has been determined that the main storage has been used, and there is no pointingString constant
Because string constants may be used multiple times in a program, theBefore the program runs
Memory 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 into
Binary form
For storage
other
-
- We can see from the figure that the memory at the bottom of the stack is zero
0c0000000
, todecimal
After equal to3GB
, as well as1G
Memory allocated toThe kernel area
.
The kernel area
: MainlyMessage processing
And multithreaded operations
- We can see from the figure that the memory at the bottom of the stack is zero
-
- The address of the code area is
0x00400000
Start? Why not start0x0
To start? because0x0
isnil
, fromnil
There is a problem with starting allocation, so there is a retention area before the code area
The reservations
: Reserved for system processingnil
Etc.
- The address of the code area is
Related interview questions
-
- Why do you say
Static
Does the modified variable take up no memory?
- Answer: because
Static
Decorated variable The decorated variable is in the global area and does not occupy its allocated memory
- Why do you say
-
- 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 speed
It’s very fast