1. Objective-c語言中的MRC(MannulReference Counting)
在MRC的內(nèi)存管理模式下榆综,與對變量的管理相關(guān)的方法有:retain,release和autorelease令宿。retain和release方法操作的是引用記數(shù),當(dāng)引用記數(shù)為零時(shí)卵皂,便自動(dòng)釋放內(nèi)存。并且可以用NSAutoreleasePool對象砚亭,對加入自動(dòng)釋放池(autorelease 調(diào)用)的變量進(jìn)行管理灯变,當(dāng)drain時(shí)回收內(nèi)存。
(1)retain捅膘,該方法的作用是將內(nèi)存數(shù)據(jù)的所有權(quán)附給另一指針變量添祸,引用數(shù)加1,即retainCount+= 1;
(2)release寻仗,該方法是釋放指針變量對內(nèi)存數(shù)據(jù)的所有權(quán)刃泌,引用數(shù)減1,即retainCount-= 1;
(3)autorelease,該方法是將該對象內(nèi)存的管理放到autoreleasepool中耙替。
//假設(shè)Number為預(yù)定義的類
Number* num = [[Number alloc] init];
Number* num2 = [num retain];? ? ? ? //此時(shí)引用記數(shù)+1亚侠,現(xiàn)為2
[num2 release];? ? ? ? ? ? ? ? ? ? ? //num2 釋放對內(nèi)存數(shù)據(jù)的所有權(quán) 引用記數(shù)-1,現(xiàn)為1;
[num release];? ? ? ? ? ? ? ? ? ? ? //num釋放對內(nèi)存數(shù)據(jù)的所有權(quán) 引用記數(shù)-1,現(xiàn)為0;
[num add:1 and 2];? ? ? ? ? ? ? ? ? //bug,此時(shí)內(nèi)存已釋放俗扇。
//autoreleasepool 的使用 在MRC管理模式下硝烂,我們摒棄以前的用法,NSAutoreleasePool對象的使用铜幽,新手段為@autoreleasepool
@autoreleasepool {
Number* num = [[Number alloc] init];
[num autorelease];? ? ? ? ? ? //由autoreleasepool來管理其內(nèi)存的釋放
}
對與Objective-c中屬性的標(biāo)識(shí)符可以總結(jié)為:
@property (nonatomic/atomic,retain/assign/copy,readonly/readwrite) Number* num;
(1)nonatomic/atomic滞谢,表示該屬性是否是對多線程安全的,是不是使用線程鎖除抛,默認(rèn)為atomic狮杨,
(2)retain/assign/copy,是有關(guān)對該屬性的內(nèi)存管理的到忽。
2. Objective-c語言中的ARC(AutomaticReference Counting)
在ARC中與內(nèi)存管理有關(guān)的標(biāo)識(shí)符橄教,可以分為變量標(biāo)識(shí)符和屬性標(biāo)識(shí)符,對于變量默認(rèn)為__strong喘漏,而對于屬性默認(rèn)為unsafe_unretained颤陶。也存在autoreleasepool。
對于變量的標(biāo)識(shí)符有:
(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.
對于變量標(biāo)識(shí)符的用法:
__strong Number* num = [[Number alloc]init];
在ARC內(nèi)存管理模式下搅方,其屬性的標(biāo)識(shí)符存在以下幾種:
@property (nonatomic/atomic, assign/retain/strong/weak/unsafe_unretained/copy,readonly/readwrite) Number* num;//默認(rèn)為unsafe_unretained
其中assign/retain/copy與MRC下property的標(biāo)識(shí)符意義相同,strong類似與retain,assign類似于 unsafe_unretained姨涡,strong/weak/unsafe_unretained與ARC下變量標(biāo)識(shí)符意義相同涛漂,只是一個(gè)用于屬性的標(biāo)識(shí),一個(gè)用于變量的標(biāo)識(shí)(帶兩個(gè)下劃短線__)悠轩。所列出的其他的標(biāo)識(shí)符與MRC下意義相同鉴象。
(1)對于assign,你可以對標(biāo)量類型(如int)使用這個(gè)屬性。你可以想象一個(gè)float,它不是一個(gè)對象稽犁,所以它不能retain、copy来屠。
(2)對于copy俱笛,指定應(yīng)該使用對象的副本(深度復(fù)制)传趾,前一個(gè)值發(fā)送一條release消息。基本上像retain榕订,但是沒有增加引用計(jì)數(shù),是分配一塊新的內(nèi)存來放置它。特別適用于NSString,如果你不想改變現(xiàn)有的螺垢,就用這個(gè)枉圃,因?yàn)镹SMutableString,也是NSString坎穿。
舊工程配置arc方案:
1,直接在targets->build phases中修改compiler Flags,是否支持arc。添加:-fobjc-arc玲昧,就可以讓舊項(xiàng)目支持arc篮绿。如果想讓原來支持arc的不使用arc則添加-fno-objc-arc
2,因?yàn)樵赽uild phases中可以改變是否支持arc孵延,所以應(yīng)該在代碼中添加判斷是否支持arc亲配,這樣不管以后.m的arc是否改變尘应,都不用再次調(diào)整代碼吼虎。