Learn swift--automatic reference counting against Java (Automatic Reference counting)

Source: Internet
Author: User
Tags closure

The automatic reference count (Automatic Reference counting) is referred to as arc, which is Swift's tracking and management of application memory. Similar to the GC in Java, but not quite the same.
Arc is the instance of the reference number 0,arc will destroy this instance.
GC is when the program cannot access this instance, called the instance "unreachable", the GC will be responsible for recycling these "unreachable" instances, the recovery algorithm is very complex, Java specification for many of the GC behavior is not strict rules, different vendors in their respective implementations of the JVM have different implementations.
ARC requires the developer to ensure that the instance is no longer referenced by any attributes, constants, and variables, and that the arc will automatically release its occupied memory.
In general, there is no problem, but a strong circular reference requires us to break this strong association to avoid memory leaks, which are divided into four scenarios, which I do under a brief description:

    1. There are apartment apartments and person inhabitants two classes, apartment has an optional variable of type person, the person has an optional variable of type apartment, the two variables are strongly referenced to each other, causing a memory leak, The person variable of the apartment instance can be nil, because there can be no one living in the apartment, this time we can set the apartment's man variable to a weak reference, preceded by the weak keyword, which breaks the strong circular reference, ARC can release the memory of these two instances.
    2. Customer and CreditCard, simulating the credit cards of bank customers and customers. Of these two classes, each one takes an instance of the other class as its own property. This relationship can cause a cyclic strong reference. A customer may have or do not have a credit card, but a credit card is always associated with a customer, this time we can set the customer attribute of the credit card to no main reference, preceded by the unowned keyword, also broke the strong circular reference, can be released by ARC memory.
    3. Country and city, simulating countries and cities, in this model, each country must have the capital, each city must belong to a country. This allows the two properties to be accessed directly after initialization (no optional expansion is required), while avoiding circular references. To meet this requirement, the Capitalcity property of country is declared as an implicit parsing of an optional type's property by adding an exclamation point (city!) at the end of the type. This means that, like other optional types, the default value of the Capitalcity property is nil, but you do not need to expand its value to access it. The country property of city is set to no master reference, and the strong circular reference is also broken.
    4. A cyclic strong reference that is caused by a closure that defines a capture list as part of a closure when a closure is defined, in such a way that a cyclic strong reference between a closure and a class instance can be resolved.
      For more information, please refer
      Http://wiki.jikexueyuan.com/project/swift/chapter2/16_Automatic_Reference_Counting.html
      I also refer to the above summary, dredge their knowledge flow, easy to understand.

Learn swift--automatic reference counting against Java (Automatic Reference counting)

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.