Summary of ios low-level articles
Storage space is divided into RAM and ROM.
Random Access Memory (RAM) : A running memory that can be directly accessed by a CPU. The READ/write speed is fast, but the memory cannot be stored during power-off. It is divided into:
- dynamic
DRAM
, the speed is slower, the need for regular refresh (charging), often said memory bar refers to it, the price will be a little lower, the mobile phone running memory is also refers to it. - static
SRAM
It’s faster. What we call level 1 cache, level 2 cache is that. It’s a little bit more expensive.
ROM(Read only Memory) : memory that can be stored by power failure, such as SD card and Flash(mechanical disk can also be simply understood as ROM). Lots of use: NandFlash(big space, cheap), and NorFlash(running applications directly, fast reads).
As THE RAM type does not have the power failure storage capacity (that is, once the power supply is stopped, all the data will be garbled after being powered on again, so it needs to be initialized), app programs are generally stored in ROM. RAM access is much faster and more expensive than ROM.
Since RAM cannot be powered down for storage, our APP programs, flash packages, downloaded files and so on are stored in ROM.
The ROM used in the phone is basically NandFlash, which is not directly accessible by the CPU, but requires the file system/driver (embedded EMC) to read it into the RAM CPU to access it.
Memory (RAM) partition area
Stack area
define
- The stack is
Scale to a lower address
, - Is a piece of the stack
Contiguous memory areas
, follow theFirst in last Out (FILO)
The principle of - Of the stack
Address space
In iOS it is0 x7 beginning
- The stack area is generally in
Runtime allocation
storage
The stack area is automatically allocated and freed by the compiler for storage
A local variable
Parameters of a function
, eg: Function’s hidden argument (id self, SEL _cmd)
The characteristics of
-
Advantages: Because the stack is automatically allocated and freed by the compiler, there is no memory fragmentation, so it is fast and efficient
-
Disadvantages: limited stack memory size, inflexible data
The iOS main thread stack size is 1MB
- The other threads are
512KB
MAC
only8M
The above instructions for memory size are provided in the Threading Programming Guide
Heap area
define
- The heap is
Extend to higher addresses
- The heap is
A discontinuous memory region
, similar to theChain table structure
(easy to add and delete, not easy to query), followFirst in first out
(FIFO) principle - The heap
Address space
In iOS it is0x6
At first, the allocation of space is always dynamic - The allocation of heap areas is generally in
Runtime allocation
storage
The heap area is allocated and released dynamically by the programmer. If the programmer does not release it, it may be reclaimed by the operating system after the program is finished, mainly for storage
OC
The use ofalloc
Or usenew
Open up space to create objectsC
Language useMalloc, Calloc, Realloc
Allocated space, neededfree
The release of
The characteristics of
- Advantages: flexible and convenient, wide range of data adaptation
- Weakness: the need to
Manual management
.Slow speed
And easy toGenerate memory fragmentation
When accessing memory in the heap, it is generally necessary to read the pointer address of the stack through the object, and then access the heap through the pointer address
Global zone (static zone, i.e..bss &.data)
The global area is the memory space allocated at compile time, which generally starts with 0x1 in iOS. During the program running, the data in this memory is always stored and released by the system after the program ends
uninitialized
theThe global variable
andA static variable
, that is, BSS region (.bSS)initialized
theThe global variable
andA static variable
Data area (.data)
A global variable is a variable whose value can be dynamically modified at run time, while a static variable is a static modified variable, including a static local variable and a static global variable
Constant area (.rodata)
The constant area is the memory space allocated at compile time and released by the system at the end of the program
- Already used and not directed to
String constant
Because string constants can be used multiple times in a program, memory is allocated ahead of time before the program runs
Code area (.text)
Code areas are allocated at compile time to store code that is compiled into binary and stored in memory while the program is running
The address starts with 0x1, which means it’s in the constant area and the address starts with 0x6, which means it’s in the heap area and the address starts with 0x7, which means it’s in the stack
The function of stack
The function of stack
Also known as theThe stack area
To allocate memory from the highest address to the lowest address, as opposed to the heap. See the illustration at the beginning of this articleThe stack frame
Refers to theA separate contiguous area of memory occupied by a function (running and incomplete)
- Newly created in the application
Each thread has its own stack space
The stack can be used freely during the thread. And there are tens of thousands of function calls in the thread, these functionsShared
This of the processStack space
. The stack space used by each function is oneThe stack frame
All stack frames make up the thread’s complete stack - The function call takes place in
The stack
Every one of themInformation about the function
(for example, local variables, call records, etc.)Store in a stack frame
Run the command onceA function call
, generates a stack frame associated with it, and then converts itThe stack frame is pushed onto the function stack
And when the delta functionPerform the end
, then the corresponding of this functionThe stack frame goes off the stack and is released
As shown in the figure below, it is the stack frame layout of classic Graph-ARM
ARM stack frame layout
- Among them
main stack frame
forCall the stack frame of the function
func1 stack frame
forThe stack frame of the current function (called)
The bottom of the stack
inhigh
Address, stack goes down.FP
isThe stack base
It points to the delta functionStart address of stack frame
SP
It’s a functionThe stack pointer
, it points toTo the top of the stack
The location of the.ARM pressure stack
theThe order
Very regular (and relatively easy to hack), in orderCurrent function pointer PC
,Return pointer LR
,The stack pointer SP
,The stack base FP
,Number of arguments and Pointers passed in
,The local variable
andTemporary variable
. If a function is about to call another function, the temporary variable area should hold the other function’s arguments before jumping.- The ARM can also
Use the stack base address and the stack pointer to indicate the position of the stack frame
, the stack pointer SP keeps moving, ARM’sThe characteristics of
Is that,The address in both stack Spaces (SP+FP) must be preceded by two code addresses (PC+LR) that explicitly identify an address within the location of the calling function
.
Stack overflow
In general, applications do not need to consider the size of the heap and stack, but the reality is that the heap and stack are not unlimited. Too much recursion can cause stack overflows, and too many alloc variables can cause heap overflows.
So ways to prevent stack overflow: (1) avoid deep recursive calls;
(2) Do not use too many local variables to control the size of local variables;
(3) Avoid allocating objects that occupy too much space and release them in time;
(4) If it is not possible, call system API to modify the stack size of thread in appropriate situation;