Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Всеки път

Slide 6

Slide 6 text

03.11.2013

Slide 7

Slide 7 text

I ❤︎ LEGACY

Slide 8

Slide 8 text

@current_year = 2011 ‑︎ @current_year = 2012

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

2.0 & 2.1

Slide 11

Slide 11 text

I ❤︎ LEGACY

Slide 12

Slide 12 text

?

Slide 13

Slide 13 text

Garbage Collection

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Stop-and-Copy 1 hour coding 6 hours debugging

Slide 19

Slide 19 text

Garbage Collection

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

BG PUBLIC ENEMY №1

Slide 22

Slide 22 text

-а/-ът

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

#ътставка

Slide 33

Slide 33 text

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Common misconceptions

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

<=>

Slide 50

Slide 50 text

O(nlgn)

Slide 51

Slide 51 text

cnlgn

Slide 52

Slide 52 text

CPU cache virtual memory*

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

REFERENCE COUNTING MARK & SWEEP MARK & COMPACT COPY

Slide 57

Slide 57 text

Reference Counting

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

roots 1 1 REFERENCE COUNTING

Slide 60

Slide 60 text

roots 1 2 REFERENCE COUNTING

Slide 61

Slide 61 text

roots 1 1 REFERENCE COUNTING

Slide 62

Slide 62 text

roots 1 1 1 REFERENCE COUNTING

Slide 63

Slide 63 text

roots 1 0 1 REFERENCE COUNTING

Slide 64

Slide 64 text

roots 0 1 REFERENCE COUNTING

Slide 65

Slide 65 text

roots 1 REFERENCE COUNTING

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

roots 1 1 REFERENCE COUNTING

Slide 71

Slide 71 text

roots 2 2 REFERENCE COUNTING

Slide 72

Slide 72 text

roots 1 2 REFERENCE COUNTING

Slide 73

Slide 73 text

roots 1 1 Memory Leak REFERENCE COUNTING

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

roots 1 1 1 1 1 1 1 1 1

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

#lolpython

Slide 86

Slide 86 text

Mark & Sweep

Slide 87

Slide 87 text

малко интро

Slide 88

Slide 88 text

header header header heaps

Slide 89

Slide 89 text

header off-heap storage ✘

Slide 90

Slide 90 text

Free list Heap Heap MARK/SWEEP

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

roots MARK/SWEEP

Slide 93

Slide 93 text

roots roots Garbage MARK/SWEEP

Slide 94

Slide 94 text

Heap MARK/SWEEP

Slide 95

Slide 95 text

Heap Fragmentation MARK/SWEEP

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

Mark & Compact

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

MARK/COMPACT

Slide 100

Slide 100 text

MARK/COMPACT

Slide 101

Slide 101 text

STALE POINTERS MARK/COMPACT Active Memory Forwarding Adresses

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

Copy

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

From space To space COPY

Slide 106

Slide 106 text

From space To space COPY

Slide 107

Slide 107 text

From space To space COPY

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

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

Slide 111

Slide 111 text

STOP-THE-WORLD collector mutator

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

PARALLEL collector mutator

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

CONSERVATIVE vs. PRECISE

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

No content

Slide 119

Slide 119 text

Weak generational hypothesis: Most objects die young

Slide 120

Slide 120 text

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

Slide 121

Slide 121 text

Old Generation New Generation roots

Slide 122

Slide 122 text

Old Generation New Generation roots roots

Slide 123

Slide 123 text

Old Generation New Generation roots roots

Slide 124

Slide 124 text

Old Generation New Generation roots roots BUG ☢

Slide 125

Slide 125 text

WRITE BARRIER

Slide 126

Slide 126 text

old ˠ young

Slide 127

Slide 127 text

roots Old Generation New Generation remembered set

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

Reference counting No cycle detection ;(

Slide 130

Slide 130 text

Reference counting Full cycle detection

Slide 131

Slide 131 text

Reference counting Almost full cycle detection

Slide 132

Slide 132 text

__del__ finalizers

Slide 133

Slide 133 text

gc.garbage

Slide 134

Slide 134 text

trololothon

Slide 135

Slide 135 text

Mark and sweep Incremental, conservative (...)

Slide 136

Slide 136 text

Java!

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

4

Slide 139

Slide 139 text

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

Slide 140

Slide 140 text

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

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

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

Slide 143

Slide 143 text

HotSpot JRockit IBM J9 Azul Zing

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

C C++

Slide 147

Slide 147 text

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

Slide 148

Slide 148 text

malloc ! GC_MALLOC realloc ! GC_REALLOC

Slide 149

Slide 149 text

scan the stack find all pointers mark/sweep

Slide 150

Slide 150 text

detect memory leaks

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

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

Slide 153

Slide 153 text

2.0 & 2.1

Slide 154

Slide 154 text

CoW friendly ‐︎ copy-on-write

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

RGenGC

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

RARRAY_PTR(obj)

Slide 161

Slide 161 text

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

Slide 162

Slide 162 text

break lots of C extensions vs. non-generational GC

Slide 163

Slide 163 text

Shady Sunny

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

incrementally update the C extensions

Slide 167

Slide 167 text

incrementally update the virtual machine

Slide 168

Slide 168 text

I ❤︎ LEGACY

Slide 169

Slide 169 text

Books!

Slide 170

Slide 170 text

No content

Slide 171

Slide 171 text

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

Slide 172

Slide 172 text

No content