Garbage Collection
Mark and Sweep (with Copy Collection)
Garbage: Example
1: var f = 0;
2: {
3: var x = 1;
4: f = fun(y) {
5: print(x);
6: x = x + 1;
7: };
8: }
9: f(1);
10: f(2);
Line | FP (a) | Heap (h) |
1 | 200 | { 100 : 0, 200: {p : 0, f : 100}} |
3 | 208 | { 100 : 0, 108 : 1, 200 : {p : 0, f : 100}, 208 : {p : 200, x : 108} } |
7 | 208 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
8 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
9 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 124} , 216 : {p : 208} } |
10 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 132:3, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 132}, 216 : {p : 208}, 224 : {p : 208} } |
Garbage: Example
1: var f = 0;
2: {
3: var x = 1;
4: f = fun(y) {
5: print(x);
6: x = x + 1;
7: };
8: }
9: f(1);
10: f(2);
Line | FP (a) | Heap (h) |
1 | 200 | { 100 : 0, 200: {p : 0, f : 100}} |
3 | 208 | { 100 : 0, 108 : 1, 200 : {p : 0, f : 100}, 208 : {p : 200, x : 108} } |
7 | 208 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
8 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
9 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 124} , 216 : {p : 208} } |
10 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 132:3, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 132}, 216 : {p : 208}, 224 : {p : 208} } |
Reference Counting
Reference Counting: Challenges
Cycles
x = {next: None} // object O1
y = {next: x } // object O2
x.next = y;
x = 0;
y = x;
x
y
0
c=2
next:
c = 1
next:
c = 1
data:
c = 1
data:
c = 1
Mark and Sweep
Mark
Garbage: Example
1: var f = 0;
2: {
3: var x = 1;
4: f = fun(y) {
5: print(x);
6: x = x + 1;
7: };
8: }
9: f(1);
10: f(2);
Line | FP (a) | Heap (h) |
1 | 200 | { 100 : 0, 200: {p : 0, f : 100}} |
3 | 208 | { 100 : 0, 108 : 1, 200 : {p : 0, f : 100}, 208 : {p : 200, x : 108} } |
7 | 208 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
8 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 200 : {p : 0, f : 116}, 208 : {p : 200, x : 108} } |
9 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 124} , 216 : {p : 208} } |
10 | 200 | { 100 : 0, 108 : 1, 116 : (208, y, {print(x); x = x + 1} ), 124: 2, 132:3, 200 : {p : 0, f : 116}, 208 : {p : 200, x : 132}, 216 : {p : 208}, 224 : {p : 208} } |
1
2
�x�y�z
S: “Hello world”
�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
R:
R:
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
Mark
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
In MITScript, (as in most scripting languages) the rootset is just the stack of stack frames
(and any other global pointers…e.g., None last slide)
After mark
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
Mark
Sweep
After mark
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
List of allocated objects
1
2
R:�x�y�z
S: “Hello world”
M�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
After mark
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
After sweep
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
Deallocated
Can we do better?
Can we do better? (Answer)
List of allocated objects
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
Halves memory overheard of garbage collector
List of allocated objects
1
2
R:�x�y�z
S: “Hello world”
R:�t
Stack Frame
Vars
x
y
z
Stack
o
o
o
Stack
o
Can we do even better?
An alternative: copying collector
Heap A
Heap B
An alternative: copying collector
Heap A
Heap B
R1
R2
R3
R4
An alternative: copying collector
Heap A
Heap B
R1
R2
R3
R4
R1
R2
R3
R4
An alternative: copying collector
Heap A
Heap B
R1
R2
R3
R4
R1
R2
R3
R4
Object relocation
A
C
A
B
B
C
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Addr: A’
Addr: B’
Addr: C’
Ptr to B
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Addr: A’
Addr: B’
Addr: C’
Ptr to B’
Forward �to B’
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Addr: A’
Addr: B’
Addr: C’
Ptr to B’
Forward �to B’
Ptr to B
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Addr: A’
Addr: B’
Addr: C’
Ptr to B’
Forward �to B’
Ptr to B
Old
New
Addr: A
Addr: B
Addr: C
Ptr to B
Ptr to B
Addr: A’
Addr: B’
Addr: C’
Ptr to B’
Forward �to B’
Ptr to B’
Reachability
Reachability
Tradeoffs
Key issues with GC