Memory Five memory areas

(1) The heap

  • Is allocated and released by programmers to hold dynamically allocated memory segments during runtime. Variable size, can be increased and decreased

  • A data structure where the heap extends to a higher address and is a discontinuous area of memory. The programmer is responsible for when to free memory (such as free or delete), and in the iOS ARC program, the system automatically manages the counter, which is zero, and frees memory after the next runloop has finished. Everything in the heap is anonymous, so it cannot be accessed by name, but only by pointer.

For the heap, frequent new/dealloc is bound to cause discontinuity in memory space, resulting in a large amount of fragmentation, making the program inefficient.

2. Stack area (heap)

  • The stack is allocated and released by the compiler to hold variables temporarily created by the program, function parameters, local variables, and so on

  • Is a contiguous memory area, following the principle of “first in, last out”, to low-address data structures

  • When a function is called, its parameters are pushed onto the stack of the process that initiated the call, and the return value of the function is pushed back onto the stack when the call ends.

  • Because of its lifO nature, the stack is particularly handy for saving/resuming the call scene. In this sense, we can think of the stack as an area of memory where temporary data is stored and exchanged.

(BSS segment)

  • The global area is the memory space allocated at compile time. The data in this memory remains during the program running and is released by the system after the program ends. Static modified variables are always saved to the constants section

  • To store global variables that have been initialized in the executable file, that is, to store statically allocated variables and global variables (.data)

  • A BSS segment is usually an area of memory used to store uninitialized global and static variables in a program.

  • The BSS segment clears 0 before the program executes, so uninitialized global variables (static variables) are already 0. So the case is still stored in the BSS segment, which is reclaimed from the BSS segment once initialized and stored in the data segment (data segment).

4. Constant data segment

  • Memory space allocated at compile time and released by the system at the end of the program. It’s a constant, it’s a special area

  • Usually an area of memory used to store initialized global and static variables in a program. Data segment belongs to static memory allocation and can be divided into read-only data segment and read/write data segment. String constants, etc., are stored in read-only data segments that are retrieved at the end of the program.

5, Code segment/text segment

  • The binary code used to store a function, which is an in-memory mirror of the executable. Code segments need to be protected from illegal modification at run time, allowing only read operations, not write operations