I am trying to learn about Memory Management in iOS development. I read this article / tutorial: Make Memory Management Great Again
In that tutorial, there is a deinit
method inside a class, like this:
class Human {
var passport: Passport?
let name: String
init(name: String) {
self.name = name
}
deinit {
print("I'm gone, friends")
}
}
After we make an instance, the reference count is one because it is a strong reference. until this step, I understand.
var bob: Human? = Human(name: "Bob Lee")
It is said that when we make an instance, it actually takes space in our RAM.
If we assign nil to 'bob' variable, the deinit will print ("I'm gone, friends"), and the relationship no longer exists, so the reference count becomes 0 which causes both objects to be deallocated.
The things that makes me confused:
in my actual code / from tutorial I follow, I never see 'deinit' in my class, and I never assign nil to the instance, so the object will never be deallocated and it will take space in my memory like fat? should I write deinit in my code? because I think if over limited of space, it would be filled with data objects and eventually my app would break
it is said that:
Automatic Reference Counting to indicate whether an object is still being used or no longer needed
No longer needed? What does it mean?