When I moved to Objective C (iOS) from C++ (and little Java) I had hard time understanding memory management in iOS. But now all this seems natural and I know retain, autorelease, copy and release stuff. After reading about ARC, I am wondering is there more benefits of using ARC or it is just that you dont have to worry about memory management. Before moving to ARC I wanted to know how worth is moving to ARC.
- XCode has "Convert to Objective C ARC" menu. Is the conversion is that simple (nothing to worry about)?
- Does it help me in reducing my apps memory foot-print, memory leaks etc (somehow ?)
- Does it has much testing impact on my apps ?
- What are non-obvious advantages?
- Any Disadvantage of moving to it?
Here's my specific take on ARC:
It's simple. It works. Use it. As Kevin Low points out though, you will need to go through and fix up the bits where you use Core Foundation objects. That will just require a healthy lashing of
__bridge
or__bridge_transfer
though.Nope, not really. OK, sort of. It will help reduce memory leaks where you have coded incorrectly previously. It won't reduce memory footprint.
None whatsoever.
The future. There'll be more to come on the bonus that the compiler taking an intricate knowledge of how objects are reference counted gives. For example ARC provides the lovely
objc_retainAutoreleasedReturnValue
optimisation already, which is very nice.None whatsoever.
Please take my word for it and start using ARC. There's no reason (IMO) not to, thus the advantages definitely out-weigh the disadvantages!
For an in-depth look at how ARC works to perhaps help convince you that it's good, please take a look at my blog posts entitled "A look under ARC's hood" - here, here, here & here.
I discovered: ARC makes your code A LOT faster. In Apples WWDC video they say, that a couple of CPU cycles are saved for each NSObject's retain and release methods. This is because there is no need to check it on runtime anymore (It is outsourced to the compiler now). It's about 6 CPU cycles for each retain. If you use a loop which creates a lot of objects, then you can really feel the difference.
ARC not only makes the code faster, but YOU write less code, which accelerates your development process dramatically. And last but not least you must not search for memoryleaks by about 90% of your code. If you don't use a lot of low-level stuff, which must use "__bridge casts", you are COMPLETELY out of memory leaks.
Conclusion: If you can do it, DO IT!
If you're using Core Foundation or non-Objective-C code, then it's not as simple as you will have to manually go through your code and make sure all the casts between Objective-C and Core Foundation are bridged (if you have any casts). You'll also still have to manage memory for non-Objective-C code.
It's supposed to essentially take care of all memory leaks for you, since it automates the retain, release, copy, etc. So far, I've never had an Objective-C leak since switching to ARC.
No. Building might take a tad bit longer since it has to go through all your code and insert all the retain and release code.
Not sure if there are any. In the end, all ARC is, is an automator.
You will have to learn about bridged casts as well as you cannot build for anything lower than iOS 4.
In the end, it's definitely worth it. I was skeptical at first, but after watching the WWDC video where they explain how it works, I liked it more and more.
3) You should re-test your apps, but in my experience it will pretty much just work. Look at all the compiler warnings very carefully though!!!
4) Non obvious advantages: it's just really faster to code when you do not have to think about memory management. Perhaps that is obvious but the amount it helps still surprised me.
5) Disadvantages: Really the only disadvantage is having to turn off ARC for some third party libraries.
ARC has been so useful I simply will not code without it any longer. There's no reason to have to deal with all of that any more and it works well enough in practice.
Here's what you really need to know about ARC:
The compiler understands Objective-C and Cocoa better than you. I don't mean this as an insult; it understands it better than me. I think you could safely say it understands the rules better than all but maybe a dozen people worldwide. And it knows tricks to use them to a degree that you and I can't repeat, even if we understood as well as it does.
The rest is just details:
Have you read Apple's documentation on ARC? It answers a lot of the questions you're asking.
Based on my experience, here's what I think: