Label: Memory Five area virtual memory


This section only introduces five memory areas & virtual memory

  1. The stack area
  2. The heap area
  3. Global static region
  4. The constant area
  5. Code section
  6. Virtual memory & physical memory

1 Five areas of memory

From top to bottom: stack -> heap -> Global static -> Constant -> code area (kernel and reserved areas are not considered)Development:

  1. Memory five areas, the actual refers to virtual memory, rather than real physical memory.
  2. In iOS, the default allocation of virtual memory for an application is 4 GB, but only 3 GB is allocated to the five areas, and 1 GB is allocated to the kernel area outside the five areas

1.1 the stack area

  • Local variables and arrays defined inside the function are stored on the stack; (such as (id self, SEL _cmd))
  • The stack memory space is managed by the system. (Space is opened up at the end of the function call and reclaimed at the end of the function call)
  • The stack is a continuous memory area extending from the high address to the low address. It follows the principle of FILO first in, then out, and has high efficiency.
  • Stacks are typically allocated at run time

1.2 the heap area

  • The maximum space is managed manually by us. (ARC Automatic Management)
  • The heap expands from lower to higher addresses.
  • Malloc, calloc, realloc open:
  • Heap area open up space, can be a discontinuous memory area, to the list structure exists (add and delete fast, find slow). Returns the initial address stored in the stack.
  • Free recovery. Frees the memory of the object in the heap and sets the address pointer in the stack to null.

1.3 Global Static Zone (.bSS)

  • Store global and static variables
  • Space is managed by the system. (When the program starts, open up space; At the end of the program, the space is reclaimed; Exists for the duration of program execution)
  • Static modifiers are executed only once for the lifetime of the program

1.4 Constant area (.data)

  • Stores constants (integers, characters, floats, strings, etc.) that cannot be changed throughout the program’s run time.
  • Space is managed by the system and has a life cycle of the entire program running period.

1.5 Code area (.text)

Store the CPU instructions executed by the program. (Convert code to CPU instructions at compile time)

NSInteger i = 666; NSLog(@"NSInteger I -> memory address: %p", &i); NSString * name = @ypy; NSString * name = @ypy; NSLog(@"NSString name -> memory address: %p", name); NSLog(@"NSString name -> pointer address: %p", &name); NSObject * objc = [NSObject new]; NSLog(@"NSObject objc -> memory address: %p", objc); NSLog(@"NSObject objc -> pointer address: %p", &objc); // The pointer to the object is stored in the stackCopy the code
2021-08-06 14:23:42.296970+0800 001-- memory[63570:4007723] NSInteger I -> Memory address: 0x7Ffee721EBD8 2021-08-06 14:23:42.297069+0800 001-- memory[63570:4007723] NSString name -> 0x1089e1220 2021-08-06 14:23:42.297153+0800 001-- memory[63570:4007723] NSString name -> 0x7Ffee721EBD0 2021-08-06 14:23:42.297236+0800 001-- Memory [63570:4007723] NSObject OBJC -> 0x600001065000 2021-08-06 14:23:42.297328+0800 001-- Memory [63570:4007723] NSObject OBJC -> Pointer address: 0x7ffee721ebc8Copy the code

(0x7: stack area, 0x1: constant area, 0x6: heap area)

  • For the local variable I, the address starts with 0x7, so I is stored on the stack
  • For the string object name, the object address of name and the address of the pointer to the name object are printed
    • The name object address starts with 0x1, indicating that it is stored in the constant area
    • The address of the pointer to the name object starts with 0x7, indicating that it is stored in the stack
  • For the object obj created by alloc, the address of the object obj and the address of the pointer to the objc object are printed
    • Objc object addresses start with 0x6, indicating that they are stored in the heap
    • The address of the pointer to an objc object starts with 0x7, indicating that it is stored in the stack

2 Virtual memory & physical memory

In early computers, there was no concept of virtual memory, only physical memory, and every application directly wrote all information into the memory bar. When the memory module space is insufficient (occupied by other applications), a memory warning is reported. At this point we have to manually close some applications to free up some memory for the current application to run.

2.1 Physical Memory

Physical memory: The actual size of the memory module. (4G memory, physical memory is 4G)

2.1.1 Physical Memory defects

  • Out of memory: Every application opens and loads all the information, which takes up too many resources. Large software simply cannot be loaded.
  • Insecure: The memory address is fixed every time the application is loaded, making it easy to tamper with the data directly from the memory address. In the early days of local plug-ins, data was tampered with through memory addresses.

2.2 Virtual Memory

Later, after research, it was found that each application used only a small part of the memory (active part) of the application. So the memory is evenly divided into many pages. Instead of loading all apps at startup, each startup app is allocated a virtual memory size that is cut into the same size pages as physical memory.

2.3 develop

  • Memory management unit
    • Memory Management Unit (MMU) : Memory Management Unit, sometimes called paged Memory Management Unit (PMMU)
    • Computer hardware that processes memory access requests for the central processing Unit (CPU).
  • Memory page size
    • Linux and MacOS: 4K per page
    • IOS: 16K per page
  • A page table
    • Address mapping table of the virtual memory and physical memory of an application
  • Virtual space size
    • Each application (process) can be allocated 4G size by default. But it’s really just a page table that records the mapping. Page tables are stored in the operating system’s memory region.
    • The actual amount of physical memory used by the application is determined at runtime.
  • Wild pointer: Released early and cannot find content when querying
  • Memory leak: No release, memory is being used
  • Overrelease: Releases a freed object.
  • Buffer zones
    • There is a small area of unused memory between the stack area and the heap area. Use to create a buffer area between the stack area and the heap area
  • Overflow:
    • Data arriving in the buffer is copied to the small buffer without paying attention to the boundary of the small buffer. As a result, the small buffer becomes full and overwrites other data in the memory region adjacent to the small buffer.
  • Define and const differences:
    • Define: macro. No syntax recognition at compile time, no type. Memory is allocated at compile time. Macros are replaced and memory is created with each use.
    • Const: constant. Syntax recognition is performed at compile time and the type needs to be specified. Memory is not allocated at compile time, only the first time it is used, memory is opened up and the memory address is recorded. Subsequent calls will not open up memory, directly return the memory address of the record. It’s more efficient. Less memory usage.