Garbage Collection vs. Automatic Reference Counting

Kaninchen jäger
The Startup
Published in
4 min readFeb 11, 2018
Photo by Gary Chan on Unsplash

In the previous blog post, we saw how iOS uses Automatic Reference Counting (ARC) as its object lifetime management system. In this post I try to point out some of the significant differences ARC has compare to a garbage collection or more specifically tracing garbage collection (to be more precise reference counting and tracing are the two different implementation of the garbage collection. In other words ARC is a form of garbage collection. Since tracing garbage collection is the most common type of garbage collection, usually the term “garbage collection” refers to tracing garbage collection).

Automatic Reference Counting

ARC is a compiler feature uses object-ownership. Instead of looking for unused objects in the background during runtime, ARC releases the objects from the memory if the object has reference count of zero i.e. it has no owner anymore. ARC automatically injects Objective-C runtime equivalents for retain, release (the compiler inserts retains and releases in all the places you should have) at compile time. I have an entire post explaining how ARC works.

Advantages of Automatic Reference Counting

  • Destruction of the objects is real-time and predictable. It has deterministic reclamation of objects (when the last strong reference to the object goes away) while GC frees an object “sometime later”. Objects are released as soon as they can no longer be referenced without long pauses for collection cycles. This makes the objects to be released sooner. It is important for the systems with limited memory such as mobile devices.
    This defines away a class of subtle bugs that can exist in GC apps that aren’t exposed because the collector doesn’t trigger “in the buggy window”.
  • No background processing, which makes it more efficient again for lower-power systems.
  • Tracing garbage collection cycles are triggered too often if the set of live objects fills most of the available memory, it requires extra space to be efficient. Reference counting performance does not deteriorate as the total amount of free space decreases.
  • Asymptotic complexity of reference counting does not depend upon the total size of the heap.

Disadvantages of Automatic Reference Counting

  • Cannot collect retain cycles. Retain cycles keep reference counts above zero even when they are unreachable
  • Multithreaded reference counting need to be considered.

Garbage Collection

Garbage Collection (GC) is a form of automatic memory management attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Prior to existence of any garbage collection you had to manually add calls to retain and release (in other languages “free” or “destroy”).

There are several strategies to implement a garbage collection, the most common two are tracing, and reference counting. Tracing garbage collection is used by the .NET and Java platforms.

Garbage collection acts at run-time. It detects unused objects and it releases them once no part of your code is using them anymore. This happens at indeterminate intervals (either after a certain amount of time has passed, or when the runtime sees available memory getting low), so objects are not necessarily released at the exact moment they are no longer been used.

Advantages of Garbage Collection

  • GC can clean up entire object graphs, including retain cycles. This is the primary advantage of the garbage collection over ARC that you don’t need to be worry about retain cycles.
  • GC happens in the background, so less memory management work is done as part of the regular application flow.

Disadvantages of Garbage Collection

  • Because GC happens in the background, the exact time frame for object releases is undetermined.
  • When a GC happens, other threads in the application may be temporarily put on hold.

Further Study

ARC vs. GC
A thread on Apple’s Objective-C mailing list
A brief history of garbage collection

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by 295,232+ people.

Subscribe to receive our top stories here.

--

--