Docsity

Garbage Collection: Automatic Memory Management in Programming Languages, Study notes of Computer Science

Partial preview of the text

Download Garbage Collection: Automatic Memory Management in Programming Languages and more Study notes Computer Science in PDF only on Docsity!

Garbage Collection

Marius Nita

(Adapted from Andrew Tolmach’s slides.) [email protected]

 Hoping to cover

^ ^ Introduction to three families of collection algorithms:^ Motivation & basics Copying Collection^ ^ Mark & Sweep^ ^ Reference Counting^

 Advanced issues and topics

 Garbage Collection (GC)

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

^ Typically: How does it work?

(^) 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

two-field records, both fields are pointers to other records. For simplicity, consider a simple heap of “cons cells:” Simple heap model

HEAP

STACK GLOBALS

^ Most straightforward collection strategy. Reference counting

(^) 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.

 Reference counting

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

 Reference counting

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

1.^ Two phases: Mark & Sweep

(^) 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

Initially all cells are on free list. Usestruct cell *free, heap[HEAPSIZE], *roots[ROOTS];};struct cell *c[2];int mark:1; struct cell { M&S: implementation

(^) 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 gc() { M&S: implementation

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 gc() { M&S: implementation

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

 Copying collection

(^) 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,

Copying (cont’d)

ALLOCATION SPACE

RESERVE SPACE ALLOCATION SPACE

(^) RESERVE SPACE

DATA & GARBAGEDATA

DATA

START OF CYCLE:

AFTER COLLECTION:BEFORE COLLECTION:

ALLOCATION SPACE

(^) RESERVE SPACE