In IOS, memory is divided into five areas:Stack area, heap area, global area, constant area and code area

Stack area
-
High address to low address extension
System data structure, correspondingProcess or thread
Is unique - It’s a piece
Contiguous memory area
, followFirst in last out (Filo)
principle -
Runtime allocation
, in IOSBeginning with 0x7
-
Automatically allocated and released by the compiler
, mainly used to store:Local variables and function parameters (such as hidden parameters (ID self, sel _cmd))
Advantages and disadvantages
- Advantage: because the stack is
Compiler automatically allocates and releases
No memory fragmentation,Fast and efficient
- Disadvantages: stack
Limited memory size and inflexible data
,- The size of IOS main thread is 1MB, and that of other threads is 512KB
- 8MB on MAC
Heap
- The heap is made of
Low address to high address extension
Data structure of -
Discontinuous memory area
, similarLinked list structure
(easy to add and delete, not easy to find), followFirst in first out (fofi)
principle -
Runtime assigned
, in IOSBeginning with 0x6
-
Programmers dynamically allocate and release
, if the programmer does not release it, it will be recycled by the system after the program is completed, which is mainly used to store:Open space to create objects
- When accessing memory in the heap, you generally need
Read the pointer address of the stack area through the object
, and then throughPointer address access heap
Advantages and disadvantages
- advantage:
Flexible and convenient, wide adaptability
- Disadvantages: need
Manual management, slow speed
, easy to produceMemory fragmentation
Global area \ static area (. BSS &. Data)
-
Compile time allocation
The memory space of the program. During the program running, the data always exists. After the program endsSystem release
- In IOS, generally
Start with 0x1
Read / write area
Mainly used to store:
-
uninitialized
ofglobal variable
andStatic variable
, i.e. BSS area -
Initialized
ofglobal variable
andStatic variable
, i.e. data area-
global variable
It means that the variable value can beThe runtime is dynamically modified
, andStatic variable
yesStatic modification
Variable containingStatic local variable
andstatic global
-
Constant area (. Rodata)
-
Compile time allocation
Memory space,The system will release automatically after the program ends
Read only area
- Main storage: used and not pointed
string constant
- String constant because it may be in the program
Multiple use
, allAllocate memory in advance before the program runs
Code area (. Text)
Compile time allocation
Read only area
- Main storage:
Program running code
, the code will be compiled intoBinary memory
Function stack (stack frame)
A separate contiguous area of memory occupied by a function while it is running and incomplete
-
Each thread has a dedicated stack space
, the stack space can be freely used during the thread, and the function of the current threadshare
Change stack space,The stack space used by each function is a stack frame, and all stack frames constitute the complete stack of this thread
-
The function is called on the stack
, the relevant information of each function (local variables, call records, etc.) isStored in a stack frame
, every timefunction call
A new stack frame is generated and thenPush function stack
, when the function execution ends, the correspondingStack frame out of stack and released
stack overflow
Generally, we do not need to consider the size of the stack, but the stack is not unlimited,Too much recursion can lead to stack overflow
,Too many allocs can cause heap overflow
How to prevent stack overflow:
- Avoid recursive calls that are too deep
- Do not use too many local variables and control the size of local variables
- Avoid the allocation of objects occupying large memory and release them in time
- Where appropriate
Call the system API to modify the stack size of the thread