Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Garbage Collection

Stefan Kanev
November 03, 2013

Garbage Collection

OpenFest 2013 talk about garbage collection

Stefan Kanev

November 03, 2013
Tweet

More Decks by Stefan Kanev

Other Decks in Programming

Transcript

  1. Garbage Collection
    Стефан Кънев
    http://skanev.com/
    @skanev
    OpenFest
    3 ноември 2013
    София

    View Slide

  2. Здравейте, аз съм Стефан

    View Slide

  3. DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS

    View Slide

  4. twitter @skanev
    github skanev
    blog http://skanev.com/

    View Slide

  5. Всеки път

    View Slide

  6. 03.11.2013

    View Slide

  7. I ❤︎ LEGACY

    View Slide

  8. @current_year = 2011
    ‑︎
    @current_year = 2012

    View Slide

  9. View Slide

  10. 2.0 & 2.1

    View Slide

  11. I ❤︎ LEGACY

    View Slide

  12. ?

    View Slide

  13. Garbage Collection

    View Slide

  14. View Slide

  15. View Slide

  16. “The undecidability of liveness is a
    corollary to the halting problem”
    — Garbage Collection, Jones et al

    View Slide

  17. View Slide

  18. Stop-and-Copy
    1 hour coding
    6 hours debugging

    View Slide

  19. Garbage Collection

    View Slide

  20. View Slide

  21. BG PUBLIC ENEMY №1

    View Slide

  22. -а/-ът

    View Slide

  23. View Slide

  24. Ще деплойнем в пръдъкшън
    след като билда мине на
    кънтиниъс интегрейшъна.
    DISCLAIMER

    View Slide

  25. събирачи на смет
    свободни списъци
    пържоли
    корени

    View Slide


  26. View Slide

  27. Пълният член смуче!

    View Slide

  28. View Slide

  29. ЦСКА vs. Левски

    View Slide

  30. ЦСКА vs. Балкан Ботевград

    View Slide

  31. Долу пълния член!

    View Slide

  32. #ътставка

    View Slide


  33. View Slide

  34. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  35. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  36. Автоматично освобождаване на
    неизползваната памет, без
    баене от страна на програмиста

    View Slide

  37. това включва и Reference counting

    View Slide

  38. Не целя:
    kernel developer-и с Garbage Collector
    web developer-и с malloc()

    View Slide

  39. Целя:
    kernel developer-и с Garbage Collector
    web developer-и с malloc()

    View Slide

  40. Целя:
    Да добиете някаква представа за
    collector-ите и особеностите им

    View Slide

  41. Common misconceptions

    View Slide

  42. View Slide

  43. ✔! алокацията е по-бърза от malloc()

    View Slide

  44. ✘ паузи по минута са възможни

    View Slide

  45. heap
    памет, където обекти
    се алокират в
    произволен ред
    mutator
    програмата
    (променя паметта)
    roots
    стека и регистрите
    live set
    паметта, достъпна от
    корените
    pointer
    “стрелка” от един
    обект към друг
    cells/objects
    парчета управлявана
    памет
    garbage
    недостижимата памет

    View Slide

  46. i = 0
    while i < N:
    i += 1
    doSomething(i)

    View Slide

  47. Динамично алокиране

    View Slide

  48. View Slide

  49. <=>

    View Slide

  50. O(nlgn)

    View Slide

  51. cnlgn

    View Slide

  52. CPU cache
    virtual memory*

    View Slide

  53. различни приложения,
    различни pattern-и на работа

    View Slide

  54. сложен инженерен проблем

    View Slide

  55. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  56. REFERENCE COUNTING
    MARK & SWEEP
    MARK & COMPACT
    COPY

    View Slide

  57. Reference Counting

    View Slide

  58. 1. Всеки обект има брояч
    2. Всеки указател увеличава брояча
    3. Триенето на указател намалява
    4. free() когато брояча е нула
    Outline

    View Slide

  59. roots
    1 1
    REFERENCE
    COUNTING

    View Slide

  60. roots
    1 2
    REFERENCE
    COUNTING

    View Slide

  61. roots
    1 1
    REFERENCE
    COUNTING

    View Slide

  62. roots
    1 1 1
    REFERENCE
    COUNTING

    View Slide

  63. roots
    1
    0 1
    REFERENCE
    COUNTING

    View Slide

  64. roots
    0 1
    REFERENCE
    COUNTING

    View Slide

  65. roots
    1
    REFERENCE
    COUNTING

    View Slide

  66. просто & лесно

    View Slide

  67. View Slide

  68. View Slide

  69. View Slide

  70. roots
    1 1
    REFERENCE
    COUNTING

    View Slide

  71. roots
    2 2
    REFERENCE
    COUNTING

    View Slide

  72. roots
    1 2
    REFERENCE
    COUNTING

    View Slide

  73. roots
    1 1
    Memory Leak
    REFERENCE
    COUNTING

    View Slide

  74. View Slide

  75. weak-refs
    разни алгоритми

    View Slide

  76. чисто решение няма
    трябва tracing

    View Slide

  77. View Slide

  78. roots
    1 1
    1
    1 1
    1
    1
    1
    1

    View Slide

  79. детерминизъм
    vs.
    паузи при големи обекти

    View Slide

  80. ✔; Проста имплементация
    ✔; Детерминистичност
    ✘ Инкрементации/декрементации
    ✘ Допълнителна памет
    ✘ Паузи при големи обекти*
    ✘ Не освобождава цикли
    ✔; Повече по-късно

    View Slide

  81. Reference Counting има много
    варианти

    View Slide


  82. View Slide

  83. x = []
    y = []
    id(x) == id(y)
    => False
    id([]) == id([])
    => True

    View Slide

  84. a = []
    a = id(a)
    b = []
    b = id(b)
    a == b

    View Slide

  85. #lolpython

    View Slide

  86. Mark & Sweep

    View Slide

  87. малко интро

    View Slide

  88. header header header
    heaps

    View Slide

  89. header
    off-heap storage

    View Slide

  90. Free list
    Heap
    Heap
    MARK/SWEEP

    View Slide

  91. 1. Започваме от root-овете
    2. Рекурсивно маркираме всичко, до
    което може да стигнем
    3. Трием немаркираното
    Outline

    View Slide

  92. roots
    MARK/SWEEP

    View Slide

  93. roots
    roots
    Garbage
    MARK/SWEEP

    View Slide

  94. Heap
    MARK/SWEEP

    View Slide

  95. Heap
    Fragmentation
    MARK/SWEEP

    View Slide

  96. ✔; Събира цикли
    ✔; По-икономичен (памет и процесор)
    ✘ Пауза за обхождане и събиране*
    ✘ Повече cache misses
    ✘ Недетерминистичен
    ✘ Нужда от свободна памет
    ✘ Линеен на спрямо heap-а

    View Slide

  97. Mark & Compact

    View Slide

  98. 1. Маркираме всичко
    2. Местим обекти в дупките
    3. Обновяваме указателите
    Outline

    View Slide

  99. MARK/COMPACT

    View Slide

  100. MARK/COMPACT

    View Slide

  101. STALE POINTERS
    MARK/COMPACT
    Active Memory Forwarding Adresses

    View Slide

  102. ✔; Евтина алокация
    ✔; Дефрагментира паметта
    ✘ Линейно на heap-а
    ✘ Копирането е бавно
    ✘ Няма свободни pointer-и (мести)

    View Slide

  103. Copy

    View Slide

  104. 1. Разделяме heap-а на две
    2. Алокираме в едната половина
    3. Копираме достижимото в другата
    Outline

    View Slide

  105. From space To space
    COPY

    View Slide

  106. From space To space
    COPY

    View Slide

  107. From space To space
    COPY

    View Slide

  108. ✔; Евтина алокация
    ✔; Дефрагментира паметта
    ✔; Линейно на живите обекти
    ✘ 50% място разхищение
    ✘ Трябва да мести обекти

    View Slide

  109. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  110. STOP-THE-WORLD
    vs.
    INCREMENTAL
    vs.
    PARALLEL
    vs.
    CONCURRENT

    View Slide

  111. STOP-THE-WORLD
    collector
    mutator

    View Slide

  112. INCREMENTAL
    sweep
    mutator
    mark
    ✔! Разпределени кратки паузи
    ✘ Overhead при алокация

    View Slide

  113. PARALLEL
    collector
    mutator

    View Slide

  114. CONCURRENT
    collector
    mutator
    ✔! Не спира света
    ✘ Overhead за синхронизация

    View Slide

  115. MOSTLY CONCURRENT
    collector
    mutator
    ✘ Мутатора и колектора се гонят

    View Slide

  116. CONSERVATIVE
    vs.
    PRECISE

    View Slide

  117. Трябва помощ от компилатора

    View Slide

  118. View Slide

  119. Weak generational hypothesis:
    Most objects die young

    View Slide

  120. 1. Разделяме heap-а на млади и стари
    2. Събираме основно младите (minor)
    3. От време на време събираме старата
    генерация (major cycle)
    Outline

    View Slide

  121. Old Generation New Generation
    roots

    View Slide

  122. Old Generation New Generation
    roots
    roots

    View Slide

  123. Old Generation New Generation
    roots
    roots

    View Slide

  124. Old Generation New Generation
    roots
    roots
    BUG

    View Slide

  125. WRITE
    BARRIER

    View Slide

  126. old ˠ young

    View Slide

  127. roots
    Old Generation New Generation
    remembered
    set

    View Slide

  128. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  129. Reference counting
    No cycle detection ;(

    View Slide

  130. Reference counting
    Full cycle detection

    View Slide

  131. Reference counting
    Almost full cycle detection

    View Slide

  132. __del__
    finalizers

    View Slide

  133. gc.garbage

    View Slide

  134. trololothon

    View Slide

  135. Mark and sweep
    Incremental, conservative (...)

    View Slide

  136. Java!

    View Slide

  137. View Slide

  138. 4

    View Slide

  139. 1
    SerialGC
    Stop-the-world copy for young generation
    Stop-the-world mark/compact for old
    generation

    View Slide

  140. 2
    ParallelGC
    Stop-the-world copy for young generation
    Stop-the-world mark/compact for old
    generation

    View Slide

  141. 3
    ConcMarkSweepGC
    Stop-the-world copy for young generation
    Mostly concurrent, non-compacting mark-
    sweep for older generation
    Fallback to stop-the-world compacting full
    collection

    View Slide

  142. 4
    G1GC
    Stop-the-world copy for young generation
    Mostly concurrent marking old generation
    Stop-the-world, mostly incremental
    compacting for old generation
    Fallback to stop-the-world compaction

    View Slide

  143. HotSpot
    JRockit
    IBM J9
    Azul Zing

    View Slide

  144. View Slide

  145. View Slide

  146. C
    C++

    View Slide

  147. Boehm-Demers Collector
    http://www.hpl.hp.com/personal/Hans_Boehm/gc/

    View Slide

  148. malloc ! GC_MALLOC
    realloc ! GC_REALLOC

    View Slide

  149. scan the stack
    find all pointers
    mark/sweep

    View Slide

  150. detect memory leaks

    View Slide

  151. View Slide

  152. i.Garbage Collection
    ii.Algorithms
    iii.Classifications
    iv.Languages
    v.Ruby 2.0 & Ruby 2.1

    View Slide

  153. 2.0 & 2.1

    View Slide

  154. CoW friendly
    ‐︎
    copy-on-write

    View Slide

  155. View Slide

  156. View Slide

  157. View Slide

  158. RGenGC

    View Slide

  159. View Slide

  160. RARRAY_PTR(obj)

    View Slide

  161. #define RARRAY_PTR(a) \
    ((RBASIC(a)->flags & RARRAY_EMBED_FLAG) ? \
    RARRAY(a)->as.ary : \
    RARRAY(a)->as.heap.ptr)

    View Slide

  162. break lots of C extensions
    vs.
    non-generational GC

    View Slide

  163. Shady
    Sunny

    View Slide

  164. Shady objects,
    - go into the remembered set
    when shaded
    - do not get promoted in the old
    generation

    View Slide

  165. #define RARRAY_PTR(a) \
    ((VALUE *) \
    RARRAY_CONST_PTR(RGENGC_WB_PROTECTED_ARRAY ?\
    OBJ_WB_UNPROTECT((VALUE)a) : \
    ((VALUE)a)))

    View Slide

  166. incrementally update
    the C extensions

    View Slide

  167. incrementally update
    the virtual machine

    View Slide

  168. I ❤︎ LEGACY

    View Slide

  169. Books!

    View Slide

  170. View Slide

  171. DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS
    DEVELOPERS

    View Slide

  172. View Slide