使用ARC有什么优点和缺点?

时间:2021-11-25 22:54:56

What are the advantages and disadvantages of using the new automatic reference counting (ARC) memory management style in an iOS project?

在iOS项目中使用新的自动引用计数(ARC)内存管理方式的优点和缺点是什么?

Can you choose not to use ARC when developing with the iOS 5.0 SDK?

在使用iOS 5.0 SDK开发时,是否可以选择不使用ARC ?

Do you recommend ARC or manual reference counting (MRC) for a new project?

对于新项目,你推荐使用ARC或manual reference counting (MRC)吗?

Will an application using ARC be able to run on older OS versions than iOS 5.0?

使用ARC的应用程序能在比iOS 5.0更老的OS版本上运行吗?

4 个解决方案

#1


60  

What are the advantages and disadvantages of using the new automatic reference counting (ARC) memory management style in an iOS project?

在iOS项目中使用新的自动引用计数(ARC)内存管理方式的优点和缺点是什么?

An ARC program's execution is nearly identical to well written MRC. That is, the behavioral differences are often undetectable because both the order of operations and performance are very close.

一个ARC程序的执行几乎等同于写好的MRC。也就是说,行为上的差异通常是无法察觉的,因为操作的顺序和性能都非常接近。

If you already know how to implement OS X or iOS apps with manual reference counting (MRC), ARC doesn't really add functionality -- it just allows you to remove reference counting operations from your sources.

如果您已经知道如何使用手动引用计数(MRC)实现OS X或iOS应用程序,那么ARC并没有真正添加功能——它只是允许您从源中删除引用计数操作。

If you don't want to learn MRC, then you may want to first try ARC. A lot of people struggle with, or try to ignore common practices of MRC (example: I've introduced a number of objc devs to the static analyzer). If you want to avoid those issues, ARC will allow you to postpone your understanding; you cannot write nontrivial objc programs without understanding reference counting and object lifetimes and relationships, whether MRC, ARC, or GC. ARC and GC simply remove the implementation from your sources and do the right thing in most cases. With ARC and GC, you will still need to give some guidance.

如果你不想学MRC,你可以先试试ARC。许多人都在与MRC的常见实践作斗争,或者试图忽略它们(例如:我已经向静态分析器引入了许多objc devs)。如果你想避免这些问题,ARC会让你推迟你的理解;如果不了解引用计数、对象生存期和关系(无论是MRC、ARC还是GC),就无法编写重要的objc程序。ARC和GC只是从源文件中删除实现,在大多数情况下做正确的事情。使用ARC和GC,您仍然需要提供一些指导。

I've not measured this, but it may be worth mentioning that compiling ARC sources would take more time and resources.

我还没有对此进行测量,但值得一提的是,编译ARC源代码将需要更多的时间和资源。

If the program you're developing has rather loose usage of reference counting (e.g. a typical amount of autoreleases), switching to ARC could really improve your program's execution times and peak memory usage.

如果您正在开发的程序对引用计数的使用相当松散(例如,一个典型数量的自动转发器),那么切换到ARC可以真正提高程序的执行时间和峰值内存使用。

Can you choose not to use ARC when developing with the iOS 5.0 SDK?

在使用iOS 5.0 SDK开发时,是否可以选择不使用ARC ?

Yes, using CLANG_ENABLE_OBJC_ARC. ARC is binary compatible, and all that really happens is that the compiler does its best to introduce the appropriate reference counting operations automatically for you, based on the declarations visible to the current translation (see my answer here as to why translation visibility is important). Therefore, you can also enable and disable it for some sources in a project and enable it for others.

是的,使用CLANG_ENABLE_OBJC_ARC。ARC是二进制兼容的,而真正发生的是编译器会根据当前翻译可见的声明自动地为您引入适当的引用计数操作(请参阅我在这里的回答,解释为什么翻译可见性很重要)。因此,您还可以在项目中启用并禁用它,并为其他资源启用它。

