Objective-C 自动引用计数和垃圾回收有什么区别?

What is the difference between Objective-C automatic reference counting and garbage collection?(Objective-C 自动引用计数和垃圾回收有什么区别?)

本文介绍了Objective-C 自动引用计数和垃圾回收有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 Xcode 4.2 中引入的新的自动引用计数 (ARC),我们不再需要在 Objective-C 中手动管理保留/释放.

With the new automatic reference counting (ARC) introduced in Xcode 4.2, we no longer need to manually manage retain / release in Objective-C.

这似乎类似于垃圾收集,就像在 Mac 上的 Objective-C 和其他语言中所做的那样.ARC 与垃圾回收有何不同?

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?

推荐答案

正如我在回答中描述的那样 这里,ARC可以提供最好的手动内存管理和跟踪垃圾收集.它主要消除了开发人员在 Objective-C 对象上跟踪手动保留、释放和自动释放的需要,同时避免了垃圾收集器进程的需要,该进程会耗尽移动设备上的有限资源并导致正在运行的应用程序偶尔出现卡顿.

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 通过应用所有 Objective-C 开发人员多年来必须使用的规则,在编译时插入引用计数所需的适当保留和释放.这使开发人员不必自己管理它.因为保留和释放是在编译时插入的,所以不需要收集器进程来不断地清理内存并删除未引用的对象.

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.

跟踪垃圾收集相对于 ARC 的一个小优势是 ARC 不会为您处理保留周期,跟踪垃圾收集可以拾取这些.

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.

有关该主题的精彩阅读来自 Apple 的此线程Objective-C 邮件列表,其中 Chris Lattner 是这样说的:

A great read on the subject comes from this thread on Apple's Objective-C mailing list, where Chris Lattner has this to say:

GC 优于 ARC 的主要优点是它收集保留循环.第二个优点是保留"分配是原子",因为它们是一个简单的存储.ARC有几大与 libauto GC 相比的优势:

The primary advantage of GC over ARC is that it collects retain cycles. A secondary advantage is that "retained" assignments are "atomic" because they are a simple store. ARC has several big advantages over libauto GC:

  1. 它具有确定性的对象回收(当对该对象的最后一个强引用消失时),其中 GC 有时会释放一个对象稍后".这定义了可能存在于 GC 中的一类细微错误由于收集器未触发在错误的窗口".
  2. ARC 的高水位线通常比 GC 低得多,因为对象释放得更快.
  3. libauto 提供了一个脆弱的编程模型,你必须小心不要丢失写屏障等.
  4. 并非所有的系统框架都是 GC 干净的,而且这些框架偶尔会随着它们的发展而退化.
  5. ARC 没有假根.libauto 保守地扫描堆栈,这意味着看起来像指针的整数可以根对象图.
  6. ARC 没有任何东西可以启动和停止您的应用,从而导致 UI 卡顿.就 GC 实现而言,libauto 相当先进go 因为它不会立即停止每个线程,但它仍然会通常最终会停止所有 UI 线程.

我目前正在将我的手动内存管理项目以及使用 Objective-C 垃圾回收的项目迁移到 ARC.在几个 Mac 应用程序中使用垃圾收集一段时间后,我发现将这些项目迁移到 ARC 有一些显着优势.

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.

这篇关于Objective-C 自动引用计数和垃圾回收有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Objective-C 自动引用计数和垃圾回收有什么区别?

基础教程推荐