Objective-C basics 3: Memory Management continued,

Source: Internet
Author: User

Objective-C basics 3: Memory Management continued,

1. In the previous article, we talked about the basis of Memory Management in OC. We will summarize the time to use memory management.
1) when creating an object with new, alloc, or copy, you must release the object.

2) when an object is owned, if it is a temporary object, it does not need to be released; if it needs to be retained for a long time, it must be retained and released in the dealloc method.

2. Garbage collection mechanism in OC

OC2.0 introduces the garbage collection mechanism, that is, after the garbage collection mechanism is enabled, we forget to release the object. OC will help us release the object, this shows that OC is more powerful than C ++.

The main principle of the garbage collection mechanism is to regularly and dynamically track objects during running, check the reference Technology of objects and clean them up. Garbage collection only supports mac and ios, the reason is that the app is dynamically released during running and not applicable to mobile apps. Therefore, apple introduces ARC to solve the problem. Note that no garbage collection mechanism is set in the XCode6 settings I am currently using.

3. ARC mechanism in IOS

The automatic reference counting mechanism is similar to the garbage collection mechanism, which helps you manage the object lifecycle. The difference is that the garbage collection mechanism decides to release objects at runtime. ARC secretly adds retain and release methods to our code during compilation. You can select a project and use the menu Edit-> Refactor-> Convert to Objective-c arc to Convert the project to ARC. ARC only works for the drop-down object. For more information, see the OC basic tutorial.

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.