新的自动引用计数机制是如何工作的? [英] How does the new automatic reference counting mechanism work?

查看:34
本文介绍了新的自动引用计数机制是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我简要解释一下 ARC 的工作原理吗?我知道它与垃圾收集不同,但我只是想知道它究竟是如何工作的.

Can someone briefly explain to me how ARC works? I know it's different from Garbage Collection, but I was just wondering exactly how it worked.

另外,如果 ARC 在不影响性能的情况下做 GC 所做的事情,那么 Java 为什么要使用 GC?为什么它不也使用 ARC?

Also, if ARC does what GC does without hindering performance, then why does Java use GC? Why doesn't it use ARC as well?

推荐答案

每个使用 Objective-C 的新开发人员都必须学习何时保留、释放和自动释放对象的严格规则.这些规则甚至指定了暗示从方法返回的对象的保留计数的命名约定.一旦您牢记这些规则并始终如一地应用它们,Objective-C 中的内存管理就会成为您的第二天性,但即使是最有经验的 Cocoa 开发人员也不时会犯错.

Every new developer who comes to Objective-C has to learn the rigid rules of when to retain, release, and autorelease objects. These rules even specify naming conventions that imply the retain count of objects returned from methods. Memory management in Objective-C becomes second nature once you take these rules to heart and apply them consistently, but even the most experienced Cocoa developers slip up from time to time.

借助 Clang 静态分析器,LLVM 开发人员意识到这些规则足够可靠,因此他们可以构建一个工具来指出代码所采用路径中的内存泄漏和过度释放.

With the Clang Static Analyzer, the LLVM developers realized that these rules were reliable enough that they could build a tool to point out memory leaks and overreleases within the paths that your code takes.

自动引用计数 (ARC) 是下一个合乎逻辑的步骤.如果编译器可以识别您应该保留和释放对象的位置,为什么不让它为您插入该代码?严格的、重复的任务是编译器和他们的兄弟们擅长的.人类会忘记事情并犯错,但计算机的一致性要高得多.

Automatic reference counting (ARC) is the next logical step. If the compiler can recognize where you should be retaining and releasing objects, why not have it insert that code for you? Rigid, repetitive tasks are what compilers and their brethren are great at. Humans forget things and make mistakes, but computers are much more consistent.

然而,这并不能让您完全不用担心这些平台上的内存管理.我在回答中描述了需要注意的主要问题(保留周期)此处,这可能需要您稍微考虑一下以标记弱指针.但是,与您在 ARC 中获得的相比,这微不足道.

However, this doesn't completely free you from worrying about memory management on these platforms. I describe the primary issue to watch out for (retain cycles) in my answer here, which may require a little thought on your part to mark weak pointers. However, that's minor when compared to what you're gaining in ARC.

与手动内存管理和垃圾收集相比,ARC 为您提供了两全其美的方法,它无需编写保留/释放代码,又没有在垃圾收集环境中看到的暂停和锯齿状内存配置文件.关于垃圾收集的唯一优势是它处理保留循环的能力以及原子属性分配成本低廉的事实(如此处).我知道我正在用 ARC 实现替换所有现有的 Mac GC 代码.

When compared to manual memory management and garbage collection, ARC gives you the best of both worlds by cutting out the need to write retain / release code, yet not having the halting and sawtooth memory profiles seen in a garbage collected environment. About the only advantages garbage collection has over this are its ability to deal with retain cycles and the fact that atomic property assignments are inexpensive (as discussed here). I know I'm replacing all of my existing Mac GC code with ARC implementations.

至于这是否可以扩展到其他语言,它似乎是针对 Objective-C 中的引用计数系统.将其应用于 Java 或其他语言可能很困难,但我对低级编译器的详细信息了解不足,无法在那里做出明确的声明.鉴于 Apple 是在 LLVM 中推动这项工作的一方,Objective-C 将排在第一位,除非另一方为此投入大量资源.

As to whether this could be extended to other languages, it seems geared around the reference counting system in Objective-C. It might be difficult to apply this to Java or other languages, but I don't know enough about the low-level compiler details to make a definitive statement there. Given that Apple is the one pushing this effort in LLVM, Objective-C will come first unless another party commits significant resources of their own to this.

这个在 WWDC 上的揭幕震惊了开发者,所以人们没有意识到可以做这样的事情.随着时间的推移,它可能会出现在其他平台上,但目前它是 LLVM 和 Objective-C 独有的.

The unveiling of this shocked developers at WWDC, so people weren't aware that something like this could be done. It may appear on other platforms over time, but for now it's exclusive to LLVM and Objective-C.

这篇关于新的自动引用计数机制是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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