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

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

问题描述

有人可以简单地向我解释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.

但是,这并不能完全免除你对这些平台上的内存管理的担心。我在我的回答中描述了要注意(保留周期)的主要问题这里,这可能需要一点思考你的一部分,以标记弱指针。

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.

至于这是否可以扩展到其他语言,它似乎围绕引用计数系统。它可能很难应用到Java或其他语言,但我不知道有关低级编译器详细信息,以制定一个确定的语句。考虑到苹果是在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天全站免登陆