Dynamic Memory Allocation and Garbage Collection
- Chapters 3 (sec. 2) and 7 (sec. 7-8) of the text
- Additional [optional] reading if desired - Chapter 12 of Aho, Hopcroft,
and Ullman, "Data Structures and Algorithms".
When the amount of memory needed for a computation is not known at compile
time, memory will need to be allocated dynamically. As memory is needed
a sufficiently large chunk of memory is requested (e.g. malloc, new) and
if possible the request is made from system memory called the heap. When
the chunk of memory is no longer needed it can either be explicitly
returned (e.g. free) or it becomes garbage and we can leave it to the
system to reclaim the unused memory when there amount of space in the heap
becomes too small. 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.
- cons cells and list allocation (uniform size)
- extent and overlap
- garbage collection (mark and sweep)
- variable size allocation
- reference counting
Created: April 10, 2008 (revised April 20, 2010) by jjohnson AT cs DOT drexel DOT edu
- 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