問題描述
從 iOS 5 開始,我是否不再需要擔心內(nèi)存管理?此外,是否必須重寫為 iOS 4 及更早版本編寫的所有程序以允許 iOS 為您管理內(nèi)存?
Do I no longer have to worry about memory management iOS 5 onwards? Also, will all programs written for iOS 4 and earlier versions have to be rewritten to allow iOS to manage the memory for you?
推薦答案
您似乎在談論其他答案中提到的自動引用計數(shù).ARC 是一種 GC,它可以自動釋放內(nèi)存,但與好的垃圾收集器有許多不同.
You appear to be talking about Automatic Reference Counting, mentioned in other answers. ARC is a kind of GC in that it automates memory freeing, but has a number of differences from a good garbage collector.
首先,它主要是一種編譯器技術(shù).編譯器知道 Cocoa 的引用計數(shù)準則,因此它根據(jù)規(guī)則將保留和釋放插入到它們應該在的位置.它的工作方式就像您自己編寫保留和釋放一樣——它只是為您插入它們.正常的垃圾收集器會在程序運行時跟蹤程序的內(nèi)存.
Firstly, it's mainly a compiler technology. The compiler knows about Cocoa's reference-counting guidelines, so it inserts retains and releases where they should be according to the rules. It works just like if you'd written the retains and releases yourself — it simply inserts them for you. Normal garbage collectors keep track of your program's memory while it is running.
第二,因為它就像retain和release一樣,所以它不能捕獲retain循環(huán)(如果對象A保留了對象B,而對象B保留了對象A,并且沒有其他任何引用,它們都成為不朽的).您需要采取相同的預防措施來防止它們.
Second, since it is just like retain and release, it can't catch retain cycles (if Object A retains Object B and Object B retains Object A, and nothing else references either of them, they both become immortal). You need to take the same precautions to prevent them.
它還使用與自動垃圾收集器不同的資源.與 Objective-C 一起使用的垃圾收集器必須掃描未引用的內(nèi)存并收集它——這很昂貴,并且可能導致在較慢的系統(tǒng)上卡頓"——但它們只需要偶爾這樣做,理論上甚至可以微調(diào)它們的收集周期與程序?qū)嶋H使用內(nèi)存的方式相匹配.一般來說,GC 程序會比非 GC 程序使用更多的內(nèi)存,并且在 GC 決定收集時會顯著減慢.另一方面,ARC 將掃描"移至編譯時并在內(nèi)存可用時立即釋放內(nèi)存,但它必須不斷更新對象引用計數(shù),而不是像收集器一樣等待垃圾堆積.
It also uses resources differently from an automatic garbage collector. The garbage collectors used with Objective-C have to scan for unreferenced memory and collect it — which is expensive, and can lead to "stuttering" on slower systems — but they only have to do this occasionally, and in theory can even fine-tune their collection cycles to match how a program actually uses its memory. In general, a GC program will use more memory than a non-GC program and will slow down significantly when the GC decides to collect. ARC, on the other hand, moves the "scanning" to compile-time and frees memory as soon as it's available, but it has to constantly update object reference counts instead of waiting for garbage to build up like a collector.
這篇關(guān)于iOS 5 有垃圾收集嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!