๐Ÿ“ARC (Automatic Reference Counting) : ์ž๋™ ์ฐธ์กฐ ๊ณ„์‚ฐ

2022. 6. 24. 16:19ใ†Programming/Swift

 

 

ARC (Automatic Reference Counting)

 

: Objective-C / Swift์— ๋Œ€ํ•œ ์ž๋™ ์ฐธ์กฐ ๊ณ„์‚ฐ์„ ์ œ๊ณตํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ

- ์ปดํŒŒ์ผ ์‹œ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์ž๋™์œผ๋กœ retain, release๋ฅผ ์ ์ ˆํ•œ ์œ„์น˜์— ์‚ฝ์ž…

- Objective-C ์—์„œ๋Š” ์ˆ˜๋™์œผ๋กœ reference count ์ฆ๊ฐ์„ ํ†ตํ•ด ๊ฐ์ฒด ์œ ์ง€ ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ 

-> ์ˆ˜๋™์œผ๋กœ ์ž‘์„ฑ๋˜๋˜ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ž‘์„ฑํ•ด์ค€๋‹ค. 

 

 

 

๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋Š” Data, Heap, Stack, Code ์ด๋ ‡๊ฒŒ 4๊ฐ€์ง€ ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ ์ค‘ ํž™ ์˜์—ญ๊ณผ ๊ด€๋ จ์žˆ์Œ

- Heap ์€ class, closure ๋“ฑ์˜ ์ฐธ์กฐํ˜•(reference) ์ž๋ฃŒ ๋“ค์ด ๋จธ๋ฌด๋Š” ๊ณต๊ฐ„

+ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋™์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ -> ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ํ•„์š” 

 

 

Heap ์˜์—ญ์— ์ฐธ์กฐํ˜• ์ž๋ฃŒ๋“ค์ด ์–ผ๋งˆ๋‚˜ ์ฐธ์กฐ๋˜๊ณ  ์žˆ๋Š”์ง€ ์นด์šดํŒ… & ์ด์— ๋”ฐ๋ผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น ๋ฐ ์ œ๊ฑฐ

-> ์ด๊ฒƒ์„ ์ž๋™์œผ๋กœ ์ถ”์  ๋ฐ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ARC !

 

 

ARC์˜ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ Swift Runtime ์ด๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๊ตฌํ˜„๋จ

Swift Runtime์€ ๋™์  ํ• ๋‹น๋˜๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ HeapObject๋ผ๋Š” ๊ตฌ์กฐ์ฒด๋กœ ํ‘œํ˜„

HeapObject์—์„œ๋Š” Swift์—์„œ ๊ฐ์ฒด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ, ์ฆ‰ reference count์™€ type meta data๋ฅผ ํฌํ•จ

 

 

 


 

ARC ์ •๋ฆฌ: ๊ฐ์ฒด์— ๋Œ€ํ•ด ๊ฐ•ํ•œ ์ฐธ์กฐ๊ฐ€ ๋‚จ์•„ ์žˆ๋Š” ํ•œ ํ•ด๋‹น ๊ฐ์ฒด๋Š” ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ X

but ๊ฐ•ํ•œ ์ฐธ์กฐ๋งŒ ์“ด๋‹ค๋ฉด ์ผ์ผ์ด ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ๋ฅผ ์œ„ํ•ด ์‹ ๊ฒฝ ์จ์•ผํ•จ -> ์—„์ฒญ๋‚œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋‚ญ๋น„

๊ทธ๋ž˜์„œ iOS์—๋Š” weak, unowned์„ ํ†ตํ•ด ๋” ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๊ฐ€๋Šฅ

 


 

 

 

์ถœ์ฒ˜:

https://sujinnaljin.medium.com/ios-arc-๋ฟŒ์‹œ๊ธฐ-9b3e5dc23814

https://devsrkim.tistory.com/entry/Swift-๋ฉ”๋ชจ๋ฆฌ๋ฅผ-์ฐธ์กฐํ•˜๋Š”-๋ฐฉ๋ฒ•-Strong-Weak-Unowned

https://velog.io/@wook4506/iOS-Swift-๋ฉ”๋ชจ๋ฆฌ-์ฐธ์กฐ-๋ฐฉ๋ฒ•-strong-weak-unowned

 

'Programming > Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๐Ÿ“„ Fileprivate ๋ž€?  (0) 2022.07.21
๐Ÿ‘ฉโ€๐Ÿ’ป UUID(Universally Unique IDentifier)๋ž€?  (0) 2022.06.30
@IBAction & @IBOutlet ๐Ÿค”  (0) 2022.06.24
[Swift UI] View / Some์ด๋ž€? / ContentView / Previews  (0) 2022.06.16
[SwiftUI] Geometry Reader  (0) 2022.06.16