Lecture 3: Dynamic Memory Allocation and Garbage Collection
Background Material
Reading
- Chapters 5 (sec. 7) and 8 (sec. 5) of the text
- Additional [optional] reading if desired - Chapter 12 of Aho, Hopcroft,
and Ullman, "Data Structures and Algorithms".
Theme
In functional programming there is no notion of memory: computation proceeds
be returing values and names are just used as shorthand for the values of
expressions since there is no assignment. Thus the computation of a functional
program will produce many values which when implemented must be
stored somewhere. Since values are needed dynamically as the program is
executed memory must be allocated dynamically. If many values are required
memory requirements may become an issue. However, values that are no longer
being used become garbage and in principle could be used again. The process
of collecting memory that is no longer being used is called garbage
collection.
In this lecture we present an overview of techniques for dynamic memory
allocation and garbage collection.
Topics
- cons cells and list allocation (uniform size)
- extent and overlap
- garbage collection (mark and sweep)
- variable size allocation
- First-fit
- Next-fit
- Best-fit
- reference counting
Sample Functions
- extent.scm - recursive function to
the extent of an object. (extent '(1 (2 3) 4)) => 5.
slides
Exercises
- Assume a heap with room of 8 cons cells with the available cell
list initialized to contain all cells.
Trace through memory allocation and garbage collection for
(begin (define L '(1 2 3)) (define M '(4 5)) (define N '(6 7 8))
(set-car! L M) (define M 0) (define N 0) (define N '(1)))
- Given a heap with two available cells of size 1300 and 1200
respectively. Show a sequence of memory request sizes for which
first-fit does not satisfy the request and best-fit does satisfy the
request. Give another sequence where the opposite is true. I.E.
first-fit satisfies the request and best-fit does not satisfy the
request.
Created: April 10, 2008 (revised April 16, 2009) by jjohnson AT cs DOT drexel DOT edu