Objective-C 自动引用计数和垃圾回收有什么区别?
通过 Xcode 4.2 中引入的新自动引用计数(ARC),我们不再需要在 Objective-C 中手动管理保留/释放。
这看起来类似于 Mac 上的 Objective-C 和其他语言中的垃圾收集。 ARC 与垃圾回收有何不同?
With the new automatic reference counting (ARC) introduced in Xcode 4.2, we no longer need to manually manage retain / release in Objective-C.
This seems similar to garbage collection, as done in Objective-C on the Mac, and in other languages. How does ARC differ from garbage collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如我在此处、ARC可以提供最好的手动内存管理和跟踪垃圾收集。它主要消除了开发人员跟踪 Objective-C 对象上的手动保留、释放和自动释放的需要,同时也避免了对垃圾收集器进程的需要,垃圾收集器进程可能会耗尽移动设备上的有限资源并导致正在运行的应用程序偶尔出现卡顿。
ARC 通过应用多年来所有 Objective-C 开发人员必须使用的规则,在编译时插入引用计数所需的适当保留和释放。这使开发人员不必自己管理。由于保留和释放是在编译时插入的,因此不需要收集器进程来不断清理内存并删除未引用的对象。
跟踪垃圾收集相对于 ARC 的一个微小优势是 ARC 不会为您处理保留周期,跟踪垃圾收集可以拾取这些内容。
关于这个主题的精彩读物来自 Apple 的 Objective-C 上的这个线程邮件列表,克里斯·拉特纳 (Chris Lattner) 说道:
我目前正在将手动内存管理项目以及使用 Objective-C 垃圾收集的项目迁移到 ARC。在几个 Mac 应用程序中使用垃圾收集一段时间后,我发现将这些项目迁移到 ARC 具有一些显着的优势。
As I describe in my answer here, ARC can provide the best of both manual memory management and tracing garbage collection. It mostly removes the need for a developer to track manual retains, releases, and autoreleases on Objective-C objects, yet avoids the need for a garbage collector process which can use up limited resources on a mobile device and cause occasional stutters in a running application.
ARC inserts the appropriate retains and releases required for reference counting at compile time, by applying the rules that all Objective-C developers have had to use over the years. This frees the developer from having to manage this themselves. Because the retains and release are inserted at compile time, no collector process is needed to continually sweep memory and remove unreferenced objects.
One slight advantage that tracing garbage collection has over ARC is that ARC will not deal with retain cycles for you, where tracing garbage collection can pick these up.
A great read on the subject comes from this thread on Apple's Objective-C mailing list, where Chris Lattner has this to say:
I am currently migrating both my manually memory managed projects, as well as those using Objective-C garbage collection, to ARC. After using garbage collection in a couple of Mac applications for a while now, I see some significant advantages in moving these projects to ARC.
ARC 依赖于编译时“引用”对象,这使其在低功耗模式环境(移动设备)中高效。
GC 依赖于基于运行时的“可访问”对象,这使其在多线程环境中高效。
ARC操作
将代码注入到可执行文件中,根据未使用的对象的引用计数“自动”执行。
GC 在运行时工作,因为它将检测未使用的对象图(将消除保留周期)并以不确定的时间间隔删除它们
自动引用计数的优点
未使用。
垃圾收集的优点
作为常规申请流程的一部分。
自动引用计数的缺点
垃圾收集的缺点
版本尚未确定。
暂时搁置。
ARC rely on a compile time "referenced" objects which make it efficient in a low-power mode environments (Mobile devices).
GC rely on a runtime based "reachable" objects which make it efficient in a multi-threaded environment.
Operation
ARC injects a code into the executable to be executed "automatically" on unused objects depending on their reference count.
GC works in the runtime as it will detect the unused object graphs (will eliminate retain-cycles) and remove them on an indeterminate time intervals
Advantages of Automatic Reference Counting
unused.
Advantages of Garbage Collection
as part of the regular application flow.
Disadvantages of Automatic Reference Counting
Disadvantages of Garbage Collection
releases is undetermined.
temporarily put on hold.
ARC 是垃圾收集的一种形式。
您可能的意思是“ARC 和跟踪垃圾收集(如 JVM 和 .NET)之间有什么区别?”。主要区别在于 ARC 速度较慢并且存在泄漏周期。这就是 JVM 和 .NET 都使用跟踪垃圾收集器的原因。有关更多信息,请阅读引用计数和跟踪垃圾收集如何进行比较?。
ARC is a form of garbage collection.
You probably mean "what is the difference between ARC and tracing garbage collection (like the JVM and .NET)?". The main differences are that ARC is slower and leaks cycles. That's why the JVM and .NET both use tracing garbage collectors. For more information, please read How do reference counting and tracing garbage collection compare?.
简短而甜蜜的答案如下:
java的GC是运行时,而ARC是编译时。
GC在运行时引用对象并检查对象运行时的依赖关系。
而 ARC 在编译时附加了release、retain、autorelease调用。
the short and sweet answer is as follow:
GC of java is Runtime, while ARC is compile time.
GC has reference to the objects at runtime and check for the dependencies of object runtime.
While ARC appends the release, retain, autorelease calls at compiletime.