Mixed mode (some MRC and some ARC sources) is however quite complicated, and subtly, notably wrt implementations which may be duplicated by the compiler (e.g. an inline function's body may be incorrect). Such mixed mode issues will be very difficult to isolate. ObjC++ programs and sources will be particularly difficult in this regard. Furthermore, the behavior may differ based on your optimizations settings (as one example); a program which works perfectly in a debug build may introduce a leak or zombie in release.

混合模式(一些MRC和一些ARC源)非常复杂,而且非常微妙,特别是wrt实现,编译器可能会复制它(例如,一个内联函数的主体可能是不正确的)。这种混合模式的问题将很难加以孤立。ObjC+程序和资源在这方面会特别困难。此外,根据优化设置,行为可能会有所不同(如一个示例);在调试构建中运行良好的程序可能会在发行版中引入泄漏或僵尸。

Do you recommend ARC or manual reference counting (MRC) for a new project?

对于新项目,你推荐使用ARC或manual reference counting (MRC)吗?

Personally, I'll be sticking with MRC for some time. Even if ARC has been tested in real world usage, it's likely that there are a number issues remaining which show up in complex scenarios, which you will want to avoid being the first to know and debug. OS X's Garbage Collection is an example of why you may want to wait. As one example, the switch could alter when objects are destroyed -- your objects may be destroyed sooner and never be placed in autorelease pools. It could also change the order in which ivars are released, which could have some side effects.

就我个人而言,我将坚持MRC一段时间。即使ARC已经在现实世界的使用中进行了测试,很可能仍然存在一些在复杂场景中出现的问题,您将希望避免成为第一个知道和调试的问题。OS X的垃圾收集是您可能希望等待的一个例子。例如,当对象被破坏时,开关可能会改变——您的对象可能会被更快地销毁,并且永远不会被放置在autorelease池中。它还可以改变象牙释放的顺序,这可能会产生一些副作用。

I also have a large codebase that I don't want to lose a week testing this feature for at this time. Finally, backwards compatibility is still important for me.

我还有一个很大的代码基,我不想在这个时候浪费一个星期的时间来测试这个特性。最后,向后兼容性对我来说仍然很重要。

Will an application using ARC be able to run on older OS versions than iOS 5.0?

使用ARC的应用程序能在比iOS 5.0更老的OS版本上运行吗?

If you develop with MRC, it will be backwards compatible. If you develop with ARC, it will not necessarily be compatible. In fact, it may not even compile without a little extra work. The requirements for the runtime are available in some earlier versions. See also this question. If you need backwards compatibility, ARC will not be an option for some OS versions.

如果您使用MRC开发,它将向后兼容。如果您使用ARC开发,它不一定是兼容的。事实上,如果不做一点额外的工作,它甚至可能无法编译。运行时的需求在一些早期版本中是可用的。看到这个问题。如果您需要向后兼容,ARC将不会是一些OS版本的选项。

Lastly, if you were to limit the choice to GC or ARC, I'd recommend ARC.

最后,如果您将选择限制为GC或ARC,我推荐ARC。

#2


1  

you an turn it off/on with CLANG_ENABLE_OBJC_ARC = NO advantage is you have to write less code and memory management is easier. Disadvantage is that you have to scratch everything you learned about memory management :) I prefer turn it off.

您可以使用CLANG_ENABLE_OBJC_ARC =关闭它,您必须编写更少的代码,并且内存管理更容易。缺点是你必须把你学到的有关记忆管理的知识都擦掉。

#3


0  

You can turn on ARC through "Edit->Refactor->Convert to Objective C Arc", this will refactor your code entirely (get rid of all the memory management calls and such). There is no inverse operation so make sure you've got things under source control if you're having second thoughts. This post shows you how to disable it for specific files. I don't think there is too much argument to be made for not turning to it apart from the fact that it hurts to see all this effort put into good memory management go down the drain and that we will have to stop jumping to the ceiling every time we see init, new, copy without a corresponding release/autorelease (and that will take some getting used to). Perhaps it could be argued that in some circumstances manual memory management results in really noticeable performance/memory footprint improvements, if so I'd also be interested.

您可以通过“编辑->重构->转换为Objective C ARC”打开ARC,这将完全重构您的代码(删除所有内存管理调用等)。这里没有逆操作,所以如果你有其他的想法,请确保你在源代码控制下。这篇文章向您展示了如何为特定的文件禁用它。我不认为有太多的论点是不向它除了伤害看到所有这些努力投入好内存管理前功尽弃,我们不得不停止跳到天花板每次我们看到init,新的副本没有相应的发布/生成(这将需要一些时间来适应)。也许有人会说,在某些情况下,手动内存管理会带来显著的性能/内存占用改进,如果是这样的话,我也会感兴趣。

#4


0  

I am using Lion and xcode 4.3. I had the same problem.

我使用的是Lion和xcode 4.3。我也有同样的问题。

To fix it I turned the "Build Settings->Objective-C Automatic Reference Co" to "No".

为了修复它,我将“构建设置—>Objective-C自动引用Co”转换为“No”。

In order to see that it was set to "Yes" I had to also enable the "All" and "Levels" options on the toolbar that is just below the "Build Settings" toolbar.

为了查看它被设置为“Yes”,我还必须启用工具栏上的“All”和“level”选项,该选项位于“Build Settings”工具栏的下方。

