Docsity

Garbage Collection: Memory Management Techniques and Algorithms, Study notes of Computer Science

Partial preview of the text

Download Garbage Collection: Memory Management Techniques and Algorithms and more Study notes Computer Science in PDF only on Docsity!

Dynamic Memory Allocation II

Nov 7, 2002

Topics Topics

 Explicit doubly-linked free lists

 Segregated free lists

 Garbage collection

 Memory-related perils and pitfalls

class22.ppt

“The course that gives CMU its Zip!”

Keeping Track of Free Blocks

  Method 1Method 1 : Implicit list using lengths -- links all blocks: Implicit list using lengths -- links all blocks

  Method 2Method 2 : Explicit list among the free blocks using: Explicit list among the free blocks using

pointers within the free blocks pointers within the free blocks

  Method 3Method 3 : Segregated free lists: Segregated free lists

 Different free lists for different size classes

  Method 4Method 4 : Blocks sorted by size (not discussed): Blocks sorted by size (not discussed)

 Can use a balanced tree (e.g. Red-Black tree) with pointers

within each free block, and the length used as a key

Allocating From Explicit Free Lists

free block pred succ free block pred succ Before: After: (with splitting)

Freeing With Explicit Free Lists

Insertion policy Insertion policy : Where in the free list do you put a: Where in the free list do you put a

newly freed block? newly freed block?

 LIFO (last-in-first-out) policy

 Insert freed block at the beginning of the free list  Pro: simple and constant time  Con: studies suggest fragmentation is worse than address ordered.

 Address-ordered policy

 Insert freed blocks so that free list blocks are always in address order » i.e. addr(pred) < addr(curr) < addr(succ)  Con: requires search  Pro: studies suggest fragmentation is better than LIFO

Freeing With a LIFO Policy (cont)

Case 3: f-a-a Case 3: f-a-a

 Splice out prev, coalesce with self, and add to beginning of free list

Case 4: f-a-f Case 4: f-a-f

 Splice out prev and next, coalesce with self, and add to beginning of list p s f self a

before:

p s f a

after:

p1 s f self f

before:

f

after:

p2 s p1 s1 p2 s

Explicit List Summary

Comparison to implicit list: Comparison to implicit list:

 Allocate is linear time in number of free blocks instead of

total blocks -- much faster allocates when most of the

memory is full

 Slightly more complicated allocate and free since needs to

splice blocks in and out of the list

 Some extra space for the links (2 extra words needed for

each block)

Main use of linked lists is in conjunction with Main use of linked lists is in conjunction with

segregated free lists segregated free lists

 Keep multiple linked lists of different size classes, or

possibly for different types of objects

Segregated Storage

Each Each size class size class has its own collection of blockshas its own collection of blocks

 Often have separate size class for every small size (2,3,4,…)

 For larger sizes typically have a size class for each power of 2

Simple Segregated Storage

Separate heap and free list for each size class Separate heap and free list for each size class

No splitting No splitting

To allocate a block of size n: To allocate a block of size n:

 If free list for size n is not empty,

 allocate first block on list (note, list can be implicit or explicit)

 If free list is empty,

 get a new page

 create new free list from all blocks in page

 allocate first block on list

 Constant time

To free a block: To free a block:

 Add to free list  If page is empty, return the page for use by another size (optional)

Tradeoffs: Tradeoffs:

 Fast, but can fragment badly

For More Info on Allocators

D. D. KnuthKnuth,, ““The Art of Computer Programming, SecondThe Art of Computer Programming, Second

Edition Edition””, Addison Wesley, 1973, Addison Wesley, 1973

 The classic reference on dynamic storage allocation

Wilson et al, Wilson et al, ““Dynamic Storage Allocation: A SurveyDynamic Storage Allocation: A Survey

and Critical Review and Critical Review””, Proc. 1995 Int, Proc. 1995 Int ’’ l Workshop onl Workshop on

Memory Management, Memory Management, KinrossKinross, Scotland, Sept, 1995., Scotland, Sept, 1995.

 Comprehensive survey

 Available from CS:APP student site (csapp.cs.cmu.edu)

Implicit Memory Management:

Garbage Collection

Garbage collection Garbage collection:: automatic reclamation of heap-automatic reclamation of heap-

allocated storage -- application never has to free allocated storage -- application never has to free

Common in functional languages, scripting languages, Common in functional languages, scripting languages,

and modern object oriented languages: and modern object oriented languages:

 Lisp, ML, Java, Perl, Mathematica,

Variants (conservative garbage collectors) exist for C Variants (conservative garbage collectors) exist for C

and C++ and C++

 Cannot collect all garbage

void foo() {

int *p = malloc(128);

return; /* p block is now garbage */

Classical GC algorithms

Mark and sweep collection (McCarthy, 1960) Mark and sweep collection (McCarthy, 1960)

 Does not move blocks (unless you also “compact”)

Reference counting (Collins, 1960) Reference counting (Collins, 1960)

 Does not move blocks (not discussed)

Copying collection ( Copying collection (MinskyMinsky, 1963), 1963)

 Moves blocks (not discussed)

For more information, see For more information, see Jones and Lin, Jones and Lin, ““ GarbageGarbage

Collection: Algorithms for Automatic Dynamic Collection: Algorithms for Automatic Dynamic

Memory Memory”” , John Wiley & Sons, 1996., John Wiley & Sons, 1996.

Memory as a Graph

We view memory as a directed graph We view memory as a directed graph

 Each block is a node in the graph  Each pointer is an edge in the graph  Locations not in the heap that contain pointers into the heap are called root nodes (e.g. registers, locations on the stack, global variables)

Root nodes

Heap nodes

Not-reachable

(garbage)

reachable

A node (block) is A node (block) is reachable if there is a path from any root to that node.if there is a path from any root to that node. Non-reachable nodes are Non-reachable nodes are garbage garbage (never needed by the application)(never needed by the application)

Mark and Sweep Collecting

Can build on top of Can build on top of mallocmalloc/free package/free package

 Allocate using malloc until you “run out of space”

When out of space: When out of space:

 Use extra mark bit in the head of each block

 Mark: Start at roots and set mark bit on all reachable memory

 Sweep: Scan all blocks and free blocks that are not marked

Before mark

root

After mark

After sweep

free

Mark bit set

free

Mark and Sweep (cont.)

ptr mark(ptr p) {

if (!is_ptr(p)) return; // do nothing if not pointer

if (markBitSet(p)) return // check if already marked

setMarkBit(p); // set the mark bit

for (i=0; i < length(p); i++) // mark all children

mark(p[i]);

return;

Mark using depth-first traversal of the memory graph Sweep using lengths to find next block

ptr sweep(ptr p, ptr end) {

while (p < end) {

if markBitSet(p)

clearMarkBit();

else if (allocateBitSet(p))

free(p);

p += length(p);