Our device📱basically has two types of memories where it saves our data:

  • Disk memory
  • Random Access Memory (RAM)

When we open an application, a small part of RAM is allocated for that application where all the instances for its classes are stored. This chunk of memory is called Heap memory and this is the part of memory we refer to when we talk about memory management.

Heap_ is the portion of memory (RAM) where dynamically allocated memory resides_

Since the heap memory is very limited, it is very important that we manage it properly 💰. Poor management of memory will considerably slow down the app and sooner or later, cause a crash.

But how does swift manage the heap memory?

Swift uses something called Automatic Reference Counting (ARC) for memory management. Every time a new object of a class is created, ARC allocates some amount of memory to store data associated with that object. Each object will have a reference count property that keeps track of everything that has a **strong **reference to it. Every time a strong reference is created, the reference count of that object gets incremented by one. And whenever a reference goes out of scope, the reference count gets decremented by one.

#arc #swift #memory-leak #memory-management #retain-cycle

Memory Management in Swift
1.15 GEEK