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