Open In App

Swift – Deinitialization and How its Works?

Improve
Improve
Like Article
Like
Save
Share
Report

Deinitialization is the process to deallocate memory space that means when we finished our work with any memory then we will remove our pointer that is pointing to that memory so that the memory can be used for other purposes. In other words, it is the cycle to let loose unused space which was involved by unused class case objects for a better memory of the executives. They are generally accessible only with class types so we can easily utilize deinitializers and tidy up unused class occurrence objects. A class can have almost one deinitializer in it and it does not take any parameter. A deinitialization process which immediately called before a class instance is deallocated. We can start deinitialization with the deinit keyword. deinit is the predefined keyword to deallocate memory in Swift. Inside deinit, we can write our code.

Syntax:

deinit {

    // deinitialization action

}

Working of Deinitialization

Swift automatically frees up the used resources by deallocating our instances when they’re not required anymore. It handles the memory of the executives of occasions through programmed reference counting or ARC(also known as Automatic Reference Counting). We don’t have to play out any manual cleanup when our cases are deallocated. Quick naturally deallocates our occasions when they’re not generally expected to let loose memory assets. Be that as it may, when we are working with our own assets, we may have to play out some extra cleanup. In Swift, we don’t have to call deinitializers physically those will call naturally not long before the occurrence of deallocation. Assuming we characterize a deinitializer in a class it can get to every one of the properties and can change the conduct of properties. The class object case will not deallocate until the characterized deinitializer is called.

Deinitializers are called naturally, not long before occasion deallocation happens. Superclass deinitializers are inherited by their own subclasses and at the end of the subclass deinitializer implementation, the superclass deinitializer is called automatically. The deinitializers of the superclass are always called even if a subclass doesn’t own its deinitializer. If a situation occurs in which space does not deallocate even if its deinitializer is called, then the deinitializer gets to the properties of the occurrence it approaches and alters himself according to those properties to deallocate memory.

Example 1: In the example below we have initialized a variable with 10 the latter increment it by 10 during class initialization. After that, we have printed its value and then we pointed our class object to null and again printed the value of the variable. When we first printed the value of the variable we get 20 as an output but after deinitialization, we get 0 as an output. 

Swift




// Swift program to illustrate deinitialization 
// Initialize a variable x with value 10
var x = 10
  
// Create a class
class deinitGfg 
{
  
    // Initialize
    init() 
    {
        x = x + 10
    }
      
    // Deinitialization  x to 0
    deinit 
    {
        x = 0
    }
}
  
// Created an instance of class 
var obj: deinitGfg? = deinitGfg()
  
print("Value of x before deinit = \(x)")
obj = nil
print("Value of x after deinit = \(x)")


Output:

Value of x before deinit = 20
Value of x after deinit = 0

Example 2: In this example, We have created a deinitializer inside the Gfg class. Then, we have created an instance of the Gfg class and assigned it to a Gfg type variable named ob. then we assign nil to ob and it will deallocate the memory.

Swift




// Swift program to illustrate deinitialization 
  
// Create a class
class Gfg 
{
    var x: Int
      
    // define initializer
    init() 
    {
        x = 5
        print("Memory Initialized for variable x")
    }
      
    // define deinitializer
    deinit 
    {
        print("Memory Deinitialized")
    }
}
  
// Create an instance of class
var ob: Gfg? = Gfg()
  
// Deallocate object
ob = nil


Output:

Memory Initialized for variable x
Memory Deinitialized


Last Updated : 31 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads