Memory Management in Swift
Posted By : Varun Wadhwa | 26-Dec-2017
In this blog, I’ll explain how Memory is managed in Swift. In Swift ARC (Automatic Reference Count) is used to manage the memory. ARC allocates some memory to store the info. when a new object is created by init(). Then the info. About the instance is stored in the memory (that was allocated by ARC).
ARC keeps track of variables which referring the instance by maintaining a reference count. When there’re no more references to the instance the ARC free up the memory by deinit.
Example :
class Demo {
init() {
print("inside init")
}
deinit {
print("inside deinit")
}
}
var demoVar1 : Demo?
demoVar1 = Demo(); // strong reference 1
var demoVar2 = demoVar1 // strong reference 2
// There’re two strong reference
demoVar1 = nil; // reference count reduces by 1
demoVar2 = nil; // now there’s no strong reference , so deinit (i.e prints inside deinit ) will be called.
Retain Cycle : There can be situations where ARC failed to free up the memory and cause Memory leaks, such situation causes when there is retain cycle.
Example :
class Demo1 {
var demoVar : Demo2?
init(){
print("inside init of Demo1")
}
deinit {
print("inside deinit of Demo1")
}
}
class Demo2 {
var demoVar : Demo1?
init() {
print("inside init of Demo2");
}
deinit {
print("inside deinit of Demo2")
}
}
var demo2Class : Demo2? = Demo2() //one strong reference to Demo2
var demo1Class : Demo1? = Demo1() // one strong reference to Demo1
demo2Class!.demoVar = demo1Class // total 2 strong reference to Demo1
demo1Class!.demoVar = demo2Class // total 2 strong reference to Demo2
// setting variable to nil
demo2Class = nil // one strong reference reduced for Demo2
demo1Class = nil // one strong reference reduced for Demo1
But still there’s one strong reference to each instance so ARC will not deallocate the memory in this situation and there’ll be a memory leak.
Weak References : In order to avoid situations like retain cycle, we use weak keyword so that no there’s no strong reference therefore no increase in reference count and hence no memory leak.
Example : In above example if we use weak, the deinit block of both classes will be called.
weak var demoVar : Demo2?
Unowned References : Unowned is same as weak except It can’t be nil, so It can’t be declared by using optionals. We use unowned in case when we’re sure that variable can’t be accessed after instance deallocation. Memory leaks can also be detected using Instruments.
Thanks,
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Varun Wadhwa
Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.