IOS Development Series-ARC introduction, ios series-arc

Source: Internet
Author: User

IOS Development Series-ARC introduction, ios series-arc
1. What is ARC?

The so-called ARC is the Automatic Reference Counting, that is, the Automatic Reference count. ARC is introduced from iOS5. The introduction of the ARC mechanism aims to simplify the memory management in the development process. The ARC mechanism is more automated than the previous MRC (Manual Reference Counting. In the process of using ARC for development, developers only need to consider the use of strong/weak, and do not need to consider when objects need retain, release/autoreal.pdf. Using ARC generally does not reduce program efficiency.

Ii. Working Principle of ARC

An important principle of ARC is:As long as an object isstrongIf the Pointer Points to it, it will not be destroyed. If the object is not directed by any strong pointer, it will be destroyed..

ARC is based on reference counting. when an object is pointed by a strong pointer, its count is + 1. If no strong Pointer Points to, its count is 0, and the object will be destroyed. As long as an object has at least one strong pointer pointing to it, it will not be destroyed. However, ARC is prone to a Strong Reference Cycle problem. Even though the AddBook and Entry objects are no longer used, due to the ARC mechanism, both objects have a strong pointer pointing to each other, therefore, neither of these two objects will be recycled, resulting in memory being released.

There is a solution for the above situation: Introduce weak in one of the objects and replace its strong

After weak is introduced, after the entry is used up, because there is no strong pointer to the AddrBook, The AddrBook will be released first. Then, because the AddrBook is released, the Strong pointer to the Entry will also be destroyed, at this time, there is no strong pointer to the Entry, so the Entry will be released. In this way, the memory cannot be released.

Here is a problem. When should we use strong and weak? See the following analysis:

ViewController directly holds the View, so ViewController should have a strong pointing to the view. Similarly, view directly holds subviews, so there should be strong pointing to subviews. Because viewcontroller needs to use a subviews object but does not want to directly hold subviews, it has to point to subviews through weak. In this way, you can use the subviews object without changing the view holding relationship in viewcontroller. A general rule can be obtained:For directly held relationships, the owner must point to the owner through strong. If there is an indirect hold relationship, the indirect holder must point to the indirect owner through weak.

 

Reference: http://www.raywenderlich.com/5773/beginning-arc-in-ios-5-tutorial-part-2

        http://onevcat.com/2012/06/arc-hand-by-hand/


The difference between arc and non-Arc Code for IOS development

You have deleted all the Code related to contentView.
If you write release in ARC, the system will report an error. If you are not in ARC mode, right?
 
Release can be used in arc development for ios

After using the arc mode, you don't need to use release, because after you select the arc mode, release and autorelease will automatically become unavailable.
 

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.