Memory management in Swift

Memory management in Swift

6 years ago
Anonymous $roN-uuAfLt

https://medium.com/elements/memory-management-in-swift-31e20f942bbc

Recently I was having a hard time tracking down a bug that was the cause of performance problems in the app I was working on. It took me a while but it turned out that these performance problems were caused by a memory leak. After this experience I thought it would be a good time to learn more about memory management in Swift which resulted in this blog post. Lets start with the basics.

Memory refers to all the hardware involved in storing information on your device. Your iPhone has two main ways of storing data 1. the disk 2. Random Access Memory (RAM). When an app is run on your iPhone a file containing all executable instructions will be loaded into the RAM. At the same time the system will claim a chunk of the RAM, which is called the heap. This is the place where all instances of our classes will live while the app is running. When we talk about memory management we refer to the process of managing heap memory. This means managing the life cycles of objects on the heap and making sure that these objects are freed when they are no longer needed so the memory can be reused. In Objective-C aside from primitives like Int, CGRect etc. everything is an object and therefore will be allocated on the heap. In Swift reference types are allocated on the heap, but values types are not. Managing the heap memory is very important because objects can be large and our apps get only so much memory from the system. Running low on memory will cause an iOS App to run slower and eventually will make the app crash. Although nowadays it is getting more rare to see a RAM overload since our devices are getting more powerful its always important to be a good memory citizen.

Last Seen
4 minutes ago
Reputation
0
Spam
0.000