Docsity

Dynamic Memory Allocation Advanced Concepts, Lecture Slide - Computer Science, Slides of Software Engineering

Partial preview of the text

Download Dynamic Memory Allocation Advanced Concepts, Lecture Slide - Computer Science and more Slides Software Engineering in PDF only on Docsity!

Dynamic Memory Alloca/on:

Advanced Concepts

15-­‐213: Introduc0on to Computer Systems 18 th Lecture, Oct. 26, 2010 Instructors: Randy Bryant and Dave O’Hallaron

Today

 Explicit free lists  Segregated free lists  Garbage collec/on  Memory-­‐related perils and piBalls

Explicit Free Lists

 Maintain list(s) of free blocks, not all blocks  The “next” free block could be anywhere  So we need to store forward/back pointers, not just sizes  S0ll need boundary tags for coalescing  Luckily we track only free blocks, so we can use payload area Size Payload and padding a Size a Size a Size a Next Prev Allocated (as before) Free

Explicit Free Lists

 Logically:  Physically: blocks can be in any order A B C 4 4 4 4 6 6 4 4 4 4 Forward (next) links Back (prev) links A (^) B C

Freeing With Explicit Free Lists

InserAon policy : Where in the free list do you put a newly freed block?  LIFO (last-­‐in-­‐first-­‐out) policy  Insert freed block at the beginning of the free list  Pro: simple and constant 0me  Con: studies suggest fragmenta0on is worse than address ordered  Address-­‐ordered policy  Insert freed blocks so that free list blocks are always in address order: addr(prev) < addr(curr) < addr(next)Con: requires search  Pro: studies suggest fragmenta0on is lower than LIFO

Freeing With a LIFO Policy (Case 1)

 Insert the freed block at the root of the list free( ) Root Root Before A:er conceptual graphic

Freeing With a LIFO Policy (Case 3)

 Splice out successor block, coalesce both memory blocks and insert the new block at the root of the list free( ) Root Root Before A:er conceptual graphic

Freeing With a LIFO Policy (Case 4)

 Splice out predecessor and successor blocks, coalesce all 3 memory blocks and insert the new block at the root of the list free( ) Root Root Before A:er conceptual graphic

Keeping Track of Free Blocks

 Method 1: Implicit list using length—links all blocks  Method 2: Explicit list among the free blocks using pointers  Method 3: Segregated free list  Different free lists for different size classes  Method 4: Blocks sorted by size  Can use a balanced tree (e.g. Red-­‐Black tree) with pointers within each free block, and the length used as a key 5 4 6 2 (^5) 4 6 2

Today

 Explicit free lists  Segregated free lists  Garbage collec/on  Memory-­‐related perils and piBalls

Seglist Allocator

 Given an array of free lists, each one for some size class  To allocate a block of size n :  Search appropriate free list for block of size m > n  If an appropriate block is found:  Split block and place fragment on appropriate list (op0onal)  If no block is found, try next larger class  Repeat un0l block is found  If no block is found:  Request addi0onal heap memory from OS (using sbrk() )  Allocate block of n bytes from this new memory  Place remainder as a single free block in largest size class.

Seglist Allocator (cont.)

 To free a block:  Coalesce and place on appropriate list (op0onal)  Advantages of seglist allocators  Higher throughput  log 0me for power-­‐of-­‐two size classes  Be^er memory u0liza0on  First-­‐fit search of segregated free list approximates a best-­‐fit search of en0re heap.  Extreme case: Giving each block its own size class is equivalent to best-­‐fit.

Today

 Explicit free lists  Segregated free lists  Garbage collec/on  Memory-­‐related perils and piBalls

Implicit Memory Management:

Garbage Collec/on

Garbage collecAon: automa/c reclama/on of heap-­‐allocated storage—applica/on never has to free  Common in func/onal languages, scrip/ng languages, and modern object oriented languages:  Lisp, ML, Java, Perl, Mathema0ca  Variants (“conserva/ve” garbage collectors) exist for C and C++  However, cannot necessarily collect all garbage *void foo() { int p = malloc(128); return; / p block is now garbage / }