[Week06][CS] Implicit Memory Management : Garbage Collection

Pyotatoยท2023๋…„ 5์›” 17์ผ
0

[CS]

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

Garbage Collection: ํž™๊ณต๊ฐ„์— ํ• ๋‹น๋œ ๊ณต๊ฐ„์„ ์ž๋™์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ๊ฒƒ

  • application์ด freeํ•ด์ค„ ํ•„์š”๊ฐ€ ์ ˆ๋Œ€ ์—†์Œ
  • Functional, scripting, ํ˜„๋Œ€ Object oriented ์–ธ์–ด์—์„œ ํ”ํžˆ ์žˆ์Œ: Lisp, ML, Java, Perl, Mathematica...
  • C์™€ C++์—์„œ๋Š” ๋ณด์ˆ˜์ ์ธ ํ˜•ํƒœ์˜ gc ์กด์žฌ :๋ชจ๋“  garbage collect(์ˆ˜์ง‘)๋ถˆ๊ฐ€:
  • ์–ธ์ œ ๋ฉ”๋ชจ๋ฆฌ free์‹œ์ผœ์ค„ ์ง€ memory manager๊ฐ€ ์•Œ๊นŒ?
    • ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฏธ๋ž˜์— ๋ญ๊ฐ€ ์–ด๋–ป๊ฒŒ ์“ฐ์ผ ์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†์Œ (์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋ฏ€๋กœ)
    • ํ•˜์ง€๋งŒ ํŠน์ • ๋ธ”๋ก๋“ค์„ ๊ฐ€๋ฅดํ‚ค๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด ๊ทธ ๋ธ”๋ก๋“ค์„ ์‚ฌ์šฉ(์ ‘๊ทผ)ํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฑด ์•Œ ์ˆ˜ ์žˆ์Œ.

1. ๊ณ ์ „์ ์ธ GC ์•Œ๊ณ ๋ฆฌ์ฆ˜

Mark & Sweep (McCarthy,1960)

  • compactํ•˜์ง€ ์•Š์€ ์ด์ƒ ๋ธ”๋ก์„ ์˜ฎ๊ธฐ์ง€ ์•Š์Œ

Reference counting (Collins,1960)

CS์—์„œ reference counting์€ ์ฐธ์กฐ์˜ ๊ฐœ์ˆ˜,ํฌ์ธํ„ฐ์˜ ๊ฐœ์ˆ˜ ๋“ฑ์„ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ˆ  ๋˜๋Š” ์ž์›(object, ๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋ก, ๋””์Šคํฌ ๊ณต๊ฐ„ ๋“ฑ๋“ฑ)์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ์‹์ด๋‹ค.
GC์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ reference counting์€ ๋”์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” object๋“ค์„ deallocateํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

  • ๊ฐ object์— ๊ด€ํ•ด counting track๋ฅผ ์ฐธ์กฐํ•˜๋ฉด์„œ, ์ฐธ์กฐ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ด ๋˜๋ฉด object๊ฐ€ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  destroy(์ œ๊ฑฐ)ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จ.

  • object๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์ด object์— ์˜ํ•ด ์ฐธ์กฐ๋˜๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ์นด์šดํŠธ๋„ ๊ฐ์†Œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ํ•˜๋‚˜์˜ ์ฐธ์กฐ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด free๋˜๋Š” object์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฌด์ง€ ์ปค์งˆ ์ˆŸ ์žˆ๋‹ค.

  • ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด reference count๊ฐ€ 0์ด ๋˜๋ฉด ๋ฐ”๋กœ ์ œ๊ฑฐํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ฐธ์กฐ๋˜์ง€ ์•Š์€ object๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด ์ฃผ๊ธฐ์ ์œผ๋กœ ์ด ๋ฆฌ์ŠคํŠธ์—์„œ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์„ ์ทจํ•จ.

  • Simple reference count๋Š” ์žฆ์€ ์—…๋ฐ์ดํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

  • ์ฐธ์กฐ๊ด€๊ณ„๊ฐ€ ์ˆ˜์ •๋˜๊ฑฐ๋‚˜ ์‚ญ์ œ๋˜๊ฑฐ๋‚˜, object์˜ reference count๊ฐ€ ๊ฐ์†Œํ•˜๊ฑฐ๋‚˜, object๊ฐ€ ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๋ณต์‚ฌ๋˜์–ด reference count๊ฐ€ ์ฆ๊ฐ€ํ•œ๋‹ค ๋“ฑ๋“ฑ์˜ ๊ฒฝ์šฐ.

  • ์“ฐ์ž„

    • Reference counting์€ ํŒŒ์ผ์‹œ์Šคํ…œ์ด๋‚˜ distributed systems์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค.
    • non-incremental tracing GC์—์„œ object ๊ทธ๋ž˜ํ”„์˜ ํฌ๊ธฐ๊ฐ€ ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜ ์ ‘๊ทผ ์†๋„๊ฐ€ ๋Š๋ ค์งˆ ๋•Œ

