Object memory management mode

MRC: Manual reference counting, manual retain and release reference counting.

2.ARC: Automatic reference counting, LLVM compiler helps us manage reference counting. (Retain, copy, release, autorelease where appropriate)

The knowledge points need to know about MRC

1. The reference counting mechanism manages memory by incrementing and decrement counters. After an object is created, its retention count is at least 1. If the retention count is positive, the object continues to live. When the retention count reaches zero, the object is destroyed.

2. During the object’s life cycle, other objects retain or release the object by reference. The retention and release operations increase and decrease the retention count, respectively.

We use retain to increase the count and release to decrease the count, but in the return function, we don’t want to destroy the currently created object. Instead, we want to return it. In this case, we can use [object autoRelease] to extend the life of object. Object is automatically pooled in the function that returns the address. The retainCount of the object does not decrease by 1 after calling autoRelease, so the retainCount description of the reference count is inaccurate.

{

​     _object;

}

(void)setObject:(id)object{

​    [_object release];

​    [object retain];

}
Copy the code

What to know about ARC

1.ARC manages objects in such a way that the compiler writes hold and release operations where appropriate, and the memory-management semantics of variables can be specified by modifiers (@property’s default modifiers under ARC: Atomic, Readwrite, assign(for non-objects) or strong (for objects), atomic, Readwrite, assign(for MRC)

2. The memory management semantics of objects returned by methods are always represented by method names (copy, retain, Release, autorelease)

3.ARC only manages the memory of Objective-C objects (CoreFoundation objects require manual management by programmers, such as CFRelease and CFRetain).

4. All we need to do in dealloc is release references to other objects, unlisten KVO, remove notifications, and do nothing else because this object is about to be reclaimed

5.OC generally does not consider exception code handling, because the application usually crashes when exceptions occur, so there is no need to handle object recycling when exceptions occur (but OC itself can do this by turning on the compiler’s -fobjC-arc-exceptions flag)

If the strong pointer is reclaimed, the weak pointer will be reclaimed. If the strong pointer is reclaimed, the weak pointer will be reclaimed. If the strong pointer is reclaimed, the weak pointer will be reclaimed. Block’s WeakSelf and StrongSelf are examples of this.)

7. Autoreleasepool can reduce peak memory and release objects early

8. The object’s memory is being recycled, the address of the object is marked as available, but the object remains in memory, the pointer to the object is called wild pointer, call wild pointer object may get the original value, also may address object is written the new value (because of change of address marked as available, so that may have been other newly created object takes up), For a wild pointer, all we need to do is set its value to nil, which is called a null pointer. We can find calls to wild Pointers, or Zombie Objects, by turning on the Enable Zombie Objects option in the Xcode compiler. When we DEBUG the program, when we encounter calls to Zombie Objects, The application prints an error message (the runtime dynamically adds a new Zombie class with the name of the original class, but does not implement the original class method). The new Zombie class receives the message and executes the message forwarding mechanism of runtime.

9.1 Strong: __strong Retain this value 9.2 weak: __weak Retain this value 9.3 Retain: __strong Retain this value 9.4 assign: __unsafe__unretain can modify C types, with no reserved values for base data types and objects. __strong Keeps the value 9.6 AUTORELEASE: __autoreleasing the reference is passed, which is automatically released when the method returns, extending the life of the object

Weak and strong replace assign and retain. Currently, the variable assigned is used to adapt to a later version of iOS. It is recommended not to use weak for worthy objects.