DOC PREVIEW
Harvey Mudd CS 105 - Dynamic Memory Allocation II

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Dynamic Memory Allocation IIKeeping Track of Free BlocksExplicit Free ListsAllocating From Explicit Free ListsFreeing With Explicit Free ListsFreeing With a LIFO PolicyFreeing With a LIFO Policy (cont)Summary of Explicit ListsSlide 9Segregated StorageSimple Segregated StorageSegregated FitsBuddy AllocatorsFor More Info on AllocatorsImplicit Memory Management: Garbage CollectionGarbage CollectionClassical GC algorithmsMemory as a GraphAssumptions For This LectureMark-and-Sweep CollectingMark-and-Sweep (cont.)Conservative Mark-and-Sweep in CMemory-Related BugsDereferencing Bad PointersReading Uninitialized MemoryOverwriting MemorySlide 27Slide 28Slide 30Referencing Nonexistent VariablesFreeing Blocks Multiple TimesReferencing Freed BlocksFailing to Free Blocks (Memory Leaks)Slide 35Dealing With Memory BugsDealing With Memory Bugs (cont.)Dynamic Memory Allocation IIDynamic Memory Allocation IITopicsTopicsExplicit doubly-linked free listsSegregated free listsGarbage collectionMemory-related perils and pitfallsCS 105Tour of the Black Holes of Computing– 2 –CS 105Keeping Track of Free BlocksKeeping Track of Free BlocksMethod 1Method 1: : Implicit listImplicit list using lengths -- links all blocks using lengths -- links all blocksMethod 2Method 2: : Explicit listExplicit list among the free blocks using among the free blocks using pointers within the free blockspointers within the free blocksMethod 3Method 3: : Segregated free listSegregated free listDifferent free lists for different size classesMethod 4Method 4: : Blocks sorted by size (not discussed)Blocks sorted by size (not discussed)For example balanced tree (Red-Black?) with pointers inside each free block, block length used as key20 16 82420 16 824– 3 –CS 105Explicit Free ListsExplicit Free ListsUse data space for link pointersUse data space for link pointersTypically doubly linkedStill need boundary tags for coalescingLinks aren’t necessarily in same order as blocks!A B C16 16 16 16 2424 1616 16 16Forward linksBack linksABC– 4 –CS 105Allocating From Explicit Free ListsAllocating From Explicit Free Listsfree blockpred succfree blockpred succBefore:After:(with splitting)– 5 –CS 105Freeing With Explicit Free ListsFreeing With Explicit Free ListsInsertion policyInsertion policy: Where in free list to put newly freed : Where in free list to put newly freed block?block?LIFO (last-in-first-out) policyInsert freed block at beginning of free listPro: simple, and constant-timeCon: studies suggest fragmentation is worse than address-orderedAddress-ordered policyInsert freed blocks so list is always in address order»i.e. addr(pred) < addr(curr) < addr(succ) Con: requires search (using boundary tags) Pro: studies suggest fragmentation is better than LIFO– 6 –CS 105Freeing With a LIFO PolicyFreeing With a LIFO PolicyCase 1: a-a-aCase 1: a-a-aInsert self at beginning of free listCase 2: a-a-fCase 2: a-a-fRemove next from free list, coalesce self and next, and add to beginning of free listpred (p) succ (s)selfa ap sselfa fbefore:p sfaafter:– 7 –CS 105Freeing With a LIFO Policy (cont)Freeing With a LIFO Policy (cont)Case 3: f-a-aCase 3: f-a-aRemove prev from free list, coalesce with self, and add to beginning of free listCase 4: f-a-fCase 4: f-a-fRemove prev and next from free list, coalesce with self, and add to beginning of listp sselff abefore:p sf aafter:p1 s1selff fbefore:fafter:p2 s2p1 s1 p2 s2– 8 –CS 105Summary of Explicit ListsSummary of Explicit ListsComparison to implicit lists:Comparison to implicit lists:Allocate is linear-time in number of free blocks instead of total blocks—much faster when most of memory full Slightly more complicated allocate and free since needs to splice blocks in and out of free listSome extra space for links (2 extra words per block)Main use of linked lists is in conjunction with segregated Main use of linked lists is in conjunction with segregated free listsfree listsKeep multiple linked lists of different size classes, or possibly for different types of objects– 9 –CS 105Keeping Track of Free BlocksKeeping Track of Free BlocksMethod 1Method 1: : Implicit listImplicit list using lengths -- links all blocks using lengths -- links all blocksMethod 2Method 2: : Explicit listExplicit list among the free blocks using among the free blocks using pointers within the free blockspointers within the free blocksMethod 3Method 3: : Segregated free listSegregated free listDifferent free lists for different size classesMethod 4Method 4: : Blocks sorted by size (not discussed)Blocks sorted by size (not discussed)For example balanced tree (Red-Black?) with pointers inside each free block, block length used as key20 16 82420 16 824– 10 –CS 105Segregated StorageSegregated StorageEach Each size classsize class has its own collection of blocks has its own collection of blocks4-8121620-3236-64Often separate size class for every small size (8, 12, 16, …)For larger, typically have size class for each power of 2– 11 –CS 105Simple Segregated StorageSimple Segregated StorageSeparate heap and free list for each size classSeparate heap and free list for each size classNo splittingNo splittingTo allocate block of size n:To allocate block of size n:If free list for size n is not empty,Allocate first block on list (can be implicit or explicit)If free list is empty, Get new page Create new free list from all blocks in pageAllocate first block on listConstant timeTo free block:To free block:Add to free listIf page empty, return it for use by another size (optional)Tradeoffs:Tradeoffs:Fast, but can fragment badly– 12 –CS 105Segregated FitsSegregated FitsArray of free lists, one for each size classArray of free lists, one for each size classTo allocate block of size n:To allocate block of size n:Search appropriate list for block of size m > nIf block found, split and put fragment on smaller list (optional)If no block found, try next larger class and repeatIf largest class empty, allocate page(s) big enough to hold desired block, put remainder on appropriate listTo free a block:To free a block:Coalesce and put on appropriate listTradeoffsTradeoffsFaster search than sequential fits (log time for power-of-two size classes)Controls fragmentation of simple segregated storageCoalescing can increase search timesDeferred coalescing can help– 13 –CS


View Full Document

Harvey Mudd CS 105 - Dynamic Memory Allocation II

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Dynamic Memory Allocation II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Dynamic Memory Allocation II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Dynamic Memory Allocation II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?