Copying collection(Minsky,1963)

  • Mark-compact์™€ ๋น„์Šทํ•˜๊ฒŒ Copying collection์€ garbage๋ฅผ Collectํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.
  • ์˜คํžˆ๋ ค liveobject๋“ค์„ ํ•œ๊ณณ์— ๋ชฐ์•„์ฃผ๊ณ , ๋‚˜๋จธ์ง€ ํž™ ์˜์—ญ๋“ค์€ ์˜ค๋กœ์ง€ garbage ๋ฐ–์— ์—†๊ธฐ ๋–„๋ฌธ์— ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ implicit(์•”์‹œ์ )์ธ ๋ฐฉ์‹์ด๋‹ค.

    Like markcompact but unlike marksweep copying
    garbage collection does not really collect garbage
    Rather it moves all of the live objects into one area
    and the rest of the heap is then known to be available
    because it contains only garbage Garbage collec
    tion in these systems is thus only implicit and some
    researchers avoid applying that term to the process
    Copying collectors like markingandcompacting
    collectors move the objects that are reached by the
    traversal to a contiguous area While markcompact
    collectors use a separate marking phase that traverses
    the live data copying collectors integrate the traversal
    of the data and the copying process so that most ob
    jects need only be traversed once Objects are moved
    to the contiguous destination area as they are reached
    by the traversal The work needed is proportional to
    the amount of live data all of which must be copied
    The term scavenging is applied to the copying
    traversal because it consists of picking out the worth
    while objects amid the garbage and taking them away.


๊ทธ๋ž˜ํ”„๋กœ ๋ฉ”๋ชจ๋ฆฌ ๋‚˜ํƒ€๋‚ด๊ธฐ

๋ฉ”๋ชจ๋ฆฌ๋Š” directed(๋ฐฉํ–ฅ์ด ์žˆ๋Š”) graph๋‹ค.

  • ๊ฐ ๋ธ”๋ก๋“ค์€ ๊ทธ๋ž˜ํ”„์˜ node๋‹ค.
  • ๊ฐ ํฌ์ธํ„ฐ๋Š” ๊ทธ๋ž˜ํ”„์˜ edge๋‹ค.
  • ํž™์˜์—ญ์— ์žˆ์ง€ ์•Š์œผ๋ฉด์„œ ํž™์˜์—ญ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ณ  ์žˆ๋Š” ํฌ์ธํ„ฐ๋“ค์„ ํฌํ•จํ•œ ๋…ธ๋“œ๋“ค์€ root node๋“ค์ด๋‹ค.
    • registers, stack์˜์—ญ์— ์žˆ๋Š” ๊ฒƒ, ์ „์—ญ ๋ณ€์ˆ˜๋“ค ๋“ฑ๋“ฑ
  • ์œ„์˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ node(==๋ธ”๋ก)๋ฅผ ํ–ฅํ•œ edge(pointer)๊ฐ€ ์žˆ๋‹ค๋ฉด reachable(์ ‘๊ทผ๊ฐ€๋Šฅ)ํ•˜๋‹ค.
  • Non-reachable(์ ‘๊ทผ๋ถˆ๊ฐ€๋Šฅํ•œ) ๋…ธ๋“œ๋“ค์€ garbage๋‹ค.
  • ์ฆ‰, ํฌ์ธํ„ฐ(==edge)๊ฐ€ ๊ฐ€๋ฅดํ‚ค์ง€ ์•Š๋Š”(==์ด์–ด์ ธ ์žˆ์ง€ ์•Š๋Š”, ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋Š”) ๋ธ”๋ก(==๋…ธ๋“œ)๋“ค์€ application์— ์˜ํ•ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ

Mark & Sweep Collecting

