In this article, I will tell you about memory leaks. I am planning to practice Swift programming languages memory leak.
What is a memory leak?
As per Apple,
“ Memory that was allocated at some point, but was never released and is no longer referenced by your app. Since there are no references to it, there’s now no way to release it and the memory can’t be used again. So a memory leak occurs when a content remains in memory even after its life cycle has ended."
Why do I need to take care of Leaks?
In summary,
Memory leaks cause footprint.
Poor UX
Application crash
The applications and projects we work on expand over time. We are adding new features to make a quality product. As the project grows as new features are added, the application should not spend much effort. Failure to fix this problem may cause the application to crash.
e.g., Let’s understand more clearly. Considering that memory is a hotel, we can think of each hotel room as a memory. Let the people staying in the room be variable, too. Only one person was left in the room, and the person left the hotel. What happens if the room is dirty? A new person will come, but the room is not cleaned. So there was no room for the variable. This also caused a memory leak due to footprint.
Automatic Reference Counting
Swift uses Automatic Reference Counting (ARC) to track and manage your app’s memory usage. In most cases, memory management “just works” in Swift, and you don’t need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed.
Explain an example:
class Foods {
let name: String
init(name: String) {
self.name = name
}
}
We have defined the Foods class from above. Imagine we are coding Food Delivery App.
An object is released if it goes outside the scope of its current ViewController. However, if the object was created inside a function, its scope will only be inside the function. When the function execution is complete, the object is released.
We have a view controller named FoodsViewController and create an object of the Foods class in the viewDidLoad() method as follows:
override func viewDidLoad() {
super.viewDidLoad()
let food = Foods(name: "Pizza")
}
In Foods Delivery Case, the food object is not released unless the FoodsViewController goes out of scope. But it is necessary to define the runApp() function as follows:
func runApp() {
let food = Foods (name: "Pizza")
}
If we call runApp() in viewDidLoad() instead of creating the object here, the Foods object will be reallocated when runApp() goes out of scope.
As soon as the reference count reaches zero (think about the hotel example), the person will be deallocated, and the hotel associated with that person will be free to be used by another person.
How to detect Memory Leaks with Allocations and Leaks Instruments
Apple provides developers with some tools to help catch memory leaks.
Instruments
In Instruments, we use specialized tools, known as instruments, to trace different aspects of your apps, processes, and devices over time. Instruments collect data as it profiles and present the results to you in detail for analysis.
Firstly, Product > Profile > Leaks
Secondly, you must choose Leaks.
Then, press the red button on the top left of the panel to start recording. When the app automatically launches, we press the navigate button that presents our leaking view controller.
When we run the instruments, we can see a memory leak and the leaked objects.
I hope you enjoyed reading this article and have an idea of what you can do to improve the quality of your code.
If you want to reach my social media accounts, contact me, and be informed about my work, You can follow and communicate with me on LinkedIn, Github, and Twitter. Thanks!
Please check the links for more information:
https://help.apple.com/instruments/mac/current/#/dev7b09c84f5
https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
https://www.raywenderlich.com/16126261-instruments-tutorial-with-swift-getting-started