Swift ARC Overview - Swift Programming

What is Swift - ARC Overview?

The memory management functions and its usage are handled in through the Swift language by Automatic reference counting (ARC). ARC is generally used to initialize and deinitialize the given system resources thereby it helps in releasing memory spaces which are used by the class instances when the instances are no longer required. ARC also keeps track of the information about the relationships between our code instances which helps in managing the memory resources effectively.

Functions of ARC

  • ARC allocates a chunk of memory for storing the information each and every time whenever a new class instance is created by init().
  • Information regarding the instance type and its values are stored in the memory.
  • Whenever the class instance is no longer needed it automatically frees the memory space by deinit() so as to further class instance storage and retrieval.
  • ARC always keeps in track of currently referring class instances properties, constants and variables so that deinit() is applied only to those unused instances.
  • ARC maintains a 'strong reference' to those class instance property, constants and variables to restrict deallocation when the class instance is currently in use.

ARC Program

When we run the above program using playground, we get the following result -

ARC Strong Reference Cycles Class Instances

When we run the above program using playground, we get the following result -

ARC Weak and Unowned References

Class type properties has two ways to resolve the strong reference cycles -

  • Weak References
  • Unowned References

These references are generally used to enable one instance such that it can refer to other instances in a reference cycle. Then these instances may refer to each and every instances instead of caring about the strong reference cycle. If the user knows that some instance may return 'nil' values we can point that using the weak reference. Whenever the instance is going to return something rather than nil value then declare it by using unowned reference.

Weak Reference Program

When we run the above program using playground, we get the following result -

Unowned Reference Program

When we run the above program using playground, we get the following result -

Strong Reference Cycles for Closures

Whenever we assign a closure to the class instance property and to the body of the closure to capture a particular instance then a strong reference cycle can occur. Strong reference to the closure can be defined by 'self.someProperty' or 'self.someMethod()'. Strong reference cycles are used as reference types for the closures.

When we run the above program using playground, we get the following result -

Weak and Unowned References

When the closure and the instance are used to refer to each other then the user may define the capture in a closure as an unowned reference. Then it would not allow the user to deallocate the instance at the same time. If the instance sometimes returns a 'nil' value define the closure using the weak instance.

When we run the above program using playground, we get the following result -

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Swift Programming Topics