Weak&Strong
Memory Leak & ARC


In this article, I will discuss the "weak" and "strong" keywords in the Swift language. Before diving into that, I would like to touch on the concepts of "ARC" and "Memory Leak."
Memory Leak: It is the situation where memory is occupied indefinitely by unreferenced objects. It typically occurs due to reasons such as cyclic references and unnecessary strong references. Memory leaks cause the unnecessarily increasing amount of memory usage and adversely affect the performance of the application.
ARC (Auto Reference Counting) is a memory management technique and the object management model used in the Swift language.
ARC keeps track of how many references each object has. When an object has a reference count greater than zero, it means it is being used in memory. When all references to an object are removed, the reference count drops to zero, and the object is automatically removed from memory. This helps prevent memory leaks and reduce unnecessary memory usage in Swift.
In general, Swift ARC provides developers with a safer and more consistent experience with memory management without the need to directly deal with it. However, there are still situations where improper usage can lead to memory leaks, so we'll address these misuse cases. For example, circular references can hinder the effectiveness of ARC and lead to memory leaks. Such situations can be resolved by using weak and unowned references.


When a variable car1, which holds a strong reference to an object, is set to nil, the object created from that reference, namely car2, continues to be retained in memory.


If car2 also becomes nil, the reference count managed by ARC drops to 0, triggering the deinit function.


If car2 is defined as weak, it creates a weak reference. When car1 is set to nil, the deinit function is triggered.