0

Since Apple introduced automatic reference counting back in 2011, never really thought about it, BUT if you see the ARC algorithm and according to apple documentation all objects gets destroyed after some time.

enter image description here

And if I see the release video from the WWDC 2011 you can see that this is not a garbage collector. so the question is, if it does uses a garbage collector algorithms and all objects get destroy in the end, why is not a garbage collector?

sorry if this has been ask before, but I'm really confuse about this matters and I need help to understand it well

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
  • 1
    I don't quite get your question, or exactly what your confused about... A garbage collector is a pice of software that runs usually periodically and releases object that are no longer referenced by the program. ARC just compiles in "retain"/"release" statements on compile time. So there is no software that runs to clean up memory under ARC there is why its not a garbage collector. – Peter Segerblom Feb 17 '14 at 11:47
  • That is what I thought as well, but a teacher really mess up my idea today. – Jorge Y. C. Rodriguez Feb 17 '14 at 11:49

4 Answers4

4

It's a bit of a philosophical question, but essentially, it's a compile-time garbage collector, as a opposed to a run-time garbage collector.

Instead of a garbage-collecting subsystem that runs together with the program, the manages the retain count and ensures that all the necessary releases and retains are put in the right place.

The practical upshot of this is that because it's done by the compiler, it's less error-prone than manual retain/release, and because it's done at compile-time, it's faster than a garbage collector.

This question should shed further light on the issue.

Community
  • 1
  • 1
Williham Totland
  • 28,471
  • 6
  • 52
  • 68
3

ARC has absolutely nothing to do with garbage collection algorithms. Automated Reference Counting (ARC) uses reference counting as its underlying mechanism for making decisions about reachability of an object. In contrast, garbage collection algorithms maintain a list of "root" objects (e.g. local variables, static variables, thread objects) and use these roots to traverse object graphs to check for reachability of an object. This process is non-deterministic, in the sense that you never know when the GC algorithm is going to run, and when objects that are no longer referenced would be garbage collected. All you know is that they eventually will be garbage collected, but "eventually" may mean a really long time.

Since GC uses graph traversal for reachability analysis, it does not care about cycles in a graph: if an object is reachable from some set of objects that are not in the root set, it is still considered garbage and would be collected. Therefore, you do not need to worry about retain cycles - a very real problem that you need to always keep in mind when working with reference counting systems.

Reference counting is much simpler than garbage collection: each object has a reference count shown in the diagram, retain increments it, and release decrements it. Once the count gets to zero, the object gets destroyed. That's really it! All that ARC does is automating the insertion of calls to retain and release. In fact, it is not possible to tell from this diagram alone if we are talking about the new ARC, or a pre-ARC memory management of Cocoa. Under a reference counting system you know exactly when your object is going to be released - it is going to happen as soon as its reference count reaches zero. Autoreleasing makes this a little less visible to you, because the last reference may be released outside of your code, but that does not make the process non-deterministic. Moreover, the language lets you take full control over autoreleasing by letting you make your own autorelease pools.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    @jycr753 I think that reference counting and garbage collection systems are different enough to be treated separately. They both serve a common goal of automating resource management, but that is where their similarities end. Calling one a variety of the other does a disservice to programmers who need to program with both (e.g. a developer for iOS coming from Android background, or vice verse). Thinking that the two are essentially the same would lead to tiny mistakes that end up being very costly because they are so hard to find. – Sergey Kalinichenko Feb 17 '14 at 12:40
1

I want to add that a GC typically reviews the whole object graph to find unreachable objects in circular references. In ARC – it is still RC – only the relationships of a object being dealloc'd are reviewed. If you have a circular reference, this will never be done, the participating objects will never be dealloc'd.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
1

Apparently I can't give an answer to comments yet (new account). However, what @daskblinkenlight describes is exactly what the the book Programming Language Concepts by Peter Sestoft defines as Garbage Collection by Reference Counting (Page 179). I don't really see the difference.

Moreover, I wrote an email to Peter Sestoft (Professor at ITU Copenhagen) asking him if ARC is a garbage collection and he gave the following answer (translated from danish): "In my opinion (and for instance Paul Wilsons survey "Uniprocessor garbage collection techniques") ARC is definitely a garbage collector."

Jorge Y. C. Rodriguez
  • 3,394
  • 5
  • 38
  • 61
sqdejan
  • 71
  • 1
  • 5
  • 1
    I do not have access to that book, so I cannot answer in detail. The problem is that the term "garbage collection" is not defined. In Objective-C context please keep in mind that there has been a "traditional" GC, bookkeeping of the references is done globally and asynchronously, while in ARC the bookkeeping is made locally and synchronously (= when an object is dealloc'd). If you want to use the term GC more in general, you can call it GC, too. It's a matter of definition. Anyway people working on GC tries to expand the definition. Maybe they want to include more modern technologies … – Amin Negm-Awad Feb 17 '14 at 15:26