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

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

问题描述

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

这看起来很相似垃圾收集,Mac上的Objective-C以及其他语言中的操作。 ARC与垃圾收集有什么区别?

解决方案

正如我在,ARC可以提供最好的手动内存管理和跟踪垃圾回收功能。它大部分消除了开发人员跟踪Objective-C对象上的手动保留,发布和自动释放的需求,但却避免了需要一个垃圾收集器进程,它可以使用移动设备上的有限资源,并导致正在运行的应用程序中偶尔出现断断续续。

ARC通过应用所有Objective-C开发人员多年来不得不使用的规则,在编译时插入引用计数所需的适当保留和释放。这使开发人员无需自己管理。由于保留和释放是在编译时插入的,因此不需要收集器进程来持续扫描内存并移除未引用的对象。



跟踪垃圾回收的一个优点是ARC那是ARC 不会处理你的保留周期,追踪垃圾回收可以挑选这些。



关于这个主题的一个很好的阅读来自这个线程在Apple的Objective-C邮件列表中,其中Chris Lattner有这样的说法:


GC优于ARC的主要优点是收集保留
个周期。第二个优点是保留作业是
原子,因为它们是一个简单的商店。 ARC与libauto GC相比具有几大bb $ b优势:


  1. 它具有确定性的对象回收(当对象的最后一个强引用消失),在那里GC释放一段时间
    的对象。这定义了一类可能存在于GC
    应用程序中的微妙错误,这些应用程序由于收集程序不会触发
    错误窗口中的
    应用程序而未公开。

  2. ARC的高水位通常要比GC低很多,因为对象很快就会被释放。

  3. libauto提供了一个脆弱的编程模型,您必须小心不要丢失写入障碍等。

  4. 并非所有的系统框架都是GC清理的,并且框架偶尔会随着它们的发展而退步。

  5. ARC不会遭受错误的根源。 libauto保守地扫描堆栈,这意味着看起来像指针的整数可以以
    对象图为根。
  6. ARC没有任何可以启动并停止应用程序的操作,导致UI口吃。至于GC实现
    ,libauto是非常先进的,因为它不会立即停止每个线程,但它仍然会通过
    停止所有UI线程。


目前,我正在将手动内存托管项目以及使用Objective-C垃圾收集的项目迁移到ARC。在一段时间的Mac应用程序中使用垃圾收集功能一段时间后,我发现将这些项目移到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?

解决方案

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:

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. 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".
  2. The high water mark is generally much lower with ARC than GC because objects are released sooner.
  3. libauto provides a fragile programming model, you have to be careful to not lose write barriers etc.
  4. not all of the system frameworks are GC clean, and the frameworks do occasionally regress as they evolve.
  5. ARC doesn't suffer from false roots. libauto conservatively scans the stack, which means that integers that look like pointers can root object graphs.
  6. 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.

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自动引用计数和垃圾收集有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