[Swift] ARC (Automatic Reference Counting)

parkgyurimยท2022๋…„ 7์›” 22์ผ


๋ชฉ๋ก ๋ณด๊ธฐ

์šฐ์„  ARC ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ธฐ ์ „์— Reference Count ์—๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿงฎ Reference Count

Swift ์˜ Reference Type ์˜ ์ธ์Šคํ„ด์Šค๋Š” ์„ ์–ธ์ด ๋˜๋ฉด Heap ์˜์—ญ์— ํ• ๋‹น์ด ๋ฉ๋‹ˆ๋‹ค.

Stack ์˜์—ญ์ด ์•„๋‹ˆ๋ผ Heap ์˜์—ญ? ์ด๋ผ๊ณ  ์˜๋ฌธ์ด ๋“ค ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, Heap ์˜์—ญ์— ์‹ค์ œ ์ธ์Šคํ„ด์Šค๊ฐ€ ํ• ๋‹น์ด ๋˜๊ณ  Heap ์˜์—ญ์— ํ• ๋‹น๋œ ์ธ์Šคํ„ด์Šค์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ Stack ์˜์—ญ์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, Heap ์˜์—ญ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜๊ฐ€ Stack ์— ํ• ๋‹น!


Referecne Count ๋ž€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฐธ์กฐ๋˜๊ณ  ์žˆ๋Š” ์ƒํƒœ (ํšŸ์ˆ˜) ์ž…๋‹ˆ๋‹ค. (๋ช‡ ๋ช…์ด ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š”์ง€!)

๋”ฐ๋ผ์„œ Reference Type ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ์ด ๋˜๋ฉด Heap ์˜์—ญ์— ์ƒ์„ฑ๋˜๊ณ , ํ•ด๋‹น ์ธ์Šคํ„ด์Šค์˜ Referecne Count ๊ฐ€ 1๋กœ ์„ค์ •์ด ๋˜๋Š” ๊ฑฐ์ฃ .

๊ทธ๋ฆฌ๊ณ  Reference Type ์„ ๋ณต์‚ฌ (ํ• ๋‹น) ๋ฅผ ํ• ๋•Œ ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ฐธ์กฐ๋ฅผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌ (ํ• ๋‹น) ํ•˜๊ฒŒ ๋˜๋ฉด Reference Count ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

๋ฐ˜๋Œ€๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒŒ ๋˜๋ฉด Referecount ๋ฅผ ๊ฐ์†Œ์‹œ์ผœ์•ผํ•˜๊ตฌ์š”. (์˜ต์…”๋„ ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ผ๋ฉด nil์„ ์„ ์–ธํ•  ๊ฒฝ์šฐ์—๋„ ๋‹น์—ฐํžˆ Reference Count ๊ฐ€ ๊ฐ์†Œ)


C ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ• ๋•Œ malloc ์„ ์‚ฌ์šฉํ–ˆ์œผ๋ฉด free ๋ฅผ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๋˜ ๊ฒƒ, ๊ธฐ์–ตํ•˜์‹œ๋‚˜์š”?

Heap ์˜์—ญ์€ ๋™์ ์œผ๋กœ ํ• ๋‹น๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ์˜์—ญ์ด๊ณ , ์‚ฌ์šฉ ํ›„ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ํ• ๋‹น ํ•ด์ œ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!

๋งŒ์•ฝ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์ด ์—†์–ด Reference Count ๊ฐ€ 0 ์ด ๋˜๋ฉด ํ• ๋‹น ํ•ด์ œํ•ด์•ผ๊ฒŸ์ฃ ..?

์ •๋ฆฌํ•˜๋ฉด..

  • Reference Typed ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์–ด๋””์„ ๊ฐ€ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด Reference Count ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋˜๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ์†Œํ•œ๋‹ค!
  • Reference Count ๊ฐ€ 0 ์ด ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ• ๋‹น ํ•ด์ œํ•ด์•ผ ํ•œ๋‹ค!


๊ทผ๋ฐ ๋ˆ„๊ฐ€ ํ•˜๋ƒ? ARC ๊ฐ€ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿค– ARC


