Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
^ ^ Introduction to three families of collection algorithms:^ Motivation & basics Copying Collection^ ^ Mark & Sweep^ ^ Reference Counting^
Advanced issues and topics
garbage^ The automatic reclamation of unreachable memory (aka
).
implicitly and complex data structures that are allocatedUniversally used for high level languages with closures
Useful for any language that supports heap allocation.
deleteIt removes the need for explicit deallocation (no more
(^) and (^) free ).
issues once and for all.Let the GC implementor deal with memory corruption
(^) The user program (
mutator
) is linked against a library
known as the
(^) runtime system (RTS)
(^) (e.g. (^) libc ).
In the RTS resides the
(^) memory allocation service
, which
exposes an allocation routine to the user program.
invokes the allocation routine (e.g.When the user program desires more memory, it
(^) malloc
).
The (^) allocation service
(^) may then perform a collection to
returns.free unused memory before the allocation routine
HEAP
STACK GLOBALS
(^) Add a
(^) counter
(^) field to each record.
to it.Increment a record’s counter when taking a new pointer
Decrement it when releasing a pointer to it.
When it reaches
(^0) , put the record on a free list.
When allocating a new record, check the free list first.
mutator waitsof time in which the collector might be running while the^ ^ immediate reclamation of storage: no extended periods^ simple, easy to understand and implement
However,
9-a
mutator waitsof time in which the collector might be running while the^ ^ immediate reclamation of storage: no extended periods^ simple, easy to understand and implement
However, (^) too simple-minded (can’t collect cyclic garbage)in counter operations and checks) (^) speed overhead (every pointer assignment is wrapped (^) space overhead (one integer per record)
9-b
(^) Mark (^) each live record by tracing all pointers starting at
the root.
Sweep (^) unmarked records (garbage) onto a free list,
at the same time.making them available for reuse. Unmark marked cells
termination is guaranteed.Already marked records are ignored in the marking step, so
(^) c[0] (^) to link members of
for (i=0; i < HEAPSIZE-1; i++)^ void init_heap() {free list. free = &(heap[0]);heap[HEAPSIZE-1].c[0] = 0;heap[i].c[0] = &(heap[i+1]); }
void sweep() {
for (i=0; i<ROOTS; i++)
for (i=0; i<HEAPSIZE; i++)
mark(roots[i]);
if (heap[i].mark)
sweep();
/* unmark live data */
}
else {heap[i].mark = 0;
void mark(struct cell *cell)
/* sweep garbage */
{
heap[i].c[0] = free;
if (!cell->mark) {
free = &(heap[i]);
cell->mark = 1;
}
mark(cell->c[0]);
}
} mark(cell->c[1]); }
void sweep() {
for (i=0; i<ROOTS; i++)
for (i=0; i<HEAPSIZE; i++)
mark(roots[i]);
if (heap[i].mark)
sweep();
/* unmark live data */
}
else {heap[i].mark = 0;
void mark(struct cell *cell)
/* sweep garbage */
{
heap[i].c[0] = free;
if (!cell->mark) {
free = &(heap[i]);
cell->mark = 1;
}
mark(cell->c[0]);
}
} mark(cell->c[1]); Notice anything “strange” about}
(^) mark ?
14-a
(^) Divide the heap into
(^) two semi-spaces.
Allocate into one space (the
(^) to-space
When it fills up,
(^) move (^) the (^) live data
(^) to the (^) from-space
and reverse the roles of the two spaces.
have a copying collector for C!)Must reassign all pointers as a consequence. (Can’t
good spatial locality.Inherently compacting – no fragmentation problems,
ALLOCATION SPACE
RESERVE SPACE ALLOCATION SPACE
(^) RESERVE SPACE
DATA & GARBAGEDATA
DATA
START OF CYCLE:
AFTER COLLECTION:BEFORE COLLECTION:
ALLOCATION SPACE
(^) RESERVE SPACE