IOS memory overview


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

IOS memory overview

Memory partition

Stack area

  • High address to low address extensionSystem data structure, correspondingProcess or threadIs unique
  • It’s a pieceContiguous 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 isCompiler automatically allocates and releasesNo memory fragmentation,Fast and efficient
  • Disadvantages: stackLimited memory size and inflexible data
    • The size of IOS main thread is 1MB, and that of other threads is 512KB
    • 8MB on MAC


  • The heap is made ofLow address to high address extensionData 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 needRead 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: needManual management, slow speed, easy to produceMemory fragmentation

Global area \ static area (. BSS &. Data)

  • Compile time allocationThe memory space of the program. During the program running, the data always exists. After the program endsSystem release
  • In IOS, generallyStart with 0x1
  • Read / write area

Mainly used to store:

  • uninitializedofglobal variableandStatic variable, i.e. BSS area
  • Initializedofglobal variableandStatic variable, i.e. data area

    • global variableIt means that the variable value can beThe runtime is dynamically modified, andStatic variableyesStatic modificationVariable containingStatic local variableandstatic global

Constant area (. Rodata)

  • Compile time allocationMemory space,The system will release automatically after the program ends
  • Read only area
  • Main storage: used and not pointedstring constant
  • String constant because it may be in the programMultiple 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 threadshareChange 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 callA 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 overflowToo 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 appropriateCall the system API to modify the stack size of the thread

Recommended Today

The whole tutorial of docker installation and use, installation / complete command / dockerfile image production / docker container arrangement and one click installation of nginx + redis + MySQL / visualizer portal (version 2022)

官网: 官网: docker 镜像市场: 一、docker 说明 1.1、docker 核心 1、Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源,Docker 是一个 CS 架构软件。 2、Docker 是一个虚拟化轻量级linux服务器,可以解决我们在开发环境中运行配置问题 3.、Docker的主要目标是‘build ,ship and run any app,anywhere’,一次封装,到处运行 4、容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。 1.2、docker 版本问题 .Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。 1.3、docker 架构( 3大核心) · 1、Images 镜像 (等于软件) · 2、Registry […]