Title: List Allocation and Garbage Collection
1List Allocation and Garbage Collection
2Memory Allocation
0
1
AVAIL 0 ? (0 0 0 0 0 0 0 0) Initialize Heap
denotes list
0
0
2
1
0
3
2
0
4
3
0
5
4
0
6
5
0
7
6
0
-1
7
3Memory Allocation
0
1
AVAIL 0 (define L (list 1 2 3)) ? (cons 1
(cons 2 (cons 3 ())))
0
0
2
1
0
3
2
0
4
3
0
5
4
0
6
5
0
7
6
0
-1
7
4Memory Allocation
3
-1
AVAIL 1 (define L (list 1 2 3)) ? (cons 1
(cons 2 (cons 3 ())))
0
0
2
1
0
3
2
0
4
3
0
5
4
0
6
5
0
7
6
0
-1
7
5Memory Allocation
3
-1
AVAIL 2 (define L (list 1 2 3)) ? (cons 1
(cons 2 (cons 3 ())))
0
2
0
1
0
3
2
0
4
3
0
5
4
0
6
5
0
7
6
0
-1
7
6Memory Allocation
3
-1
AVAIL 3 ? (0 0 0 0 0) L 2 ? (1 2 3) (define
L (list 1 2 3)) ? (cons 1 (cons 2 (cons 3
())))
0
2
0
1
1
1
2
0
4
3
0
5
4
0
6
5
0
7
6
0
-1
7
7Memory Allocation
3
-1
AVAIL 5 ? (0 0 0), L 2 ? (1 2 3), M 4 ?
(4 5) (define L (list 1 2 3)) (define M (list 4
5))
0
2
0
1
1
1
2
5
-1
3
4
3
4
0
6
5
0
7
6
0
-1
7
8Memory Allocation
3
-1
AVAIL -1 ? NULL, L 2 ? (1 2 3), M 4 ? (4
5) N 7 ? (6 7 8) (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7 8))
0
2
0
1
1
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
9Memory Allocation
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 4 ?
(4 5) N 7 ? (6 7 8) (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) overlap
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
10Memory Allocation
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 7 ? (6 7 8) (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) cells still
accessible
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
11Memory Allocation
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N 0)
cells garbage
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
12Garbage Collection
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) AVAIL NULL ? garbage
collection
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
13Mark
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Mark L
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
14Mark
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Mark L
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
15Mark
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Mark L
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
16Mark
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Mark L
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
17Mark
3
-1
AVAIL -1 ? NULL, L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Mark L
0
2
0
1
4
1
2
5
-1
3
4
3
4
8
-1
5
7
5
6
6
6
7
18Sweep
3
-1
AVAIL 5 ? (0) L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Reclaim unmarked cells
0
2
0
1
4
1
2
5
-1
3
4
3
4
0
-1
5
7
5
6
6
6
7
19Sweep
3
-1
AVAIL 6 ? (0 0) L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Reclaim unmarked cells
0
2
0
1
4
1
2
5
-1
3
4
3
4
0
-1
5
0
5
6
6
6
7
20Sweep
3
-1
AVAIL 7 ? (0 0 0) L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Reclaim unmarked cells
0
2
0
1
4
1
2
5
-1
3
4
3
4
0
-1
5
0
5
6
0
6
7
21Sweep
3
-1
AVAIL 7 ? (0 0 0) L 2 ? ((4 5) 2 3), M 0
int N 0 int (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1)) Unmark cells
0
2
0
1
4
1
2
5
-1
3
4
3
4
0
-1
5
0
5
6
0
6
7
22Memory Allocation
3
-1
AVAIL 6 ? (0 0) L 2 ? ((4 5) 2 3), M 0
int N 7 ? (1) (define L (list 1 2
3)) (define M (list 4 5)) (define N (list 6 7
8)) (set-car! L M) (define M 0) (define N
0) (define N (list 1))
0
2
0
1
4
1
2
5
-1
3
4
3
4
0
-1
5
0
5
6
1
-1
7