ios Objective-C 自动引用计数和垃圾回收有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7874342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Objective-C automatic reference counting and garbage collection?
提问by Gauging
With the new automatic reference counting (ARC) introduced in Xcode 4.2, we no longer need to manually manage retain / release in Objective-C.
使用 Xcode 4.2 中引入的新自动引用计数 (ARC),我们不再需要在 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?
这似乎类似于垃圾收集,就像在 Mac 上的 Objective-C 和其他语言中所做的那样。ARC 与垃圾收集有何不同?
回答by Brad Larson
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 通过应用所有 Objective-C 开发人员多年来必须使用的规则,在编译时插入引用计数所需的适当保留和释放。这使开发人员不必自己管理。因为保留和释放是在编译时插入的,所以不需要收集器进程来持续清理内存和删除未引用的对象。
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.
跟踪垃圾收集相对于 ARC 的一个小优势是 ARC不会为您处理保留周期,而跟踪垃圾收集可以在其中捡起这些。
A great read on the subject comes from this thread on Apple's Objective-C mailing list, where Chris Lattner has this to say:
关于这个主题的一个很好的阅读来自Apple 的 Objective-C 邮件列表上的这个线程,其中 Chris Lattner 有这样说:
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:
- It has deterministic reclamation of objects (when the last strong reference to the object goes away) where GC frees an object "sometime later". This defines away a class of subtle bugs that can exist in GC apps that aren't exposed because the collector doesn't trigger "in the buggy window".
- The high water mark is generally much lower with ARC than GC because objects are released sooner.
- libauto provides a fragile programming model, you have to be careful to not lose write barriers etc.
- not all of the system frameworks are GC clean, and the frameworks do occasionally regress as they evolve.
- ARC doesn't suffer from false roots. libauto conservatively scans the stack, which means that integers that look like pointers can root object graphs.
- ARC doesn't have anything that kicks in and stops your app, causing UI stutters. libauto is pretty advanced as far as GC implementations go because it doesn't immediately stop every thread, but it still does usually end up stopping all the UI threads.
GC 优于 ARC 的主要优势在于它收集保留周期。第二个优点是“保留”分配是“原子的”,因为它们是一个简单的存储。与 libauto GC 相比,ARC 有几大优势:
- 它具有确定性的对象回收(当对对象的最后一个强引用消失时),其中 GC“稍后”释放对象。这定义了一类可能存在于 GC 应用程序中的细微错误,这些错误不会暴露,因为收集器不会“在错误窗口中”触发。
- ARC 的高水位线通常比 GC 低得多,因为对象释放得更快。
- libauto 提供了一个脆弱的编程模型,你必须小心不要丢失写障碍等。
- 并非所有的系统框架都是 GC 干净的,并且这些框架在发展过程中偶尔会倒退。
- ARC 不会受到假根的影响。libauto 保守地扫描堆栈,这意味着看起来像指针的整数可以根对象图。
- ARC 没有任何东西可以启动和停止您的应用程序,从而导致 UI 卡顿。就 GC 实现而言,libauto 非常先进,因为它不会立即停止每个线程,但它通常最终会停止所有 UI 线程。
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 垃圾收集的项目迁移到 ARC。在几个 Mac 应用程序中使用垃圾回收一段时间后,我发现将这些项目转移到 ARC 有一些显着的优势。
回答by Meseery
ARCrely on a compile time "referenced" objects which make it efficient in a low-power mode environments (Mobile devices).
ARC依赖于编译时“引用”对象,这使其在低功耗模式环境(移动设备)中高效。
GCrely on a runtime based "reachable" objects which make it efficient in a multi-threaded environment.
GC依赖于基于运行时的“可达”对象,这使其在多线程环境中高效。
Operation
手术
ARC injects a code into the executable to be executed "automatically" on unused objects depending on their reference count.
ARC 将代码注入到可执行文件中,以根据它们的引用计数在未使用的对象上“自动”执行。
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
GC 在运行时工作,因为它将检测未使用的对象图(将消除保留循环)并在不确定的时间间隔内删除它们
Advantages of Automatic Reference Counting
自动引用计数的优点
- Real-time, deterministic destruction of objects as they become unused.
- No background processing.
- 当对象变得不使用时,对它们进行实时、确定性的销毁。
- 没有后台处理。
Advantages of Garbage Collection
垃圾回收的优势
- GC can clean up entire object graphs, including retain cycles.
- GC proceed in the background, so less memory management work is done as part of the regular application flow.
- GC 可以清理整个对象图,包括保留周期。
- GC 在后台进行,因此作为常规应用程序流程的一部分完成的内存管理工作较少。
Disadvantages of Automatic Reference Counting
自动引用计数的缺点
- ARC cannot handle retain cycles automatically.
- ARC 无法自动处理保留循环。
Disadvantages of Garbage Collection
垃圾收集的缺点
- Because GC happens in the background, the exact time frame for object releases is undetermined.
- When a GC happens, other threads in the application may be temporarily put on hold.
- 因为 GC 发生在后台,对象释放的确切时间框架是不确定的。
- 当 GC 发生时,应用程序中的其他线程可能会暂时搁置。
回答by Jon Harrop
How does ARC differ from garbage collection?
ARC 与垃圾收集有何不同?
ARC is a form of garbage collection.
ARC 是垃圾收集的一种形式。
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?.
您可能的意思是“ARC 和跟踪垃圾收集(如 JVM 和 .NET)之间有什么区别?”。主要区别在于 ARC 速度较慢且会泄漏循环。这就是 JVM 和 .NET 都使用跟踪垃圾收集器的原因。有关更多信息,请阅读引用计数和跟踪垃圾收集比较如何?.
回答by objectivecdeveloper
the short and sweet answer is as follow:
简短而甜蜜的答案如下:
GC of java is Runtime, while ARC is compile time.
java的GC是Runtime,ARC是编译期。
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.
GC 在运行时引用对象并检查对象运行时的依赖关系。虽然 ARC 在编译时附加了 release、retain、autorelease 调用。