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:

  1. IOS underlying principles of alloc exploration
  2. The underlying principles of iOS are explored
  3. The underlying principles of iOS explore the nature of objects & isa’s underlying implementation
  4. Isa-basic Principles of iOS (Part 1)
  5. Isa-basic Principles of iOS (Middle)
  6. Isa-class Basic Principles of iOS Exploration (2)
  7. IOS fundamentals explore the nature of Runtime Runtime & methods
  8. Objc_msgSend: Exploring the underlying principles of iOS
  9. Slow lookups in iOS Runtime
  10. A dynamic approach to iOS fundamentals
  11. The underlying principles of iOS explore the message forwarding process
  12. Dyld (part 1)
  13. IOS Basic Principles of application loading principle dyld (ii)
  14. IOS basic principles explore the loading of classes
  15. The underlying principles of iOS explore the loading of categories
  16. IOS underlying principles to explore the associated object
  17. IOS underlying principle of the wizard KVC exploration
  18. Exploring the underlying principles of iOS: KVO Principles | More challenges in August
  19. Exploring the underlying principles of iOS: Rewritten KVO | More challenges in August
  20. The underlying principles of iOS: Multi-threading | More challenges in August
  21. GCD functions and queues in iOS
  22. GCD principles of iOS (Part 1)
  23. IOS Low-level – What do you know about deadlocks?
  24. IOS Low-level – Singleton destruction is possible?
  25. IOS Low-level – Dispatch Source
  26. IOS bottom – a fence letter blocks the number
  27. IOS low-level – Be there or be Square semaphore
  28. IOS underlying GCD – In and out into a scheduling group
  29. Basic principles of iOS – Basic use of locks
  30. IOS underlying – @synchronized Flow analysis
  31. IOS low-level – The principle of lock exploration
  32. IOS Low-level – allows you to implement a read/write lock
  33. Implementation of Objective-C Block
  34. Implementation of Objective-C Block
  35. IOS bottom – Block, comprehensive resolution!
  36. IOS Basics – Startup Optimization (part 1)
  37. 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 isSystem data structureAnd its correspondingA process or thread is uniquethe
  • The stack isScale to a lower addressData structure of
  • Is a piece of the stackContiguous memory areas, follow theFirst in last Out (FILO)The principle of
  • Of the stackAddress spaceIn iOS it is0 x7 beginning
  • The stack area is generally inRuntime 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 ofThe compiler allocates and releases automaticallyNo memory fragmentation, soFast and efficient
  • Disadvantages: stackMemory size is limited and data is inflexible

Heap area

define

  • The heap isExtend to higher addressesData structure of
  • The heap isA discontinuous memory region, similar to theChain table structure(easy to add and delete, not easy to query), followFirst in first out(FIFO) principle
  • The heapAddress spaceIn iOS it is0x6At first, the allocation of space is always dynamic
  • The allocation of heap areas is generally inRuntime 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

  • OCThe use ofallocOr usenewOpen up space to create objects
  • CLanguage useMalloc, Calloc, ReallocAllocated space, neededfreeThe release of

The advantages and disadvantages

  • Advantages: flexible and convenient, wide range of data adaptation
  • Weakness: the need toManual management.Slow speedAnd 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

  • uninitializedtheThe global variableandA static variable, that is, BSS region (.bSS)
  • initializedtheThe global variableandA static variableData 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 toString 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.