์ „์ œ

  • Application
    โ€“ new(n): ๋ชจ๋“  ์˜์—ญ์ด clear๋œ ์ƒˆ๋กœ์šด ๋ธ”๋ก์„ ๊ฐ€๋ฅดํ‚ค๋Š” ํฌ์ธํ„ฐ return
    โ€“ read(b,i): registerํ•œํ…Œ b์˜์—ญ์— ์žˆ๋Š” i ์œ„์น˜ read
    โ€“ write(b,i,v): b ๋ธ”๋ก์˜ i ์œ„์น˜์— v write
  • ๊ฐ ๋ธ”๋ก์€ header word๋ฅผ ๊ฐ€์ง
    โ€“ addressed as b[-1], for a block b
    โ€“ collector์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉ๋จ
  • GC๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” Instructions
    โ€“ is_ptr(p): p๊ฐ€ ํฌ์ธํ„ฐ์ธ์ง€ ์•„๋‹Œ์ž ๊ฒฐ์ •
    โ€“ length(b): header ๋ถˆํฌํ•จ, b ๋ธ”๋ก return
    โ€“ get_roots(): ๋ชจ๋“  root returns

malloc/free ํŒจํ‚ค์ง€ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

  • ๊ณต๊ฐ„์ด ์—†์–ด์งˆ ๋•Œ๊นŒ์ง€ malloc์„ ํ™œ์šฉํ•ด์„œ ํ• ๋‹นํ•˜๊ธฐ
  • ๊ณต๊ฐ„์ด ์—†์–ด์ง€๋ฉด
    • ๊ฐ ๋ธ”๋ก์— ์žˆ๋Š” head์˜ ์ถ”๊ฐ€์ ์ธ mark bit ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ
    • Mark(ํ‘œ์‹œํ•˜๊ธฐ) : root์—์„œ ์‹œ์ž‘ํ•ด์„œ ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ์— bit set markํ•ด์ฃผ๊ธฐ
    • Sweep(๋น—์ž๋ฃจ๋กœ ์“ธ์–ด์ฃผ๊ธฐ): ๋ชจ๋“  ๋ธ”๋ก ์Šค์บ”ํ•˜๊ธฐ โžก๏ธ ํ• ๋‹น์€ ๋์ง€๋งŒ mark์€ ํ•˜์ง€์•Š์€ ๋ธ”๋ก๋“ค freeํ•ด์ฃผ๊ธฐ
  • Markํ•ด์ฃผ๊ธฐ : ๋ฉ”๋ชจ๋ฆฌ ๊ทธ๋ž˜ํ”„์—์„œ depth-first traversal
  • Sweepํ•ด์ฃผ๊ธฐ : ํฌ์ธํ„ฐ์˜ ๊ธธ์ด๋ฅผ ํ†ตํ•ด ๋‹ค์Œ ๋ธ”๋ก์ฐพ๊ธฐ

Conservative Mark & Sweep (๋ณด์ˆ˜์ ์ธ)

  • Is_ptr() : ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋ก์„ ๊ฐ€๋ฅดํ‚ค๋Š” ์ง€์— ๋”ฐ๋ผ word๊ฐ€ ํฌ์ธํ„ฐ์ธ์ง€ ๊ฒฐ์ •
  • C ํฌ์ธํ„ฐ์—์„œ๋Š” ๋ธ”๋ก ์ค‘๊ฐ„์„ ๊ฐ€๋ฅดํ‚ฌ ์ˆ˜๋„ ์žˆ์Œ!
  • ๋ธ”๋ก ์‹œ์ž‘์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•?
    • ๊ท ํ˜•ํŠธ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ• ๋‹น๋œ ๋ชจ๋“  ๋ธ”๋ก track โžก๏ธ key ์œ„์น˜ ํŒŒ์•…
    • ๊ท ํ˜•ํŠธ๋ฆฌ ํฌ์ธํ„ฐ๋Š” header์— ์ €์žฅ ๋  ์ˆ˜ ์žˆ์Œ (word 2๊ฐœ ์ถ”๊ฐ€์ ์œผ๋กœ ์‚ฌ์šฉ)


References

profile
https://pyotato-dev.tistory.com/ ๋กœ ์ด์‚ฌ์ค‘ ๐Ÿšš๐Ÿ’จ๐Ÿš›๐Ÿ’จ๐Ÿšš๐Ÿ’จ

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