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

发布于 2024-12-11 20:12:28 字数 131 浏览 0 评论 0原文

通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绮烟 2024-12-18 20:12:28

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

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

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

关于这个主题的精彩读物来自 Apple 的 Objective-C 上的这个线程邮件列表,克里斯·拉特纳 (Chris Lattner) 说道:

GC 相对于 ARC 的主要优点是它收集保留
循环。第二个优点是“保留”的分配
“原子”因为它们是一个简单的商店。 ARC有几大
相对于 libauto GC 的优点:

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

我目前正在将手动内存管理项目以及使用 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:

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.

无力看清 2024-12-18 20:12:28

ARC 依赖于编译时“引用”对象,这使其在低功耗模式环境(移动设备)中高效。

GC 依赖于基于运行时的“可访问”对象,这使其在多线程环境中高效。

ARC操作

将代码注入到可执行文件中,根据未使用的对象的引用计数“自动”执行。

GC 在运行时工作,因为它将检测未使用的对象图(将消除保留周期)并以不确定的时间间隔删除它们

自动引用计数的优点

  • 实时、确定性地销毁对象
    未使用。
  • 无后台处理。

垃圾收集的优点

  • GC 可以清理整个对象图,包括保留周期。
  • GC 在后台进行,因此减少了内存管理工作
    作为常规申请流程的一部分。

自动引用计数的缺点

  • ARC 无法自动处理循环引用。

垃圾收集的缺点

  • 因为GC发生在后台,所以对象的确切时间范围
    版本尚未确定。
  • 当 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

  • 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.

Disadvantages of Automatic Reference Counting

  • ARC cannot handle retain cycles automatically.

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.
皇甫轩 2024-12-18 20:12:28

ARC 与垃圾回收有何不同?

ARC 是垃圾收集的一种形式。

您可能的意思是“ARC 和跟踪垃圾收集(如 JVM 和 .NET)之间有什么区别?”。主要区别在于 ARC 速度较慢并且存在泄漏周期。这就是 JVM 和 .NET 都使用跟踪垃圾收集器的原因。有关更多信息,请阅读引用计数和跟踪垃圾收集如何进行比较?

How does ARC differ from garbage collection?

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?.

情独悲 2024-12-18 20:12:28

简短而甜蜜的答案如下:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文