ARC,值得还是不值得?
ARC,值得还是不值得?
当我从C++(和一点Java)转向Objective C(iOS)时,我很难理解iOS中的内存管理。但现在这一切似乎很自然,我知道retain、autorelease、copy和release等操作。在了解了ARC之后,我想知道使用ARC是否有更多的好处,还是说你只是不用再担心内存管理的问题。在转向ARC之前,我想知道转向ARC是否值得。\n
- \n
- XCode有“转换为Objective C ARC”菜单。转换是否如此简单(没有什么需要担心的)?
- 它是否有助于减少我的应用程序的内存占用、内存泄漏等问题(某种程度上)?
- 它对我的应用程序有多大的测试影响?
- 有哪些非显而易见的优势?
- 转向它有什么不利之处?
\n
\n
\n
\n
\n
ARC, worth it or not?
Automatic Reference Counting (ARC) is a feature introduced by Apple to simplify memory management in Objective-C. It automates the retain, release, copy, etc., and is supposed to take care of memory leaks, making it easier for developers. However, there are some considerations and arguments surrounding the use of ARC.
One reason that the question of whether ARC is worth it or not arises is when developers are using Core Foundation or non-Objective-C code. In such cases, manual intervention is required to ensure proper memory management, including bridging casts between Objective-C and Core Foundation and managing memory for non-Objective-C code.
Another argument is whether ARC code performs better or not. Some developers argue that ARC code does extra work and may slow down the build time since it needs to insert retain and release code. However, others believe that ARC code can actually perform better due to optimizations that cannot be achieved manually.
When considering whether to use ARC, it is important to note that learning about bridged casts is necessary, and building for anything lower than iOS 4 is not possible. However, after understanding how ARC works and watching the WWDC video explaining its benefits, many developers find that it is worth using.
In conclusion, the question of whether ARC is worth it or not is subjective and depends on the specific use case and individual preferences. While there are arguments about the extra work and potential performance impact of ARC, many developers find that the benefits of automated memory management outweigh any potential drawbacks. Ultimately, it is recommended to test the app thoroughly after converting to ARC and make an informed decision based on the specific requirements and goals of the project.
自动引用计数(ARC)是一个用于自动管理内存的编译器特性。ARC的出现是为了减少Objective-C开发中的内存管理错误和繁琐的手动内存管理代码。编译器在理解Objective-C和Cocoa方面比开发者更加出色,因此它能够更好地处理内存管理。ARC通过一些技巧来自动处理内存管理,比开发者手动编写的内存管理代码更加高效和准确。
ARC的优势包括:
- 减少编写冗长且容易出错的内存管理代码的工作量。
- 比开发者更好地写出内存管理代码,即使开发者编写了理论上完美的内存管理代码。
- 在编译时和运行时的混合过程中,ARC可以使用一些开发者无法使用的技巧,例如减少内存使用和避免悬空指针带来的崩溃。
对于新的应用程序,直接使用ARC是一个明智的选择。然而,对于已存在的应用程序,需要重新测试以确保没有循环引用。如果应用程序目标为iOS 5之前的版本,则无法使用零弱引用。在这种情况下,开发者应考虑要求iOS 5或更高版本。
虽然ARC并非完全没有缺陷,但它仍然值得使用。最新版本的Xcode虽然可能仍然存在一些bug,但ARC的好处远远超过了这些问题。
总之,ARC的出现是为了简化Objective-C开发中的内存管理,并减少开发者错误。它通过编译器的优势和一些技巧来自动处理内存管理,使开发者可以更专注于业务逻辑而不必过多考虑内存管理问题。对于新应用程序,直接使用ARC是一个明智的选择,而对于已存在的应用程序,需要重新测试以确保没有循环引用,并考虑要求iOS 5或更高版本。尽管ARC可能存在一些问题,但它仍然值得使用。
ARC, worth it or not?
ARC (Automatic Reference Counting) is a feature in XCode that automatically manages the memory of Objective-C objects. It eliminates the need for manual memory management using retain and release statements. But is ARC worth it? Let's explore the reasons behind this question and find out the solution.
1) The first question is whether the conversion to ARC is simple and worry-free. The answer is yes, it is simple. XCode provides a "Convert to Objective C ARC" menu that handles the conversion for you. However, there might be some adjustments required for Core Foundation objects, which can be fixed using __bridge or __bridge_transfer.
2) The next concern is whether ARC helps in reducing memory footprint and memory leaks. The answer is not really. ARC does not reduce memory footprint but it does help in preventing memory leaks caused by incorrect coding. It ensures that objects are released when they are no longer needed.
3) Another question is whether ARC has a significant impact on app testing. The answer is no, it has no impact on testing. ARC is a compile-time feature and does not affect the behavior of the app during testing.
4) The non-obvious advantage of ARC is its optimization potential. The compiler, with its knowledge of object reference counting, can provide optimizations like the objc_retainAutoreleasedReturnValue. This optimization can improve performance by freeing memory immediately instead of waiting for the next run loop.
5) Finally, the question arises about any disadvantages of using ARC. The answer is none. There are no known disadvantages to using ARC. It is a reliable and efficient way to manage memory in Objective-C.
In conclusion, ARC is definitely worth using. It simplifies memory management, reduces the risk of memory leaks, and provides optimization opportunities. There are no significant drawbacks to using ARC, making it a valuable tool for iOS developers.
如果你想深入了解ARC的工作原理,可以阅读我的博客文章《A look under ARC's hood》。ARC确实可以减少内存占用,因为它可以更快地释放不再需要的对象。但这并不意味着它可以减少内存占用。如果你有相关的示例,请提供给我。
阅读LLVM文档中关于ARC的部分,可以了解它是如何优化的。它可以在函数返回结果时绕过自动释放池,立即释放内存,而不是等到下一次运行循环。如果你有一个循环中有多个这样的分配,你现在可以减少高潮时的内存使用量。
ARC不会对内存使用情况产生负面影响,甚至可能有所帮助。使用ARC可以简化内存管理,减少内存泄漏的风险,并提供优化机会。因此,没有理由不使用ARC,其优势绝对超过了劣势。