Retain Cycles

Retain Cycles are an aspect (and possibly the one major downside compared to GC) of Automatic Reference Counting. Thyey happen when two (more more) objects strongly reference each other in a circular fashion, causing an infinite loop and keeping each other from being released, even though nothing else may be referencing the interlinked objects externally anymore.

A common example is that of two classes in a parent/child relationship. If both the parent (for example a collection) references its children, and the child objects each reference the parent, the reference count for neither will ever go to zero, even when all outside references to the parent and children have been released.

The Elements compiler, just as Objective-C and Apple's Swift implementation, introduces Storage Modifiers to allow your code to deal with and avoid retain cycles. In the above example it would be common practice to mark the reference from the child class back to its parent with the weak (Oxygene and Swift) or __weak (C#) keyword.

The Profiling with Instruments article gives a more elaborate example for this.

See Also