IOS memory management mechanism and cyclic reference processing method, ios Memory Management

Source: Internet
Author: User

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!

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.