The overview

In iOS, memory is mainly divided into five areas: stack area, heap area, global/static area, constant area and code area. The layout structure is shown as follows:

A, stack area

  • The stack isA contiguous area of memoryfromFrom a high address to a low addressFor storage, follow the first in last out (FILO) principle.
  • The stack address space in iOS is0X7At the beginning.
  • Stack areas are usually allocated at run time, and memory space is allocated bySystem managementMemory is automatically freed when the declared variable is out of scope.
  • Function defined internallyA local variable,Method parametersDefault parameter in method:self,cmd), are stored in the stack area.

The advantages and disadvantages

  • Advantages: The stack is automatically allocated and released by the system, no memory fragmentation, so fast and efficient.
  • Disadvantages: stack memory size is limited, data inflexible, iOSThe main thread stackThe size is1MB.Other threadsis512KBMAC only,8MThe information comes fromThe official documentation:

validation

- (void)testStack { int a = 10; NSLog(@"a == %p size == %lu",&a,sizeof(a)); NSLog(@" method argument self: %p",&self); NSLog(@" method argument CMD: %p",&_cmd); } A == 0x7FFeee47d75c size == 4 Method parameter self: 0x7FFeee47d768 Method parameter CMD: 0x7FFeee47d760Copy the code

Second, the heap area

  • The heap isA discontinuous memory regionfromFrom a low address to a high addressTo store, similar toChain table structure(easy to add and delete, not easy to query), follow the first in first out (FIFO) principle.
  • The address space of the heap in iOS is0x6At first, the allocation of space is always dynamic.
  • Developers need to pay attention to the life cycle of variables, if not released in time, will cause memory leaks, only when the program ends by the system to recycle.
  • OCThe use ofallocornewOpen up space to create objects.
  • CLanguage usemalloc,calloc,reallocAllocated space, neededfreeRelease.

The advantages and disadvantages

  • Advantages: Flexible access to space, large memory allocation.
  • Disadvantages: Manual management is required, which is slow and prone to memory fragmentation.

validation

- (void)testHeap { NSObject *object1 = [NSObject new]; NSObject *object2 = [NSObject new]; NSLog(@"object1 = %@",object1); NSLog(@"object2 = %@",object2); } Object1 = <NSObject: 0x600002B8F430 > object2 = <NSObject: 0x600002B8F420 >Copy the code

Global/static zone

  • This area is the memory space allocated at compile timeiOSIn general with0x1At the beginning, during the running of the program, the data in this memory always exists, after the program is released by the system.
  • Uninitialized global and static variables, i.eBSS area(BSS).
  • Initialized global and static variables, i.eData area(data).

validation

int clB = 10; static int bssB = 10; - (void)test { NSLog(@"clA == %p",&clA); NSLog(@"clB == %p",&clB); } clA == 0x104131424 clB == 0x104131408Copy the code

4. Constant area (i.e. Rodata)

  • This 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.
  • Store constants:The integer,character,Floating point,stringAnd so on.

validation

Use MachOView to view constants:

Five, code area

  • This 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.
  • The code when the program is runningIt will be compiled into binary and stored in the code area of memory.

validation

View the code area with MachOView: