本文介绍了iOS 5 有垃圾收集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从 iOS 5 开始,我是否不再需要担心内存管理?此外,是否必须重写为 iOS 4 及更早版本编写的所有程序以允许 iOS 为您管理内存?

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?

推荐答案

您似乎在谈论其他答案中提到的自动引用计数.ARC 是一种 GC,它可以自动释放内存,但与好的垃圾收集器有许多不同.

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.

首先,它主要是一种编译器技术.编译器知道 Cocoa 的引用计数准则,因此它根据规则将保留和释放插入到它们应该在的位置.它的工作方式就像您自己编写保留和释放一样——它只是为您插入它们.正常的垃圾收集器会在程序运行时跟踪程序的内存.

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循环(如果对象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 一起使用的垃圾收集器必须扫描未引用的内存并收集它——这很昂贵,并且可能导致在较慢的系统上卡顿"——但它们只需要偶尔这样做,理论上甚至可以微调它们的收集周期与程序实际使用内存的方式相匹配.一般来说,GC 程序会比非 GC 程序使用更多的内存,并且在 GC 决定收集时会显着减慢.另一方面,ARC 将扫描"移至编译时并在内存可用时立即释放内存,但它必须不断更新对象引用计数,而不是像收集器一样等待垃圾堆积.

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.

这篇关于iOS 5 有垃圾收集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:12