Press ESC to close

Swift ARC

Hello friends, in this article we will talk about how to manage memory in Swift. How to manage memory is a very important issue in mobile programming. Despite the increase in RAM in iOS devices day by day, the importance of memory management is increasing day by day with the increase in the amount of RAM used with the support that should be given to old devices and with different technologies developing day by day.

In the applications we developed with Swift, ARC (Automatic Reference Counter) automatically deletes the unused objects from the memory. Of course, in order to do this, we need to define the variables accordingly. Otherwise, a Memory Leak will occur that we do not want at all. If we briefly define Memory Leak; unused and unused objects occupy space in memory.

When creating variables, remember that ARC counts references for created variables. Create your variables accordingly. Of course, this situation is not the same for every object. As you know, variables are actually of 2 types. They are of reference type and value type. They have many different features such as places where they are kept in memory. You can find a more detailed article here. ARC only counts variables of reference type.

When we create a reference type variable, its reference number is 1. If we assign this variable to a variable on another page, the reference number of the variable we created will be 2. Until this number is 0, the object we created will always occupy space in the memory. Here we can define strong, weak and unowned types while defining variables to prevent this.

Strong

It comes by default when we create a reference type variable. Increases the reference count by 1.

Weak

If a reference type variable is declared weak, the reference number is not incremented. Thanks to this, if the class it is in is deleted from memory, the variable is also deleted.

Unowned

Pretty much the same as Weak. The difference between them is that an object defined as owned cannot be set to nil. When we create this variable, we must set a value.

 

You can use Swift’s own documentation for more detailed information. If you have questions, you can reach us by sending an e-mail or comment. Good works.

Leave a Reply

Your email address will not be published. Required fields are marked *