Once those options were enabled I could see that my project had that option set to "Yes". It took me a while to figure that out as the default setting was "No", which is what displayed until I enabled the "Levels" option.

一旦启用了这些选项,我就可以看到我的项目将选项设置为“Yes”。我花了一段时间才发现,默认设置是“不”,这就是显示的内容,直到我启用了“level”选项。

#1


60  

What are the advantages and disadvantages of using the new automatic reference counting (ARC) memory management style in an iOS project?

在iOS项目中使用新的自动引用计数(ARC)内存管理方式的优点和缺点是什么?

An ARC program's execution is nearly identical to well written MRC. That is, the behavioral differences are often undetectable because both the order of operations and performance are very close.

一个ARC程序的执行几乎等同于写好的MRC。也就是说,行为上的差异通常是无法察觉的,因为操作的顺序和性能都非常接近。

If you already know how to implement OS X or iOS apps with manual reference counting (MRC), ARC doesn't really add functionality -- it just allows you to remove reference counting operations from your sources.

如果您已经知道如何使用手动引用计数(MRC)实现OS X或iOS应用程序,那么ARC并没有真正添加功能——它只是允许您从源中删除引用计数操作。

If you don't want to learn MRC, then you may want to first try ARC. A lot of people struggle with, or try to ignore common practices of MRC (example: I've introduced a number of objc devs to the static analyzer). If you want to avoid those issues, ARC will allow you to postpone your understanding; you cannot write nontrivial objc programs without understanding reference counting and object lifetimes and relationships, whether MRC, ARC, or GC. ARC and GC simply remove the implementation from your sources and do the right thing in most cases. With ARC and GC, you will still need to give some guidance.

如果你不想学MRC,你可以先试试ARC。许多人都在与MRC的常见实践作斗争,或者试图忽略它们(例如:我已经向静态分析器引入了许多objc devs)。如果你想避免这些问题,ARC会让你推迟你的理解;如果不了解引用计数、对象生存期和关系(无论是MRC、ARC还是GC),就无法编写重要的objc程序。ARC和GC只是从源文件中删除实现,在大多数情况下做正确的事情。使用ARC和GC,您仍然需要提供一些指导。

I've not measured this, but it may be worth mentioning that compiling ARC sources would take more time and resources.

我还没有对此进行测量,但值得一提的是,编译ARC源代码将需要更多的时间和资源。

If the program you're developing has rather loose usage of reference counting (e.g. a typical amount of autoreleases), switching to ARC could really improve your program's execution times and peak memory usage.

如果您正在开发的程序对引用计数的使用相当松散(例如,一个典型数量的自动转发器),那么切换到ARC可以真正提高程序的执行时间和峰值内存使用。

Can you choose not to use ARC when developing with the iOS 5.0 SDK?

在使用iOS 5.0 SDK开发时,是否可以选择不使用ARC ?

Yes, using CLANG_ENABLE_OBJC_ARC. ARC is binary compatible, and all that really happens is that the compiler does its best to introduce the appropriate reference counting operations automatically for you, based on the declarations visible to the current translation (see my answer here as to why translation visibility is important). Therefore, you can also enable and disable it for some sources in a project and enable it for others.

是的,使用CLANG_ENABLE_OBJC_ARC。ARC是二进制兼容的,而真正发生的是编译器会根据当前翻译可见的声明自动地为您引入适当的引用计数操作(请参阅我在这里的回答,解释为什么翻译可见性很重要)。因此,您还可以在项目中启用并禁用它,并为其他资源启用它。

