Automatic reference counting
Now that we understand the different ways in which data is represented in Swift, we can look at how to manage memory better. Every instance we create takes up memory. Naturally, it wouldn't make sense to keep all data around forever. Swift needs to be able to free up memory that can be used for other purposes once our program doesn't need it anymore. This is the key to managing memory in our apps. We need to make sure that Swift frees up all the memory that we no longer need as soon as possible.
The way that Swift knows it can free up memory is when the code no longer has access to an instance. If there is no longer any variable or constant referencing an instance, it can be repurposed for another instance. This is called "freeing the memory" or "deleting the object".
In Chapter 3, One Piece at a Time – Types, Scopes, and Projects, we already discussed when a variable is accessible or not in the section about scopes. This makes memory management very simple for...