-
(Swift) ARC (Automatic Reference Counting, 자동 참조 계산)iOS💖 2024. 10. 15. 18:11
@escaping 을 공부하다가 여기 까지 오게 되었음 ㅠㅠ ㅎ
ARC (Automatic Reference Counting, 자동 참조 계산)
- 앱의 메모리 사용량을 추적, 관리
- class instance 가 더 이상 필요하지 않을 때, Heap 영역에 할당된 해당 instance 에서 사용하는 메모리를 자동으로 해제
- ARC는 reference counting 이기 때문에, reference 타입인 클래스에만 적용 ( structure, enum (X) )
장점
- 더 이상 메모리가 필요하지 않은 class instance 의 메모리를 관리해줌
- ➡️ 메모리가 필요한 곳에서 사용할 수 있게 해줌
단점 (위험성)
- 사용 중인 instance를 할당 해제하여, 해당 instance의 attribute (속성) 에 접근하거나, method를 호출하는 것이 불가능 해짐
- ➡️ 앱이 crash (충돌)이 일어날 가능성이 크다.
- instance 가 필요한 동안 메모리 할당 해제되지 않도록!
- ➡️ ARC는 instance를 참조하는 속성(properties), 상수 및 변수의 수를 추적한다.
- ➡️ 활성 되고 있는 참조가 있는 한, 메모리 할당 해제는 하지 않음
(+) ARC가 메모리 할당 해제 할 수 없는 참조
- strong reference
- rc가 1개 이상인 경우
- strong reference cycle 이 생성된 경우
(+) ARC의 메모리 할당 해제를 막을 수 없는 참조
- weak, unowned reference
Documentation
docs.swift.org
메모리 참조 속성 (strong, weak, non-optional unowned, optional unowned references)
https://hortenssiaa.tistory.com/38
(Swift) 메모리 참조 속성 (strong, week, unowned)
Apple Documentation 을 보고 정리하였습니다. 1. strong 참조default reference type 이다. var / let 앞에 week, unowned 가 붙지 않으면 strong reference type 임!!instance 를 확실히 유지 & strong 참조가 유지되는 한, 할
hortenssiaa.tistory.com
'iOS💖' 카테고리의 다른 글
(Swift) the concept of capturing (capturing 개념) (2) 2024.10.16 (Swift) 메모리 참조 속성 (strong, week, unowned, optional unowned) (3) 2024.10.16 (Swift) Result<Success, Failure> 타입 (1) 2024.10.14 (Swift) @frozen (3) 2024.10.14 (Swift) private(set) var (0) 2024.10.11