retain cycle

hankyulee·2022년 6월 18일
0

value type:
The change on one of them wouldn’t affect the other one because the original value was copied to the other instance and the copied value is still the same.

reference type:
If one changes, the other one also changes because the values in that memory space would change.

ARC: Swift keeps track of the references created in the code by counting the references each class instance have in order to know when it is safe to deallocate the class instance from the memory.

deinitialize: 하나의 객체에 여러 변수가 참조하고 있을때, Only after when all instances of the same object is removed, the object is truly deinitialized and it is deallocated from the memory. Hence it prints out the statement in the deinit function.

Memory leak occurs when an amount of allocated memory space cannot be deallocated for some reason in iOS applications, the usage of memory for that application always grows while the app is running. Memory leaks may be very critical since they can use up all the memory reserved for the application quickly and making the iOS operating system force shut down the application.

참고:
value types (structs and enums).

Classes are reference types and they are stored in the heap parts of the memory.

As long as an object has a strong reference, it cannot be deallocated from the memory even if the screen containing the object has popped from the navigation stack.

Weak references also does not increase the ARC count when pointing to an object

xcode상에서 실험해보자.

reference: https://betterprogramming.pub/retain-cycles-and-memory-management-in-swift-fb6226165b17#:~:text=%E2%80%94%20in%20order%20to%20deallocate%20an,is%20called%20a%20retain%20cycle.

0개의 댓글