Writers logo

What is ARC?

Mastering Memory Management: Understanding Automatic Reference Counting (ARC)

By Roman BurdiuzhaPublished 2 years ago β€’ 2 min read

Hello, my dear friends. This is your cloud architect, CTO & founder of Gart Solutions - Roman Burdiuzha.

πŸ€” ARC, or Automatic Reference Counting, is a memory management system. It automatically tracks and manages the lifecycle of objects in memory, freeing the developer from the need to manually increase or decrease the reference count of objects.

In programming languages, objects remain in memory as long as there are active references to them. When the reference count of an object drops to zero, it means the object is no longer in use, and the system can free the memory it occupies.

πŸ”£ How it works:

πŸ”£ Increasing the reference count: Every time you create a new strong reference to an object (e.g., by assigning the object to a variable or property), ARC automatically increases the reference count of that object.

πŸ”£ Decreasing the reference count: When a strong reference to an object is destroyed (e.g., when a variable goes out of scope or is assigned nil), ARC decreases the reference count of the object.

πŸ”£ Releasing memory: When the reference count of an object reaches zero, ARC automatically frees the memory occupied by that object.

πŸ”£ ARC and managing cyclic references:

One issue that needs to be addressed when using ARC is the possibility of cyclic references, where two objects refer to each other with strong references. This can cause the reference count of both objects to never reach zero, and the memory occupied by these objects will not be freed.

πŸ€“ Example:

class MyClassA {

var classBReference: MyClassB?

}

class MyClassB {

var classAReference: MyClassA?

}

func main() {

let aClass = MyClassA()

let bClass = MyClassB()

aClass.classBReference = bClass

bClass.classAReference = aClass

}

To solve this problem, two types of references that do not increase the reference count are proposed:

βž– weak: Weak references do not increase the reference count and automatically become nil when the object is destroyed. They are usually used to prevent cyclic references when objects can be destroyed at any moment.

βž– unowned: Unowned references are similar to weak references but assume that the other object will have the same or longer lifetime. Unowned references do not become nil and can lead to runtime errors if you try to access an object that has already been freed.

πŸ€“ Example:

class MyClassA {

weak var classBReference: MyClassB?

}

class MyClassB {

unowned var classAReference: MyClassA

init(classAReference: MyClassA) {

self.classAReference = classAReference

}

}

func main() {

var aClass = MyClassA()

var bClass = MyClassB(classAReference: aClass)

aClass.classBReference = bClass

}

#️⃣ ARC greatly simplifies memory management in applications by automating most of the process, but it requires developers to understand how strong, weak, and unowned references work to prevent memory leaks and runtime errors.

Guides

About the Creator

Roman Burdiuzha

Cloud Architect | Co-Founder & CTO at Gart Solutions | DevOps & Cloud Solutions | Boosting your business performance through result-oriented tough DevOps practices

https://gartsolutions.com/

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    Β© 2026 Creatd, Inc. All Rights Reserved.