The overview
In iOS, memory is mainly divided into five areas: stack area, heap area, global/static area, constant area and code area. The layout structure is shown as follows:
A, stack area
- The stack is
A contiguous area of memory
fromFrom a high address to a low address
For storage, follow the first in last out (FILO) principle. - The stack address space in iOS is
0X7
At the beginning. - Stack areas are usually allocated at run time, and memory space is allocated by
System management
Memory is automatically freed when the declared variable is out of scope. - Function defined internally
A local variable
,Method parameters
Default parameter in method:self
,cmd
), are stored in the stack area.
The advantages and disadvantages
- Advantages: The stack is automatically allocated and released by the system, no memory fragmentation, so fast and efficient.
- Disadvantages: stack memory size is limited, data inflexible, iOS
The main thread stack
The size is1MB
.Other threads
is512KB
MAC only,8M
The information comes fromThe official documentation:
validation
- (void)testStack { int a = 10; NSLog(@"a == %p size == %lu",&a,sizeof(a)); NSLog(@" method argument self: %p",&self); NSLog(@" method argument CMD: %p",&_cmd); } A == 0x7FFeee47d75c size == 4 Method parameter self: 0x7FFeee47d768 Method parameter CMD: 0x7FFeee47d760Copy the code
Second, the heap area
- The heap is
A discontinuous memory region
fromFrom a low address to a high address
To store, similar toChain table structure
(easy to add and delete, not easy to query), follow the first in first out (FIFO) principle. - The address space of the heap in iOS is
0x6
At first, the allocation of space is always dynamic. - Developers need to pay attention to the life cycle of variables, if not released in time, will cause memory leaks, only when the program ends by the system to recycle.
OC
The use ofalloc
ornew
Open up space to create objects.C
Language usemalloc
,calloc
,realloc
Allocated space, neededfree
Release.
The advantages and disadvantages
- Advantages: Flexible access to space, large memory allocation.
- Disadvantages: Manual management is required, which is slow and prone to memory fragmentation.
validation
- (void)testHeap { NSObject *object1 = [NSObject new]; NSObject *object2 = [NSObject new]; NSLog(@"object1 = %@",object1); NSLog(@"object2 = %@",object2); } Object1 = <NSObject: 0x600002B8F430 > object2 = <NSObject: 0x600002B8F420 >Copy the code
Global/static zone
- This area is the memory space allocated at compile time
iOS
In general with0x1
At the beginning, during the running of the program, the data in this memory always exists, after the program is released by the system. - Uninitialized global and static variables, i.e
BSS area
(BSS). - Initialized global and static variables, i.e
Data area
(data).
validation
int clB = 10; static int bssB = 10; - (void)test { NSLog(@"clA == %p",&clA); NSLog(@"clB == %p",&clB); } clA == 0x104131424 clB == 0x104131408Copy the code
4. Constant area (i.e. Rodata)
- This area is the memory space allocated at compile time. The data in this memory remains during the program running and is released by the system after the program ends.
- Store constants:
The integer
,character
,Floating point
,string
And so on.
validation
Use MachOView to view constants:
Five, code area
- This area is the memory space allocated at compile time. The data in this memory remains during the program running and is released by the system after the program ends.
The code when the program is running
It will be compiled into binary and stored in the code area of memory.
validation
View the code area with MachOView: