IOS development basis-basic exploration of memory (heap, stack)

IOS development basis-basic exploration of memory (heap, stack)


  • Static storage area (global storage area)
  • Heap area
  • Stack area
  • Code area
  • Literal constant area

Memory partition map:

The code area is stored at the low address, and the stack area is stored at the high address. The districts are not continuous.

After the app is started, the code area, text constant area, and global storage area are fixed in size, and pointers to these areas will not cause crashing errors. The heap area and stack area change all the time (heap creation and destruction, stack popping and popping). When using a pointer to the memory of these two areas, pay attention to whether the memory has been released, pointing to the released memory Will produce wild pointers.

Static storage area (global storage area)

The memory is allocated when the program is compiled, and this memory exists during the entire running period of the program. It mainly stores static data, global data and constants.

Global variables and static variables will be stored in this area. In fact, global variables are also static, so they are also called global static storage areas.

Storage method:

  • The initialized global variables and static variables are placed in an area (data area)
  • Uninitialized global variables and static variables are placed in another adjacent area (BSS area)

Released by the system after the program ends.

Literal constant area

The constants used in the program are stored in this area (the constant string is placed here). After the program ends, it is released by the system. The constants used in the program will be obtained in the text constant area.

Code area

Store the binary code of the function body. Running the program is to execute the code, and the code must be loaded into the memory to be executed.

Heap area

Brief description

Also known as dynamic memory allocation. When the program is running, use malloc or new to apply for any size of memory, and the programmer is responsible for releasing the memory with free or delete when appropriate. The lifetime of dynamic memory can be determined by us. If we do not release the memory, the program will release the dynamic memory at the end (if a certain dynamic memory is no longer used, it needs to be released, otherwise it is considered that a memory leak has occurred).

Space allocation

Dynamic allocation and recovery cannot be statically allocated (the address of the heap area is allocated from low to high).

Assignment steps:

  • The operating system has a linked list of free memory addresses
  • When the system receives the application for the program, it will traverse the linked list to find the first heap node whose space is larger than the requested space, then delete the node from the list of free nodes, and allocate the space of the node to program
  • Since the size of the found heap node may not be exactly equal to the size of the application, the system will automatically put the excess part back into the free list

Size limit:

The heap is a data structure that expands to high addresses. The memory area is not continuous because the system uses a linked list to store free memory addresses, and the traversal direction of the linked list is from low addresses to high addresses. The size of the heap is limited by the effective virtual memory in the system. It can be seen that the space obtained by the heap is more flexible and larger.


In the heap, we do not extract elements according to the order of entering the heap, but extract elements according to the priority of the elements. Generally speaking, the heap is a data structure, and its insertion and deletion operations should be based on priority. It is decided that the heap also has the following characteristics:

  1. The priority of any node is not less than its child nodes
  2. The value of any node is less than or equal to its child nodes
  3. The main operation is to insert and delete its smallest element (the element value itself is the priority key value, and the small element enjoys high priority) (every time the element with the highest priority is taken out, the top participant of the heap is allowed to exit the heap)

It can be seen from the above that the advantages of the heap are flexibility and convenience, and a wide range of data adaptability, but the efficiency is somewhat reduced. [In any order]

Stack area

Brief description

When the function is executed, the storage units of the local variables in the function can be created on the stack, and these storage units are automatically released when the function is executed. The stack memory allocation operation is built into the processor's instruction set and is very efficient, but the allocated memory capacity is limited. It stores non-static local variables, such as function parameters, pointers to objects that live in the function, and so on. When the size of the stack area of the system is not enough to allocate, the system will prompt a stack overflow.

Space allocation

Allocation method: (the stack area address is allocated from high to low), static allocation (for example: allocation of local variables) and dynamic allocation (for example, allocation by the alloc function) are all done by the system, without manual management

Assignment steps:

  • When storing each function, it will ask the operating system for resources when it is executed, and the stack area is the memory when the function is running.
  • The variables in the stack area are allocated and released by the compiler. The memory is allocated with the running of the function and released with the end of the function, which is done automatically by the system.

Size limit:

The stack is a data structure extended to lower addresses, and is a contiguous area of memory. The address at the top of the stack and the maximum capacity of the stack are pre-defined by the system. If the requested space exceeds the remaining space of the stack, a memory overflow will be prompted. Therefore, the space that can be obtained from the stack is small.


It is only allowed to add data (push) or output data (pop) in a section of the link string or array (usually the top of the stack). Since the stack is only allowed to operate on one end, follow the principle of first in, last out. The advantage is fast and efficient, the disadvantages are sometimes limited, and the data is not flexible. [First in, Last out]

3.operations of the stack:

  1. Pop: Take out the top element in the stack
  2. top: View the top element of the stack
  3. push: Put a new element at the top of the stack.