CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Inspired by Professor Michelle Mills' lecture on garbage collection, Colorado State University.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
'[(E0)] 'E0
;; Program
'[(define (f x) (lambda (y) x))
(f 2)
(f 10)
(f 5)]
;; Output Value
'(closure E3 (lambda (y) x))
;; Output Memory
`???`)
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
'[(E0)] 'E0
;; Program
'[(define (f x) (lambda (y) x))
(f 2)
(f 10)
(f 5)]
;; Output Value
'(closure E3 (lambda (y) x))
;; Output Memory
'[(E0 . [(f . (closure E0 (lambda (x) (lambda (y) x))))])
(E1 . [E0 (x . 2)])
(E2 . [E0 (x . 10)])
(E3 . [E0 (x . 5)])])
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
;; Input memory
'[(E0)]
;; Env
'E0
;; Program
'[(define (f x) (lambda (y) x))
(f 2)
(f 10)
(f 5)
* (f 99)
* (f 98)]
;; Output Value
'(closure `??` (lambda (y) x))
;; Output Memory
'[(E0 . [(f . (closure E0 (lambda (x) (lambda (y) x))))])
(E1 . [E0 (x . 2)])
(E2 . [E0 (x . 10)])
(E3 . [E0 (x . 5)])
`???`
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
;; Input memory
'[(E0)]
;; Env
'E0
;; Program
'[(define (f x) (lambda (y) x))
(f 2)
(f 10)
(f 5)
* (f 99)
* (f 98)]
;; Output Value
'(closure E5 (lambda (y) x))
;; Output Memory
'[(E0 . [(f . (closure E0 (lambda (x) (lambda (y) x))))])
(E1 . [E0 (x . 2)])
(E2 . [E0 (x . 10)])
(E3 . [E0 (x . 5)])
* (E4 . [E0 (x . 99)])
* (E5 . [E0 (x . 98)])])
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Whenever we call function
fit creates a new function, which allocates a new frame.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Whenever we call function
fit creates a new function, which allocates a new frame.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Whenever we call function
fit creates a new function, which allocates a new frame.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Objective: Discard any memory that will not be used in the future
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
The choice between automatic and manual memory management is a balance between many design constraints:
-> automatic-> manual-> manual-> manualCS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Rust is an example of a new language that introduces manual memory management that is assisted by the compiler, which helps in reducing memory-management code and also enforces correctness. The implementation of this technique is considerably more involved than traditional unsafe memory management.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
- Researchers are experimenting with extending C# with APIs that allow for safe manual memory management (Parkinson et al., 2017), allowing the programmer to get the best of both worlds.
- One of the biggest difficulties of handling automatic memory management is parallelism; concurrent garbage collection is one of the most intricate pieces of technology in programming language development.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
How do we know if a piece of memory will be used in the future? The garbage collector cannot predict the future.
Therefore, garbage collection can only discard memory it can prove cannot be used. Proving that some memory is not needed is done by finding its uses (references).
The garbage collector must be able to iterate over all references in memory. In C any number can be considered a pointer, which makes C garbage collection unsound by definition. The garbage collector must introduce assumptions. For instance, assume that numbers will never be used as references.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
The functions
g_object_ref/g_object_unrefrespectively increase and decrease the reference count. […]The reference count is initialized to one by
g_object_newwhich means that the caller is currently the sole owner of the newly-created reference. When the reference count reaches zero, that is, wheng_object_unrefis called by the last client holding a reference to the object, the dispose and the finalize class methods are invoked.
Source: gnome.org
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Node x = null; Node y = null;
x = new Node(3, null); // `o1: 1`
y = x; // `o1: 2`
x = null; // `o1: 1`
y = x; // `o1: 0`
(define h1-eff (heap-alloc h0 (list 3 null))); `o1:0`
(define h1 (eff-state h1-eff))
(define o1 (eff-result h1-eff))
(define h2 (heap-put h1 x o1)) ; `o1:1`
(define h3 (heap-put h2 y (heap-get x))) ; `o1:2`
(define h4 (heap-put h3 x null)) ; `o1:1`
(define h5 (heap-put h4 y (heap-get x))) ; `o1:0`
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
By inspecting the frames allocated in the heap we can compute the reference count of each handle. We can garbage collect any frame whose reference count is zero. A handle is referenced by a frame if, and only if, the handle is contained in a frame. If
Eis a parent handle in a frame, then it is contained in that frame. IfEis the environment of a closure stored in a local binding of a frame, we say thatEis contained in that frame.
'[(E0 . [(f . (closure `E0` (lambda (x) (lambda (y) x))))])
(E1 . [`E0` (x . 2)])
(E2 . [`E0` (x . 10)])
(E3 . [`E0` (x . 5)])]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
By inspecting the frames allocated in the heap we can compute the reference count of each handle. We can garbage collect any frame whose reference count is zero. A handle is referenced by a frame if, and only if, the handle is contained in a frame. If
Eis a parent handle in a frame, then it is contained in that frame. IfEis the environment of a closure stored in a local binding of a frame, we say thatEis contained in that frame.
'[(E0 . [(f . (closure `E0` (lambda (x) (lambda (y) x))))])
(E1 . [`E0` (x . 2)])
(E2 . [`E0` (x . 10)])
(E3 . [`E0` (x . 5)])]
E0: `4`
E1: 0
E2: 0
E3: 0
We can safely collect
E1,E2, andE3.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
'[(E0)]
'E0
'[
(define (f x)
(define (z a) x)
(lambda (y) (z y)))
(f 0)
(f 10)]
'(closure E2 (lambda (y) (z y)))
'[(E0 . [(f . (closure `E0` (lambda (x) (define (z a) x) (lambda (y) (z y)))))])
(E1 . [`E0` (x . 0) (z . (closure `E1` (lambda (a) x)))])
(E2 . [`E0` (x . 10) (z . (closure `E2` (lambda (a) x)))])])
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(eval-term*?
'[(E0)]
'E0
'[
(define (f x)
(define (z a) x)
(lambda (y) (z y)))
(f 0)
(f 10)]
'(closure E2 (lambda (y) (z y)))
'[(E0 . [(f . (closure `E0` (lambda (x) (define (z a) x) (lambda (y) (z y)))))])
(E1 . [`E0` (x . 0) (z . (closure `E1` (lambda (a) x)))])
(E2 . [`E0` (x . 10) (z . (closure `E2` (lambda (a) x)))])])
E0: 3 E1: 1 E2: 1
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
The heap can represent a graph where we draw a reference-use edge from each key to each handle in the values. If we start Mark-and-sweep from
E0, then we can collectE1,E2, andE3.
'[(`E0` . [(f . (closure `E0` (lambda (x) (lambda (y) x))))])
(E1 . [`E0` (x . 2)])
(E2 . [`E0` (x . 10)])
(E3 . [`E0` (x . 5)])]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
The heap can represent a graph where we draw a reference-use edge from each key to each handle in the values. If we start Mark-and-sweep from
E0, then we can collectE1,E2, andE3.
'[(`E0` . [(f . (closure `E0` (lambda (x) (lambda (y) x))))])
(E1 . [`E0` (x . 2)])
(E2 . [`E0` (x . 10)])
(E3 . [`E0` (x . 5)])]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
If we start Mark-and-sweep from
E0, then we can safely garbage collectE1andE2, as they cannot be reached from any global, that is, we cannot reachE1norE2fromE0.
'[(`E0` . [(f . (closure `E0` (lambda (x) (define (z a) x) (lambda (y) (z y)))))])
(`E1` . [`E0` (x . 0) (z . (closure `E1` (lambda (a) x))) ])
(`E2` . [`E0` (x . 10) (z . (closure `E2` (lambda (a) x))) ])
]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
If we start Mark-and-sweep from
E2, then we can safely garbage collectE1.
'[(`E0` . [(x . 10)])
(`E1` . [`E0` (x . 20) ])
(`E2` . [`E0` (x . 30) ])
]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
*'[(E0 . [(x . 10)])
(E1 . [E0 (x . 20) ])
* (E2 . [E0 (x . 30) ])
]
'[(E0 . [(x . 10)])
(E2 . [E0 (x . 30) ])]
What happens if we allocate some data in the heap above?
(define (heap-alloc h v)
(define new-id (handle (hash-count (heap-data h))))
(define new-heap (heap (hash-set (heap-data h) new-id v)))
(eff new-heap new-id))
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
What happens if we allocate frame
[E0 (x . 9)](some frame without bindings)?
'[(E0 . [(x . 10)])
(E2 . [E0 (x . 30) ])]
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
What happens if we allocate frame
[E0 (x . 9)](some frame without bindings)?
'[(E0 . [(x . 10)])
(E2 . [E0 (x . 30) ])]
'[(E0 . [(x . 10)])
(E2 . [E0 (x . 9) ])]
hash-count is not enough!CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
frame-refs given a frame returns a set of handles contained in that framemem-mark given a function that returns the contained handles of an element,
and an initial handle, returns the set of reachable handles (including the
initial handle).mem-sweep given a heap and a set of handles returns a new heap which
only contains the handles in the given set.CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Given an initial handle, collect the set of reachable handles.
We say that a handle $x$ directly connects to a handle $y$ if handle $y$ is contained in the frame addressed by $x$. We say that a handle is contained in a frame in either situation:
If the frame has a parent, then that handle is contained in the frame.
If a closure is a local value of the frame, and that closure captures handle $x$, then $x$ is contained in the frame.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
Function
frame-refsmust return the set of contained handles.
(check-equal?
(frame-refs
(parse-frame
'(`E2`
(x . 0)
(y . (closure `E0` (lambda (x) x)))
(z . (closure `E1` (lambda (x) x))))))
(set (handle `0`) (handle `1`) (handle `2`)))
(check-equal?
(frame-refs
(parse-frame
; `no parent!`
'((x . 0)
(y . (closure `E0` (lambda (x) x)))
(z . (closure `E1` (lambda (x) x))))))
(set (handle `0`) (handle `1`)))
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(require racket/set) ; <- `do not forget to load the sets library`
(set v1 v2 v3 ...) creates a (possibly empty) set of values, corresponds to $\{ v_1, v_2, v_3, \dots\}$(set-union s1 s2) returns a new set that holds the union of sets s1 and s2, corresponds to $s_1 \cup s_2$(set-add s x) returns a new set that holds the elements of s and also element x, corresponds to $s \cup \{ x \}$(set-subtract s1 s2) returns a new set that consists of all elements that are in s1 but are not in s2, corresponds to $\{ x \mid x \in s_1 \land x \notin s_2\}$CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
(set-member? s x) returns if x is a member of set s, corresponds
to $x \in s$(set->list s) converts set s into a listHow do you iterate over the values of a frame? You might want to look at function
frame-foldor functionframe-values.
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
heap? and set of handlesCS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
heap? and set of handlesfilter. See heap-filter.CS450 ☽ Garbage collection ☽ Lecture 19 ☽ Tiago Cogumbreiro
heap? and set of handlesfilter. See heap-filter.