IOS memory management mechanism and cyclic reference processing method, ios Memory Management
Brief Introduction
ARC: Automatic Reference count, Automatic Reference Counting
MRC: Mannul Reference Counting
How ARC works
1. Each time a new instance is created, ARC allocates a memory to store instance information. The memory contains the instance type information and the values of all related attributes of the instance.
2. If the instance is no longer in use, ARC automatically releases the memory occupied by the instance and stores other data in the released memory. This ensures that the used instance does not occupy the memory space all the time.
3. ARC recovers and releases an instance in use. The properties and methods of the Instance cannot be accessed or called again. Once the instance is called, the application will crash.
4. To ensure that the instances in use will not be destroyed, ARC will track and calculate the attributes, constants and variables referenced by each instance. Once referenced, it will create a strong reference for the instance. Strong reference will hold the instance firmly, as long as the strong reference is still in, the instance will not be destroyed.
Circular strong reference
Cyclic strong reference refers to the creation of strong references between two objects, so that the memory of the two instances will never be released.
This will cause memory leakage. You can solve this problem through Weak Reference or Unowned Reference.
Common Coding in code
OC
// Define a macro # define WS (weakSelf) _ weak _ typeof (& * self) weakSelf = self; // WS (weakSelf) when calling; [XPApiManager getVideoListWithType: self. listType pageIndex: self. pageIndex pageSize: self. pageSize progress: ^ (NSProgress * progress) {[weakSelf. tableView reloadData];} finished: ^ (NSString * msg, id responseObject ){}
@property (weak, nonatomic) id<XPDetailMenuPopViewDelegate> delegate;@property (weak, nonatomic) IBOutlet UIImageView *imgFavorite;
Swift
Lazy var strHTML: ()-> String = {[unowned self] in // code writing area
Self. XXX}
XPHomeViewModel.loadCategoryGroup { [weak self] (outGroups) in self!.outGroups = outGroups }
weak var tmpSelf = selfCouponData.loadCouponData { (data, error) -> Void in tmpSelf!.couponTableView?.reloadData()}
weak var delegate: YMCategoryBottomViewDelegate?@IBOutlet weak var tableView: UITableView!