List Allocation and Garbage Collection Example
Memory Allocation AVAIL = 0 (0 0 0 0 0 0 0 0) Initialize Heap 1 AVAIL = 0 (0 0 0 0 0 0 0 0) Initialize Heap denotes list 2 1 3 2 4 3 5 4 6 5 7 6 -1 7
Memory Allocation AVAIL = 0 (define L (list 1 2 3)) 1 AVAIL = 0 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7
Memory Allocation AVAIL = 1 (define L (list 1 2 3)) -1 AVAIL = 1 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7
Memory Allocation AVAIL = 2 (define L (list 1 2 3)) -1 AVAIL = 2 (define L (list 1 2 3)) (cons 1 (cons 2 (cons 3 ‘()))) 2 1 3 2 4 3 5 4 6 5 7 6 -1 7
Memory Allocation AVAIL = 3 (0 0 0 0 0) L = 2 (1 2 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 ‘()))) 2 1 1 1 2 4 3 5 4 6 5 7 6 -1 7
Memory Allocation AVAIL = 5 (0 0 0), L = 2 (1 2 3), M = 4 (4 5) -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)) 2 1 1 1 2 5 -1 3 4 3 4 6 5 7 6 -1 7
Memory Allocation AVAIL = -1 NULL, L = 2 (1 2 3), M = 4 (4 5) 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)) 2 1 1 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Memory Allocation AVAIL = -1 NULL, L = 2 ((4 5) 2 3), 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Memory Allocation AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Memory Allocation AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Garbage Collection AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Mark AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Mark AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Mark AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Mark AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Mark AVAIL = -1 NULL, L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] 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 2 1 4 1 2 5 -1 3 4 3 4 8 -1 5 7 5 6 6 6 7
Sweep AVAIL = 5 (0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] -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 2 1 4 1 2 5 -1 3 4 3 4 -1 5 7 5 6 6 6 7
Sweep AVAIL = 6 (0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] -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 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 6 7
Sweep AVAIL = 7 (0 0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] -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 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 7
Sweep AVAIL = 7 (0 0 0) L = 2 ((4 5) 2 3), M = 0 [int] N = 0 [int] -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 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 6 7
Memory Allocation AVAIL = 6 (0 0) L = 2 ((4 5) 2 3), M = 0 [int] -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)) 2 1 4 1 2 5 -1 3 4 3 4 -1 5 5 6 1 -1 7