天天看點

iOS ARC學習彙總

<a href="http://www.yifeiyang.net/development-of-the-iphone-simply-1/" target="_blank">arc是什麼</a>

arc是ios 5推出的新功能,全稱叫 arc(automatic reference counting)。簡單地說,就是編譯階段自動做了retain/release,原先需要手動添加的用來處理記憶體管理的引用計數的代碼可以自動地由編譯器完成了。就arc并不是gc,不是運作時記憶體管理,不會做malloc/free的工作,它隻是一種代碼靜态分析(static analyzer)工具。

比如,我們生成一個對象:

iOS ARC學習彙總

addretain.png

編譯器會給我們添加上retain/release/autorelease

iOS ARC學習彙總

add2.png

比如,block的寫法,編譯器背後給我們做的事:

iOS ARC學習彙總

add3.png

我們需要手動retain和 release對象。如下:

這個過程很容易出錯。是以蘋果引入arc,在編譯階段自動幫我們做retain/release的工作,如下圖,減少了很多代碼。

iOS ARC學習彙總

add4.png

用“alloc”, “new”, “copy”, or “mutablecopy”等方法生成object的時候,object 的引用計數為1.

when you <code>create</code> an object, it has a retain count of 1. you create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutablecopy” (for example, <code>alloc</code>, <code>newobject</code>, or <code>mutablecopy</code>).

向object發送retain消息,它的引用計數+1.

when you send an object a <code>retain</code> message, its retain count is incremented by 1.

向object發送release消息,引用計數-1.

when you send an object a <code>release</code> message, its retain count is decremented by 1.

向object發送autorelease消息,引用計數-1.當引用計數為0,object被釋放 。

when you send an object a <code>autorelease</code> message, its retain count is decremented by 1 at the end of the current autorelease pool block.if an object’s retain count is reduced to zero, it is <code>deallocated</code>.

iOS ARC學習彙總

trace_obj.png

arc模式下,引用計數的規則還起作用,隻是編譯器做retain,release,autorelease的工作。

<a href="http://www.cnblogs.com/123qw/p/4149105.html" target="_blank">記憶體管理和引用計數</a>

<a href="https://developer.apple.com/videos/play/wwdc2012-406/" target="_blank">wwdc adopting automatic reference counting</a>

<a href="http://www.iwangke.me/2012/06/21/mrc_vs_arc/" target="_blank">翻譯</a>

參考:

<a href="https://developer.apple.com/library/mac/releasenotes/objectivec/rn-transitioningtoarc/introduction/introduction.html" target="_blank">transitioning to arc release notes</a>

<a href="http://www.yifeiyang.net/development-of-the-iphone-simply-7/" target="_blank">規則總結</a>

規則:

you cannot explicitly invoke dealloc, or implement or invoke retain, release, retaincount, or autorelease.

you cannot use nsallocateobject or nsdeallocateobject.

you cannot use object pointers in c structures.

there is no casual casting between id and void *.

you cannot use nsautoreleasepool objects.

you cannot use memory zones.

you cannot give an accessor a name that begins with new. this in turn means that you can’t, for example,

is arc slow?

it depends on what you’re measuring, but generally “no.” the compiler efficiently eliminates many extraneous <code>retain</code>/<code>release</code> calls and much effort has been invested in speeding up the objective-c runtime in general. in particular, the common “return a retain/autoreleased object” pattern is much faster and does not actually put the object into the autorelease pool, when the caller of the method is arc code.

<a href="http://www.techrepublic.com/blog/software-engineer/understand-memory-management-under-arc/" target="_blank">arc強引用和弱引用</a>

strong reference : 強引用指針增加對象引用計數。

weak reference :弱引用指針不增加對象引用計數,對象被釋放後,指針被置為nil

<code>__strong</code>:_strong修飾符表示對對象的“強引用”。持有強引用的變量在超出其作用域時被廢棄,随着強引用的失效,引用的對象會随之釋放

<code>__weak</code>:

使用_strong修飾符的成員變量在持有對象時,很容易發生循環引用。

循環引用容易發生記憶體洩露。記憶體洩露就是應當廢棄的對象在超出其生存周期後繼續存在。

使用_weak修飾符可以避免循環引用。_weak修飾符還有另一有優點。在持有某對象的弱引用時,若該對象被廢棄,則此弱引用将自動失效且處于nil被指派的狀态(空弱引用)。

<code>__unsafe_unretained__</code>

<code>autoreleasing</code>

使用關鍵字的格式,關鍵字在*号之後

對象之間有“父子關系

delegate模式

blocks

<a href="http://www.yifeiyang.net/development-of-the-iphone-simply-4/" target="_blank">arc循環引用</a>

<a href="http://tutuge.me/2015/03/17/what-is-autoreleasepool/" target="_blank">@autoreleasepool-記憶體的配置設定與釋放</a>

<a href="https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/articles/mmautoreleasepools.html#//apple_ref/doc/uid/20000047-cjbfbedi" target="_blank">https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/articles/mmautoreleasepools.html#//apple_ref/doc/uid/20000047-cjbfbedi</a>

使用autorelease pool 的場景:

程式不基于 ui framework.

if you are writing a program that is not based on a ui framework, such as a command-line tool.

在循環中生成大量的臨時變量,可以在for循環中用autorelease pool block。

if you write a loop that creates many temporary objects.

you may use an autorelease pool block inside the loop to dispose of those objects before the next iteration. using an autorelease pool block in the loop helps to reduce the maximum memory footprint of the application.

cocoa application中的每個線程維護自己的 autorelease pool block堆棧,是以開子線程需要生成自己的autorelease pool block.

在ios世界,主要有兩種對象:objective-c 對象和 core foundation 對象0。core foundation 對象主要是有c語言實作的 core foundation framework 的對象,其中也有對象引用計數的概念,隻是不是 cocoa framework::foundation framework 的 retain/release,而是自身的 cfretain/cfrelease 接口。

這兩種對象間可以互相轉換和操作,不使用arc的時候,單純的用c原因的類型轉換,不需要消耗cpu的資源,是以叫做 toll-free bridged

<a href="http://www.yifeiyang.net/development-of-the-iphone-simply-6/" target="_blank">對象轉換</a>

__bridge` transfers a pointer between objective-c and core foundation with no transfer of ownership.

<code>__bridge_retained</code> or <code>cfbridgingretain</code> casts an objective-c pointer to a core foundation pointer and also transfers ownership to you.

you are responsible for calling <code>cfrelease</code> or a related function to relinquish ownership of the object.

<code>__bridge_transfer</code> or <code>cfbridgingrelease</code> moves a non-objective-c pointer to objective-c and also transfers ownership to arc.

arc is responsible for relinquishing ownership of the object.

<a href="http://www.yifeiyang.net/category/embedded/iphone-embedded/%e6%b7%b1%e5%85%a5%e6%b5%85%e5%87%ba/arc/" target="_blank">易飛揚,arc系列</a>

<a href="" target="_blank">了解arc強引用和弱引用指針類型</a>

<a href="http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1" target="_blank">beginarc</a>