Write in front: iOS underlying principle exploration is my usual development and learning in the accumulation of a section of advanced road. Record my continuous exploration of the journey, I hope to be helpful to all readers.Copy the code
The directory is as follows:
- IOS underlying principles of alloc exploration
- The underlying principles of iOS are explored
- The underlying principles of iOS explore the nature of objects & isa’s underlying implementation
- Isa-basic Principles of iOS (Part 1)
- Isa-basic Principles of iOS (Middle)
- Isa-class Basic Principles of iOS Exploration (2)
- IOS fundamentals explore the nature of Runtime Runtime & methods
- Objc_msgSend: Exploring the underlying principles of iOS
- Slow lookups in iOS Runtime
- A dynamic approach to iOS fundamentals
- The underlying principles of iOS explore the message forwarding process
- Dyld (part 1)
- IOS Basic Principles of application loading principle dyld (ii)
- IOS basic principles explore the loading of classes
- The underlying principles of iOS explore the loading of categories
- IOS underlying principles to explore the associated object
- IOS underlying principle of the wizard KVC exploration
- Exploring the underlying principles of iOS: KVO Principles | More challenges in August
- Exploring the underlying principles of iOS: Rewritten KVO | More challenges in August
- The underlying principles of iOS: Multi-threading | More challenges in August
- GCD functions and queues in iOS
- GCD principles of iOS (Part 1)
- IOS Low-level – What do you know about deadlocks?
- IOS Low-level – Singleton destruction is possible?
- IOS Low-level – Dispatch Source
- IOS bottom – a fence letter blocks the number
- IOS low-level – Be there or be Square semaphore
- IOS underlying GCD – In and out into a scheduling group
- Basic principles of iOS – Basic use of locks
- IOS underlying – @synchronized Flow analysis
- IOS low-level – The principle of lock exploration
- IOS Low-level – allows you to implement a read/write lock
- Implementation of Objective-C Block
- Implementation of Objective-C Block
- IOS bottom – Block, comprehensive resolution!
- IOS Basics – Startup Optimization (part 1)
- IOS Basics – Startup Optimization (2)
Summary of the above column
- Summary of iOS underlying principles of exploration
Sort out the details
- Summary of iOS development details
preface
How the system memory is divided in iOS development, and how the system optimizes object types. This article begins with an exploration of iOS memory management optimization.
Memory layout
First, take a look at the five regions of memory:
Memory is divided into five areas: stack area, heap area, global area, constant area and code area. The detailed information is shown as follows:
Stack area
define
- The stack is
System data structure
And its correspondingA process or thread is unique
the - The stack is
Scale to a lower address
Data structure of - 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
, such as the function’s hidden argument (id self, SEL _cmd)
The advantages and disadvantages
- Advantages: because the stack is made up of
The compiler allocates and releases automatically
No memory fragmentation, soFast and efficient
- Disadvantages: stack
Memory size is limited and data is inflexible
Heap area
define
- The heap is
Extend to higher addresses
Data structure of - 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 advantages and disadvantages
- 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 extents (.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
The constant area
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 more than once in a program, memory is allocated 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
supplement
- In the figure above, the address ranges from 0x00400000 to 0xC0000000.
- 0x00400000 The value in the range is reserved by the system.
- The kernel area of the system other than 0xC0000000 (occupying 1G);
- The stack area memory is obtained by SP register.