Mixed mode (some MRC and some ARC sources) is however quite complicated, and subtly, notably wrt implementations which may be duplicated by the compiler (e.g. an inline function's body may be incorrect). Such mixed mode issues will be very difficult to isolate. ObjC++ programs and sources will be particularly difficult in this regard. Furthermore, the behavior may differ based on your optimizations settings (as one example); a program which works perfectly in a debug build may introduce a leak or zombie in release.

混合模式(一些MRC和一些ARC源)非常复杂,而且非常微妙,特别是wrt实现,编译器可能会复制它(例如,一个内联函数的主体可能是不正确的)。这种混合模式的问题将很难加以孤立。ObjC+程序和资源在这方面会特别困难。此外,根据优化设置,行为可能会有所不同(如一个示例);在调试构建中运行良好的程序可能会在发行版中引入泄漏或僵尸。

Do you recommend ARC or manual reference counting (MRC) for a new project?

对于新项目,你推荐使用ARC或manual reference counting (MRC)吗?

Personally, I'll be sticking with MRC for some time. Even if ARC has been tested in real world usage, it's likely that there are a number issues remaining which show up in complex scenarios, which you will want to avoid being the first to know and debug. OS X's Garbage Collection is an example of why you may want to wait. As one example, the switch could alter when objects are destroyed -- your objects may be destroyed sooner and never be placed in autorelease pools. It could also change the order in which ivars are released, which could have some side effects.

就我个人而言,我将坚持MRC一段时间。即使ARC已经在现实世界的使用中进行了测试,很可能仍然存在一些在复杂场景中出现的问题,您将希望避免成为第一个知道和调试的问题。OS X的垃圾收集是您可能希望等待的一个例子。例如,当对象被破坏时,开关可能会改变——您的对象可能会被更快地销毁,并且永远不会被放置在autorelease池中。它还可以改变象牙释放的顺序,这可能会产生一些副作用。

I also have a large codebase that I don't want to lose a week testing this feature for at this time. Finally, backwards compatibility is still important for me.

我还有一个很大的代码基,我不想在这个时候浪费一个星期的时间来测试这个特性。最后,向后兼容性对我来说仍然很重要。

Will an application using ARC be able to run on older OS versions than iOS 5.0?

使用ARC的应用程序能在比iOS 5.0更老的OS版本上运行吗?

If you develop with MRC, it will be backwards compatible. If you develop with ARC, it will not necessarily be compatible. In fact, it may not even compile without a little extra work. The requirements for the runtime are available in some earlier versions. See also this question. If you need backwards compatibility, ARC will not be an option for some OS versions.

如果您使用MRC开发,它将向后兼容。如果您使用ARC开发,它不一定是兼容的。事实上,如果不做一点额外的工作,它甚至可能无法编译。运行时的需求在一些早期版本中是可用的。看到这个问题。如果您需要向后兼容,ARC将不会是一些OS版本的选项。

Lastly, if you were to limit the choice to GC or ARC, I'd recommend ARC.

最后,如果您将选择限制为GC或ARC,我推荐ARC。

#2


1  

you an turn it off/on with CLANG_ENABLE_OBJC_ARC = NO advantage is you have to write less code and memory management is easier. Disadvantage is that you have to scratch everything you learned about memory management :) I prefer turn it off.

您可以使用CLANG_ENABLE_OBJC_ARC =关闭它,您必须编写更少的代码,并且内存管理更容易。缺点是你必须把你学到的有关记忆管理的知识都擦掉。

#3


0  

You can turn on ARC through "Edit->Refactor->Convert to Objective C Arc", this will refactor your code entirely (get rid of all the memory management calls and such). There is no inverse operation so make sure you've got things under source control if you're having second thoughts. This post shows you how to disable it for specific files. I don't think there is too much argument to be made for not turning to it apart from the fact that it hurts to see all this effort put into good memory management go down the drain and that we will have to stop jumping to the ceiling every time we see init, new, copy without a corresponding release/autorelease (and that will take some getting used to). Perhaps it could be argued that in some circumstances manual memory management results in really noticeable performance/memory footprint improvements, if so I'd also be interested.

您可以通过“编辑->重构->转换为Objective C ARC”打开ARC,这将完全重构您的代码(删除所有内存管理调用等)。这里没有逆操作,所以如果你有其他的想法,请确保你在源代码控制下。这篇文章向您展示了如何为特定的文件禁用它。我不认为有太多的论点是不向它除了伤害看到所有这些努力投入好内存管理前功尽弃,我们不得不停止跳到天花板每次我们看到init,新的副本没有相应的发布/生成(这将需要一些时间来适应)。也许有人会说,在某些情况下,手动内存管理会带来显著的性能/内存占用改进,如果是这样的话,我也会感兴趣。

#4


0  

I am using Lion and xcode 4.3. I had the same problem.

我使用的是Lion和xcode 4.3。我也有同样的问题。

To fix it I turned the "Build Settings->Objective-C Automatic Reference Co" to "No".

为了修复它,我将“构建设置—>Objective-C自动引用Co”转换为“No”。

In order to see that it was set to "Yes" I had to also enable the "All" and "Levels" options on the toolbar that is just below the "Build Settings" toolbar.

为了查看它被设置为“Yes”,我还必须启用工具栏上的“All”和“level”选项,该选项位于“Build Settings”工具栏的下方。

Once those options were enabled I could see that my project had that option set to "Yes". It took me a while to figure that out as the default setting was "No", which is what displayed until I enabled the "Levels" option.

一旦启用了这些选项,我就可以看到我的项目将选项设置为“Yes”。我花了一段时间才发现,默认设置是“不”,这就是显示的内容,直到我启用了“level”选项。