Swift Memory Management

Source: Internet
Author: User

1, Object-c Experience Two stages: 1, manual reference count memory management (Manual Reference COUNTING,MRC) 2, automatic reference count memory management (Automatic refernce Counting,arc)

2. The reference type memory is allocated to the heap and requires human management. The value type memory is allocated on the stack and has processor management.

3. Each object created by the class has an internal counter that tracks the number of references to the object and becomes the reference count (Reference count, short RC). When the object is created, the reference count is 1, each time the object is referred to is its reference count plus 1, if not required, the object reference is broken (assignment nil), where the reference count minus 1. Object memory is freed when the object reference count is 0.

Note: The constructor of the Init () {} class will establish a "strong reference" relationship between the instance and the object, and the deinit{} destructor will only execute if the reference count is 0.

4, strong reference loop:

When the storage properties of two objects refer to each other, an object is released on the premise that the other object is released first, and the other objects are released before the object is released, which results in a "deadlock" state that causes a memory leak.

Resolve the strong loop two ways: 1, weak reference 2, no main reference.

Weak reference: Allows one of the objects to refer to another object without a strong reference type, so that no strong reference loops are caused. A weak reference is suitable for cases where a reference object can have no value, because a weak reference can have no value, we must say that a weak reference is declared as an optional type, and a weak reference is declared with weak.

For example: Employee Class A Storage attribute department, Department B department leader (employee), if the department can not lead the department, then the Class B department leader attribute can use weak adornment, there is no circular reference between A and B.

No primary reference: one of the objects does not take a strong reference to another object. A no-primary reference applies to a reference object that always has a value, and it is often defined as a non-optional type, using the keyword unowned.

For example: An employee can have no department, a department must have a department leader, then the Class B unowned var manager:employee can be established without a master reference.

5. Lazy decoration indicates lazy loading. Lazy var fullName: ()->string={return a+b}, the properties of the class can be used in closures, must use lazy deferred loading, and after all properties have been initialized, self means that the object will be created and used.

6. Closure reference loops: If closures and capture objects always reference each other and are always destroyed at the same time, the capture inside the closure is declared as a no-primary reference.

When the captured object is likely to be nil, the capture within the closure is declared as a weak reference. If the captured object is definitely not nil, then a jobless reference is used.

Swift Memory Management

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.