The memory layout of iOS applications

  • Code snippet: compiled code
  • Data segment
    • String constants: for example, NSString * STR = @”123″;
    • Initialized data: initialized global variables, static variables, etc
    • Uninitialized data: uninitialized global variables, static variables, etc
  • Heap: Dynamically allocated space via alloc, malloc, calloc, etc. The allocated memory space addresses are getting larger and larger
  • Stack: The overhead of calling a function, such as local variables in a function, allocates smaller and smaller memory space addresses

Tagged Pointer

  • Starting from 64-bit, iOS introduces Tagged Pointer technology to optimize storage of small objects such as NSNumber, NSDate, and NSString

  • Before Tagged Pointer is used, objects such as NSNumber need to dynamically allocate memory and maintain reference counting. Object Pointers store the address values of objects in the heap

  • After using Tagged Pointer, the Data stored in the NSNumber Pointer becomes Tag+Data. That is, the Data is stored directly in the Pointer. The Tag is used to indicate whether the type is NSString, NSNumber, or something else. When Pointers are insufficient to store data, dynamically allocated memory is used to store data.

NSNumber *number = [NSNumber numberWithInt:10];
Copy the code

  • Objc_msgSend can recognize Tagged Pointer. For example, the intValue method of NSNumber directly extracts data from the Pointer, saving call overhead.

  • On iOS, the highest bit (64th from the right) of the Pointer is 1. This Pointer is called a Tagger Pointer.

  • On Mac, a Pointer with least significant bit 1 is a Tagger Pointer.

copy

  • The purpose of copying: to create a copy object, the same as the source objectEach other. The source object is modified without affecting the replica object. The replica object is modified without affecting the source object.
  • IOS provides two copying methods
    • Copy: An immutable copy is generated
    • MutableCopy: generates mutable copies
  • Deep copy, shallow copy
    • Shallow copy: does not copy the object itself, does not generate new objects, only copies the pointer to the object, the new object pointer still points to the old object
    • Deep copy: Copies the object itself and creates a new object in memory. The new object pointer points to the newly generated object

To be copied, a custom object must comply with the NSCopying protocol

@interface Person : NSObject<NSCopying>
@property(assign,nonatomic)int age;
@end

@implementation Person
- (id)copyWithZone:(nullable NSZone *)zone{
    Person *p = [Person allocWithZone:zone];
    p.age = self.age;
    return p;
}
@end
Copy the code

Principle of weak pointer

  • __strong: strong Pointers are created by default
  • __weak: A weak pointer that automatically becomes nil after the object to which it points is destroyed.
  • __unsafe_unretained: An unsafe weak pointer. After the object to which the pointer points is destroyed, the pointer still points to the address of the original object. A wild pointer error occurs when using this pointer.
struct SideTable { spinlock_t slock; RefcountMap refcnts; weak_table_t weak_table; // weak reference hash table}Copy the code

When an object is destroyed, all weak Pointers to the object in the weakly referenced hash table are found by the object’s address and assigned to nil.