Memory leaks in iOS occurs when the allocated amount of memory can not be deallocate due to retain cycles.
Swift uses the Automatic Reference Counting to manage the memory leaks.
Memory leaks increase the memory footprint incrementally in the app.
Memory leaks cause a crash in the application.
In the general way memory leaks occur when two objects strongly hold the reference to each other.
ARC maintains the reference count when any variable deallocate the memory the reference count will 0.
Memory leaks make the application heavy and slow.
Memory leaks may occurs due to third-party SDK, framework, and wrong variable declaration.
Automatic Refrence Counting
Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases,
this means that memory management “just works” in Swift, and you don’t need to think about memory
management yourself.
Arc works when you create any instance of the class and some memory occupied by Arc to store the information.
And when the instance is no longer needed then the ARC deallocates the memory and sets the reference count 0.
Example of memory leak
We will generate the memory leaks using two dependent strong objects.
Taking two classes and both contain the instance of each other with a strong reference.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class WebkulServer { var clientObject: [WebkulClient] = [] //Strong refrence object func add(clientObject: WebkulClient) { self.clientObject.append(clientObject) } } class WebkulClient { var serverObject: WebkulServer //Strong refrence object init (webkulServer: WebkulServer) { self.serverObject = webkulServer self.serverObject.add(clientObject: self) // memory leak } } |
In WebkulServer class have webkulClient object with strong refrence.
And in WebkulClient class have a WebkulServer object with strong reference.
Both object never releases the memory because both never be nil.
And both will create the cycle which will call the memory leaks.
So for avoiding memory leaks we need to use one object a weak or unowned.
Below in screenshots, the red color cross icon shows the leaks.
Weak
A weak` reference does not increase the retain count. a weak reference does not take ownership of an object.
Unowned
The unowned reference does not increase the retain count. And unowned reference never be nil.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class WebkulServer { var clientObject: [WebkulClient] = [] //Strong refrence object func add(clientObject: WebkulClient) { self.clientObject.append(clientObject) } } class WebkulClient { weak var serverObject: WebkulServer? //weak refrence object init (webkulServer: WebkulServer) { self.serverObject = webkulServer self.serverObject?.add(clientObject: self) } } |
Now we used weak with WebkulClient and then no retain cycle will create with dependent objects.
To check the memory leaks use profiling and then you can get the leaks information.
Now after using the weak there are no leaks.
And thanks for reading this blog, for detail info please click here
For more blogs please click here.
So pls follow the above step and And if you have any issues or suggestions you can leave your message in the comment section I will try to solve this.