Objective-c provides two memory management mechanism MRC (MannulReference Counting) and ARC (Automatic Reference Counting), respectively, to provide manual and automatic memory management, to meet different needs.pay attention toXcode 4.1And earlier versions do notARC, The difference between MRC and ARC is shown in Fig.We need to understandMRCBut the actual use strong pushARC。
FIG 1 MRC differs schematic ARC
1. MRC (MannulReference Counting) language Objective-c
In the MRC memory management, and management of related variables methods are: retain, release and autorelease. retain and release method is a reference count operation, when the reference count is zero, release the memory automatically. Variable and can be used NSAutoreleasePool object, added autorelease pool (the autorelease call) managing, recovering memory when drain.
(1) The retain, the effect of this method is the ownership of the data memory attached to another pointer variable, the reference count plus 1, i.e. retainCount + = 1;
(2) release, which is a pointer variable release ownership of the memory data, the reference count is decremented by 1, i.e. retainCount- = 1;
(3) autorelease, which is the subject of memory management into autoreleasepool.
Sample code:
1 //Suppose Number of predefined classes 2 Number* num = [[Number alloc] init]; 3 Number* num2 = [num retain]; //At this time, a reference count + 1, is now 2 4 [num2 release]; //num2 release ownership of the memory reference data count -1, it is now 1; 5 [num release]; //num release ownership of the memory reference data count -1, is 0; 6 [num add:1 and 2]; //bug, this time the memory is released. 7 //autoreleasepool use in the MRC management mode, we abandon previous usage, use NSAutoreleasePool object, a novel approach to @autoreleasepool 8 @autoreleasepool { 9 Number* num = [[Number alloc] init]; 10 [num autorelease]; //Autoreleasepool managed by the memory of their release 11 }
Objective-c of the identifier attribute can be summarized as:
@property (nonatomic/atomic,retain/assign/copy, readonly/readwrite) Number* num;
(1) nonatomic / atomic, indicates whether the property is multi-thread safe, is not the use of thread-locking, the default is atomic,
(2) retain / assign / copy, is related to the properties of memory management,
l assign"is the default. In the setter that is created by @synthesize, the value willsimply be assigned to the attribute, don’t operate the retain count. Myunderstanding is that "assign" should be used for non-pointer attributes.
l "retain"is needed when the attribute is a pointer to an object. The setter generated by@synthesize will retain (aka add a retain count) the object. You will need torelease the object when you are finished with it.
l "copy"is needed when the object is mutable. Use this if you need the value of theobject as it is at this moment, and you don't want that value to reflect anychanges made by other owners of the object. You will need to release the objectwhen you are finished with it because you are retaining the copy.
readwrite /readonly -"readwrite" is the default. When you @synthesize, both a getter and asetter will be created for you. If you use "readonly", no setter willbe created. Use it for a value you don't want to ever change after the instantiationof the object.
2. Objective-c ARC language (AutomaticReference Counting)
In the ARC identifier associated with the memory management can be divided into variable identifier and attribute identifiers for variables default to __strong, and for property defaults to unsafe_unretained. There autoreleasepool.
For identifier variables are:
(1) __strong,is the default. An object remains “alive” as long as there is a strong pointerto it.
(2) __weak,specifies a reference that does not keep the referenced object alive. A weakreference is set to nil when there are no strong references to the object.
(3)__unsafe_unretained,specifies a reference that does not keep the referenced object alive and is notset to nil when there are no strong references to the object. If the object itreferences is deallocated, the pointer is left dangling.
(4)__autoreleasing,is used to denote arguments that are passed by reference (id *) and areautoreleased on return,managedby Autoreleasepool.
For the use of variable identifiers:
__strong Number* num = [[Number alloc]init];
In ARC memory management scheme, which identifier is present the following properties:
@property (nonatomic/atomic, assign/retain/strong/weak/unsafe_unretained/copy,readonly/readwrite) Number* num;//The default is unsafe_unretained
Wherein the same identifier meaning assign / retain / copy and the property of the MRC, similar strong and retain, assign similar unsafe_unretained, the same strong / weak / unsafe_unretained ARC and variable identifiers sense, only one of which identifies the attribute, with a identification of the variable (underlined with two short __). Other identifiers listed under the same sense of MRC.
(1) to assign, you can use this property of the scalar types (such as int). You can imagine a float, it is not an object, so it can not retain, copy.
(2) Copy, a copy of the specified object to be used (the depth of replication), a value before sending a release message. Like substantially retain, but do not increase the reference count, a new memory allocation is to place it. Especially for NSString, if you do not want to change the existing, to use this, because NSMutableString, also NSString.
Core Foundation with respect to the objective-cObject exchange, ARC management mechanisms need to use are:
(1) (__bridge_transfer<NSType>) op oralternatively CFBridgingRelease(op) iSUSEd to consume a retain-count of a CFTypeRef whiletransferring it over to ARC. This could also be represented by id someObj =(__bridge <NSType>) op; CFRelease(op);
(2) (__bridge_retained<CFType>) op oralternatively CFBridgingRetain(op) isused to hand an NSObject overto CF-land while giving it a +1 retain count. You should handle a CFTypeRefyoucreate this way the same as you would handle a result of CFStringCreateCopy().This could also be represented by CFRetain((__bridge CFType)op); CFTypeRef someTypeRef =(__bridge CFType)op;
(3) __bridge justcasts between pointer-land and Objective-C object-land. If you have noinclination to use the conversions above, use this one.