Compilation 2010
Garbage Collection
Jan Midtgaard Michael I. Schwartzbach Aarhus University
Garbage Collection Jan Midtgaard Michael I. Schwartzbach Aarhus - - PowerPoint PPT Presentation
Compilation 2010 Garbage Collection Jan Midtgaard Michael I. Schwartzbach Aarhus University The Garbage Collector A garbage collector is part of the runtime system It reclaims heap-allocated records (objects) that are no longer in use
Compilation 2010
Jan Midtgaard Michael I. Schwartzbach Aarhus University
2
Garbage Collection
3
Garbage Collection
5 10 15 20 25 30 35
MB hours
4
Garbage Collection
5
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
6
Garbage Collection
7
Garbage Collection
function DFS(x) { if (x is a heap pointer) if (x is not marked) { mark x; for (i=1; i<=|x|; i++) DFS(x.fi) } } function Sweep() { p = first address in heap; while (p<last address in heap) { if (p is marked) unmark p; else { p.f1 = freelist; freelist = p; } p = next object pointer after p } } function Mark() { foreach (v in a stack frame) DFS(v); }
8
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
9
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
10
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
11
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
12
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
13
Garbage Collection
p q r 37 15 12 7 37 59 9 20 freelist
00017 00008 00042 00113 00249 00371 00738
14
Garbage Collection
p q r 37 15 12 7 37 59 9 20 freelist
00017 00008 00042 00113 00249 00371 00738
15
Garbage Collection
p q r 37 15 12 7 37 59 9 20 freelist
00017 00008 00042 00113 00249 00371 00738
16
Garbage Collection
p q r 37 15 12 7 37 59 20 freelist
00017 00008 00042 00113 00249 00371 00738
17
Garbage Collection
p q r 37 15 12 7 37 59 20 freelist
00017 00008 00042 00113 00249 00371 00738
18
Garbage Collection
p q r 37 15 12 7 37 59 20 freelist
00017 00008 00042 00113 00249 00371 00738
19
Garbage Collection
p q r 37 15 12 7 37 59 20 freelist
00017 00008 00042 00113 00249 00371 00738
20
Garbage Collection
p q r 37 15 12 7 37 59 20 freelist
00017 00008 00042 00113 00249 00371 00738
21
Garbage Collection
22
Garbage Collection
23
Garbage Collection
24
Garbage Collection
25
Garbage Collection
function Increment(x) { x.count++; } function Decrement(x) { x.count--; if (x.count==0) PutOnFreeList(x); } function PutOnFreelist(x) { Decrement(x.f1); x.f1 = freelist; freelist = x; } function RemoveFromFreelist(x) { for (i=2; i<=|x|; i++) Decrement(x.fi); }
26
Garbage Collection
27
Garbage Collection
8 7 6 4 3 5
from-space to-space next limit
8 7 6 5 4 3
to-space from-space limit next
28
Garbage Collection
function Forward(x) { if (x from-space) { if (x.f1 to-space) return x.f1; else for (i=1; i<|x|; i++) next.fi = x.fi; x.f1 = next; next = next + sizeof(x); return x.f1; } else return x; } function Copy() { scan = next = start of to-space; foreach (v in a stack frame) v = Forward(v); while (scan < next) { for (i=1; i<=|scan|; i++) scan.fi = Forward(scan.fi); scan = scan + sizeof(scan); } }
29
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
30
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
31
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
32
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
33
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
34
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
35
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
36
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
37
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
20
00249
38
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
20
00936
39
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
20
00936
59
00948
40
Garbage Collection
p q r 37 15 12 7 37 59 9 20
00017 00008 00042 00113 00249 00371 00738
15
00017
15
09000
37
09012
12
09024
20
00936
59
00948
41
Garbage Collection
p q r 37
15
09000
37
09012
12
09024
20
00936
59
00948
42
Garbage Collection
43
Garbage Collection
44
Garbage Collection
45
Garbage Collection
46
Garbage Collection
47
Garbage Collection
48
Garbage Collection
49
Garbage Collection
50
Garbage Collection
51
Garbage Collection
52
Garbage Collection
53
Garbage Collection
54
Garbage Collection
55
Garbage Collection
SoftReference sr; ... Image img = (sr == null)? null : sr.get() ; if (img == null) { img = getImage("huge.gif"); sr = new SoftReference(img); } display(img); img = null;
56
Garbage Collection