Swift uses Automatic Reference Counting (ARC) to track and manage your appโ€™s memory usage. In most cases, this means that memory management โ€œjust worksโ€ in Swift, and you donโ€™t need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed. - ๐Ÿ“š Swift Documentations - ARC

ARC (Automatic Reference Counting) ๋Š” Swift ์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ถ”์ ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์ž๋™์œผ๋กœ, ์•Œ์•„์„œ ๊ด€๋ฆฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ๋ฐœ์ž๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ์— ํฌ๊ฒŒ ์‹ ๊ฒฝ์„ ์“ธ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค!

์œ„์—์„œ ๋ณด์•˜๋“ฏ์ด, Reference Type ์˜ Reference Count ๋ฅผ ์ถ”์ ํ•˜๊ณ , ํ•„์š”๊ฐ€ ์—†์–ด์ง€๋ฉด ํ• ๋‹น ํ•ด์ œ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค!

in Runtime?

ARC ์˜ ์—ญํ• ์„ ๋ณด๋ฉด ๋Ÿฐํƒ€์ž„์— ๋™์ž‘ํ•˜๋Š” ๊ฒƒ ์ด๋ผ๊ณ  ์ƒ๊ฐ์ด ๋“ค ์ˆ˜ ๋„ ์žˆ์ง€๋งŒ, ์‚ฌ์‹ค์€ ์ปดํŒŒ์ผ ํƒ€์ž„์— ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

์˜ˆ์ „ Objectivce - C ๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ์‹œ์ ˆ์—๋Š” retain / release ๋ฅผ ํ†ตํ•ด ์ˆ˜๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. (MRC - Manual Reference Counting)

ARC ๋Š” ์ปดํŒŒ์ผ ํƒ€์ž„์— ์ ์ ˆํ•œ ๊ณณ์— retain, release ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Side Effect

๊ฐœ๋ฐœ์ž์˜ ์ธก๋ฉด์—์„œ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์ง์ ‘ ์ˆ˜๋™์œผ๋กœ ํ•˜์ง€ ์•Š์•„์„œ ์ข‹์€ ์ ๋„ ์žˆ์ง€๋งŒ, ARC ๋กœ ์ธํ•ด์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ ๋˜ํ•œ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ A ๋ผ๋Š” ์ธ์Šคํ„ด์Šค๊ฐ€ B ๋ผ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•ด์„œ ๋ฌด์–ธ๊ฐ€๋ฅผ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ B ์ธ์Šคํ„ด์Šค๋Š” C ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๊ณ , C ์ธ์Šคํ„ด์Šค ๋˜ํ•œ B ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ํ• ๋•Œ, A ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ชจ๋“  ์ž‘์—…์„ ๋๋‚ด๊ณ  B ์ธ์Šคํ„ด์Šค๊ฐ€ ํ•„์š”๊ฐ€ ์—†์–ด์ง„ ์ƒํƒœ๊ฐ€ ๋˜์–ด๋„ C ์ธ์Šคํ„ด์Šค์™€ ์„œ๋กœ ์ฐธ์กฐ๋ฅผ ํ•˜๊ณ  ์žˆ๊ธฐ๋•Œ๋ฌธ์— Reference Count ๊ฐ€ 0 ์ด ๋˜์ง€ ์•Š์•„ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ฐ•ํ•œ ์ˆœํ™˜ ์ฐธ์กฐ (Strong Reference Cycle) ์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ์š”, ๋‹ค์Œ ํฌ์ŠคํŒ…์„ ํ†ตํ•ด์„œ ๊ฐ•ํ•œ ์ˆœํ™˜ ์ฐธ์กฐ๋ฅผ ํ”ผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

๐Ÿ”— https://velog.io/@parkgyurim/Swift-Strong-Reference-Cycle

๐Ÿ‘ ๋งˆ๋ฌด๋ฆฌ

์˜ค๋Š˜์€ Swift์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ๋ฐฉ์‹์ธ ARC ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

ํ‹€๋ฆฐ ์ •๋ณด ๋˜๋Š” ๊ถ๊ธˆํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค! ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹คโ€ผ๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€