ARC與MRC

Objective-c中提供了兩種內存管理機制MRCMannulReferenceCounting)和ARC(Automatic ReferenceCounting),分別提供對內存的手動和自動管理,來滿足不同的需求。

注意的是Xcode4.1及其以前版本沒有ARC需要理解MRC,但實際使用時強推ARC


1. Objective-c語言中的MRC(MannulReference Counting)

在MRC的內存管理模式下,與對變量的管理相關的方法有:retain,release和autorelease。retain和release方法操作的是引用記數,當引用記數爲零時,便自動釋放內存。並且可以用NSAutoreleasePool對象,對加入自動釋放池(autorelease調用)的變量進行管理,當drain時回收內存。

(1)      retain,該方法的作用是將內存數據的所有權附給另一指針變量,引用數加1,即retainCount+= 1;

(2)      release,該方法是釋放指針變量對內存數據的所有權,引用數減1,即retainCount-= 1;

(3)      autorelease,該方法是將該對象內存的管理放到autoreleasepool中。

示例代碼:

//假設Number爲預定義的類

Number* num = [[Number alloc] init];

Number* num2 = [num retain];//此時引用記數+1,現爲2

[num2 release]; //num2 釋放對內存數據的所有權 引用記數-1,現爲1;

[num release];//num釋放對內存數據的所有權 引用記數-1,現爲0;

[num add:1 and 2];//bug,此時內存已釋放。

 

//autoreleasepool 的使用 在MRC管理模式下,我們摒棄以前的用法,NSAutoreleasePool對象的使用,新手段爲@autoreleasepool

 

@autoreleasepool {

       Number* num = [[Number alloc] init];

              [numautorelease];//由autoreleasepool來管理其內存的釋放

   }

 

對與Objective-c中屬性的標識符可以總結爲:

@property (nonatomic/atomic,retain/assign/copy, readonly/readwrite) Number* num;

(1)      nonatomic/atomic,表示該屬性是否是對多線程安全的,是不是使用線程鎖,默認爲atomic,

(2)      retain/assign/copy,是有關對該屬性的內存管理的,

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.

(3)      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(AutomaticReference Counting)

在ARC中與內存管理有關的標識符,可以分爲變量標識符和屬性標識符,對於變量默認爲__strong,而對於屬性默認爲unsafe_unretained。也存在autoreleasepool。

 使用ARC的好處:

1,代碼變得簡潔;

2,不用擔心內存管理和內存泄露;

3,代碼高速化,由於使用了編譯器管理引用計數,減少了低效代碼的可能性;

以後寫

Objective-C

的代碼變得簡單多了

以後寫

Objective-C

的代碼變得簡單多了

對於變量的標識符有:

(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.

 

對於變量標識符的用法:

__strong Number* num = [[Number alloc]init];

 

在ARC內存管理模式下,其屬性的標識符存在以下幾種:

@property (nonatomic/atomic, assign/retain/strong/weak/unsafe_unretained/copy,readonly/readwrite) Number* num;//默認爲unsafe_unretained

 

其中assign/retain/copy與MRC下property的標識符意義相同,strong類似與retain,assign類似於unsafe_unretained,strong/weak/unsafe_unretained與ARC下變量標識符意義相同,只是一個用於屬性的標識,一個用於變量的標識(帶兩個下劃短線__)。所列出的其他的標識符與MRC下意義相同。

(1)對於assign,你可以對標量類型(如int)使用這個屬性。你可以想象一個float,它不是一個對象,所以它不能retain、copy。

(2)對於copy,指定應該使用對象的副本(深度複製),前一個值發送一條release消息。基本上像retain,但是沒有增加引用計數,是分配一塊新的內存來放置它。特別適用於NSString,如果你不想改變現有的,就用這個,因爲NSMutableString,也是NSString。

 

對於Core Foundation與objective-cObject進行交換時,需要用到的ARC管理機制有:

 

(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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章