How does Automatic Reference Counter work in iOS? In fact, this topic was easier for me to understand after I got acquainted with the Manual Reference Counter. This is a very simple article to help you get a basic understanding of how memory management works in iOS.
There are several tools for memory management in iOS:
MRC - Manual Reference Counter
MRC is manual link management through code. In the early days and in prehistoric times, the developers themselves managed the reference counting through teams. It was, to put it mildly, tough:
alloc - creating an object (creating a link)
retain - referring to it (+1 to the link)
release - decrement the reference count (-1)
dealloc - if reference count is 0 = unload from memory
Basically, you allocate an object, save it at some point, and then send one release for each allocation / save you send. The dealloc method is called on an object when it is removed from memory.
Problems:
You need to constantly count retain, release
crash when accessing from unloaded memory
forgot to put the release - memory leak
ARC - Automatic reference counter
, , , - iOS . . ARC . . , ARC , .
?
(release/retain - ) dealloc -
properties change - weak/strong
property :
strong - retain
weak - assign.
, ARC:
Retain cycle is when the amount of allocated space in memory cannot be reclaimed due to save cycles. Because Swift uses Automatic Reference Counting (ARC), a save loop occurs when two or more objects contain strong references to each other. As a result, these objects keep each other in memory, because their save count will never decrease to 0, which will prevent the deinit function from being called and memory free
The solution is trivial - to make one of the links weak.
This article is more basic and introductory to the basics of memory management in iOS, for an easy conceptual understanding.