[Swift] ARC(Automatic Reference Counting)
ARC(Automatic Reference Counting)
- 자동 참조 카운팅
- 앱 메모리 사용량 추적, 관리
- class instance 가 더이상 필요하지 않으면 메모리에서 자동으로 할당 해제
- ARC는 reference type인 class instance에만 적용됨(struct, enum은 value type으로 ARC에 적용되지 않음)
How ARC Works
- ARC는 instance가 언제까지 필요한지 파악하기 위해 instance를 참조하는 프로퍼티, 상수, 변수의 수를 추적.
- 하나 이상의 요소가 instance를 참조하고 있다면 ARC는 할당을 해제하지 않음
- 프로퍼티, 상수, 변수에 instance를 할당할 때 마다 강한 참조를 만듬.
- 참조 카운트가 0이되면 deinit
Strong Reference Cycles Between Class Instances
- 두 class instance가 서로에 대한 강한 참조를 유지하여 강한 참조가 없는 경우 발생 => strong reference cycle
Strong reference Cycle 해결
- weak / unowned references로 해결
참고