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

问题描述

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

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

解决方案

正如我在,追踪垃圾回收可以挑选这些。



关于这个主题的一个很好的阅读来自,其中Chris Lattner有这样的说法:

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

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

08-03 17:33
查看更多