iOS Learning note 3-objective C-Simple memory management

Source: Internet
Author: User

Today's brief introduction to simple memory management, after IOS5.0 Apple added the arc mechanism (Automatic Reference counting), to developers a lot of convenience, but in order to better understand the iOS memory management mechanism, still need to understand it.

1. In OC, each object has a hold count, and each object has a retention count of 1 at the time of creation, and a retention count of 0 when released.

2. Create an automatically freed object

When a method is required to create an object, returning the object in the form of an automatic release is a good programming practice

+ (CAR *) car

{

Car *mycar = [[Car alloc] init];

return [MyCar autorelease];

}

3. Create a reserved property

@property (retain) Nsarray *colors;

Synthesize automatically retains the object after it is created, and the previous value is automatically freed if the object is re-assigned

4. Other ways to create objects

General rules: By Alloc, new, create, copy of any method to build an object, you have to assume the responsibility to release the object, and the class method is different, the method with these words will not generally return the object automatically released

5. Releasing objects

Release property: Self.year = nil; A custom assignment method that invokes the OC composition and frees any objects previously assigned to the property

Release instance variable (non-attribute): [age release]; An instance variable can point to an object that holds a count of +1 at any time during the object's life cycle, and must release any object currently assigned to age, thus placing the count at 0;

Friends interested in Android&ios can join our discussion QQ Group, here we only discuss dry goods:

iOS Group: 220223507

Android Group: 282552849

Welcome to follow my Sina Weibo and I exchange: @ Tang Yu _ryan

iOS Learning note 3-objective C-Simple memory management

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.