Swift – Deinitialization and How its Works?
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.
// 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.
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.
Memory Initialized for variable x Memory Deinitialized