Skip to content

Tip: Automatic Reference Counting

As defined on Wikipedia, Automatic Reference Counting (or ARC) is a “memory management enhancement where the burden of keeping track of an object’s reference count is lifted from the programmer to the compiler.”

With object-oriented programming, each new object you create takes up space in memory. This memory needs to be managed somehow. In the beginning, this management of memory was left entirely to the programmer, such as with C++ or originally with Objective-C.

Starting with Java, the concept of garbage collection became more popular. With garbage collection, the language run-time periodically purges objects from memory when they are no longer referenced. This is pretty easy on the programmer since they don’t have to worry about anything, but it can cause some significant performance problems for your app when the garbage collector is running.

An alternative that has became more popular in recent years is Automatic Reference Counting. You can think of ARC as the “just right” memory management strategy. With this technique, the language tracks references to objects and when an object’s reference count reaches 0, it is automatically and immediately removed from memory. This feature most recently generated a lot of interest when Objective-C added it a few years ago. You’re hearing more about it again as Swift also makes use of this memory management technique.

But ARC is old news for Xojo developers. Xojo has been using ARC since 1998 (v1.0)!

ARC really is a great way to handle object memory management. With ARC you generally don’t have to worry about memory management except for the special case of circular references. In those cases, you’ll need to manually release an object (set it to Nil) so that its reference count decreases to allow it to eventually reach 0, thus allowing it to get removed from memory. This special case does not occur often, but when it does you can make use of Weak References to help mitigate it.

Code Tip: Speed up SQLite with Write-Ahead Logging