With last version of iOS Apple has implemented Automatic Reference Counting for Objective-C, but I don't understand as works.
-
1Did you try [looking at the documentation](http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/_index.html)? Or [the other documentation](http://www.google.com/url?sa=t&source=web&cd=1&ved=0CCQQFjAA&url=http%3A%2F%2Fclang.llvm.org%2Fdocs%2FAutomaticReferenceCounting.html&ei=1iaXTriaLcnPiALP_azDDQ&usg=AFQjCNHBcQAROrz81zpnzFhm8tW7ApG47g)? – jscs Oct 13 '11 at 17:59
-
Or how about just doing a search here: http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work – jscs Oct 13 '11 at 17:59
-
Thank you for your help, but I wanted only a clear informations without read many pages of documentation... otherwise there would be no reason to ask questions on stackoverflow... – ADIMO Oct 13 '11 at 21:02
-
Stack Overflow expects you to do some research on your own before posting here. It is a resource for when you have already tried to solve your problem yourself. In addition, as I pointed out, there _already exist_ questions here which you could have read. – jscs Oct 13 '11 at 21:29
-
See also [What is the difference between Objective-C automatic reference counting and garbage collection?](http://stackoverflow.com/questions/7874342/what-is-the-difference-between-objective-c-automatic-reference-counting-and-garb) – Brad Larson Jan 16 '12 at 23:44
2 Answers
Automatic reference counting inserts retain
and release
messages into your code for you at compile-time, following the normal conventions. So it's exactly as if you did the memory management yourself manually, except that the compiler is smart enough to be able to write that bit for you, and much less likely to make a mistake.
So it's not garbage collection, it's more like a (very simple) form of static analysis. And you still get overwhelmingly deterministic memory management and little overall change in runtime costs, as per the caveats raised by Catfish_Man below.

- 99,986
- 12
- 185
- 204
-
3No change in runtime costs isn't exactly accurate. The compiler has to be significantly more conservative than a human would be about object lifetime, typically resulting in about 20% more refcount churn. On the other hand, ARC has some neat tricks it does to avoid autoreleasing in many cases, which can lower memory usage and improve performance. Whether it's a win or a loss performance-wise depends on the exact code in question. – Catfish_Man Oct 13 '11 at 15:15
-
Good point. I've toned down my answer and made an explicit reference to your comment. – Tommy Oct 13 '11 at 15:31
-
@Tommy: You say "100% deterministic" but the docs say "ARC is permitted to re-order and eliminate operations in a manner which may alter the overall computation history". http://clang.llvm.org/docs/AutomaticReferenceCounting.html#optimization – J D Jan 25 '14 at 15:22
-
@JonHarrop you're clearly in the right so I've softened my language. What I was trying to communicate was the difference between things being deallocated proximately to the disappearance of their final reference and things hanging around until the next garbage sweep. Obviously from there I made an inaccurate claim. – Tommy Jan 25 '14 at 19:38
-
@Tommy: "hanging around until the next garbage sweep". Most garbage collectors are generational so most garbage only "hangs around" inside the nursery generation until it is overwritten by a new object. That is much the same as a deallocated object hanging around in the memory allocator until it is overwritten by a new object. – J D Jan 26 '14 at 12:10
-
@JanHarrop an object that is a candidate for garbage collection is not destroyed until the garbage collector finds it. That's why e.g. C# has the special `IDisposible` interface for dealing with external resources; Java has a convention which relies on explicitly closing external resources. A `dealloc`d object has already been destroyed and just happens to leave a trace in memory. So the former is a real difference in object semantics, the latter just a figment of the implementation. – Tommy Jan 26 '14 at 19:45
Automatic Reference Counting implements automatic memory management for Objective-C objects and blocks, freeing the programmer from the need explicitly insert retains and releases. It does not provide a cycle collector; users must explicitly manage lifetime instead.
Read this spec - Automatic Reference Counting

- 29,669
- 15
- 106
- 125