15 213 Introduction to Computer Systems Memory Management I Dynamic Storage Allocation Oct 8 1998 Topics User level view Policies Mechanisms class14 ppt Harsh Reality Memory 3Matters Memory is not unbounded It must be allocated and managed Many applications are memory dominated Especially those based on complex graph algorithms Memory referencing bugs especially pernicious Effects are distant in both time and space Memory performance is not uniform Cache and virtual memory effects can greatly affect program performance Adapting program to characteristics of memory system can lead to major speed improvements class14 ppt 2 CS 213 F 98 Dynamic Storage Allocation Application Dynamic Storage Allocator Heap Memory Application Requests and frees contiguous blocks of memory Allocator e g Unix malloc package Provides an abstraction of memory as a set of blocks Doles out free memory blocks to application Keeps track of free and allocated blocks Heap memory region starting after bss segment class14 ppt 3 CS 213 F 98 Process memory image Reserved for kernel Alpha 0xffff ffff ffff ffff 0xffff fc00 0000 0000 0xffff fbff ffff ffff 0x0000 0400 0000 0000 0x0000 03ff ffff ffff 0x0000 03ff 8000 0000 0x0000 03ff 7fff ffff Not accessible Reserved for shared libraries and dynamic loader Available for heap Heap via malloc Grows up Bss segment gp 0x0000 0001 2000 0000 0x0000 0000 1fff ffff sp 0x0000 0000 0001 0000 0x0000 0000 0000 ffff 0x0000 0000 0000 0000 class14 ppt Data segment Text segment Stack Grows down to zero Available for stack Not accessible by convention 64KB 4 CS 213 F 98 Malloc void malloc int size package if successful returns 8 byte aligned pointer to memory block of at least size bytes is size 0 returns NULL if unsuccessful returns NULL void free void p returns block pointed at by p to pool of available memory p must come from a previous call to malloc class14 ppt 5 CS 213 F 98 Definitions and assumptions Heap Memory fixed size Relative Address word address 0 4 8 Allocated block 4 words 12 Free block 3 words Program Primitives Allocation request Ai n Allocate n words and call it block i Example A7 128 Free request Fj Free block j Example F7 class14 ppt 6 CS 213 F 98 Allocation example A1 4 A2 5 A3 6 F2 A4 2 class14 ppt 7 CS 213 F 98 Applications Constraint s Can issue arbitrary sequence of allocation and free requests Free requests must correspond to an allocated block Allocators Can t control number or size of allocated blocks Must respond immediately to all allocation requests i e can t reorder or buffer requests Must allocate blocks from free memory i e can only place allocated blocks in free memory Must align blocks so they satisfy all alignment requirements usually 8 byte alignment Can only manipulate and modify free memory Can t move the allocated blocks once they are allocated i e compaction is not allowed class14 ppt 8 CS 213 F 98 Fragmentation A1 4 A2 5 A3 6 F2 A4 6 class14 ppt oops 9 CS 213 F 98 Fragmentation cont Def external fragmentation is the inability to reuse free memory possible because applications can free blocks in any order potentially creating holes Minimizing fragmentation is the fundamental problem of dynamic resource allocation Unfortunately there is no good operational definition Function of Number and sizes of holes Placement of allocated blocks Past program behavior pattern of allocates and frees Future program behavior class14 ppt 10 CS 213 F 98 Fragmentation cont A B C Which heaps have a fragmentation problem It depends Qualitatively C has fewer and bigger holes But fragmentation occurs only if program needs a large block Still C is probably less likely to encounter problems Definitive answer requires a model of program execution class14 ppt 11 CS 213 F 98 Fragmentation cont First Fit A1 1 A2 2 A3 4 Oops The policy for placing allocated blocks has a big impact on fragmentation class14 ppt 12 CS 213 F 98 Fragmentation cont Best Fit A1 1 A2 2 A3 4 But best fit doesn t always work best either class14 ppt 13 CS 213 F 98 Fragmentation cont Best Fit A1 1 A2 2 A3 2 A4 2 oops class14 ppt 14 CS 213 F 98 Fragmentation cont First Fit A1 1 A2 2 A3 2 A4 2 class14 ppt 15 CS 213 F 98 Splitting A1 1 1 Find a free block that is big enough 2 Split the block into two free blocks 3 Allocate the first block class14 ppt 16 CS 213 F 98 Coalescing 2 F2 1 free the block 2 merge any adjacent free blocks into a single free block Crucial operation for any dynamic storage allocator Can be immediate performed at every free request deferred performed every k free requests or when necessary class14 ppt 17 CS 213 F 98 Organizing the set of free blocks Some data structure needed to organize the search of the free blocks Efficient implementations use the free blocks themselves to hold the necessary link fields disadvantage every allocated block must be large enough to hold the link fields since the block could later be freed imposes a minimum block size could result in wasted space internal fragmentation Common approach list of free blocks embedded in an array of allocated blocks class14 ppt 18 CS 213 F 98 Organizing the set of free blocks address ordering no memory overhead doubly linked list of free blocks simple popular reasonable memory overhead might not scale to large sets of free blocks tree structures more scalable less memory efficient than lists segregated free lists different free lists for different size classes of free blocks internal fragmentation class14 ppt 19 CS 213 F 98 Placement policies When a block is allocated we must search the free list for a free block that is large enough to satisfy the request feasible free block Placement policy determines which feasible free block to choose A1 1 Each of these free blocks is feasible Where do we place the allocated block class14 ppt 20 CS 213 F 98 Placement policies cont first fit search list from beginning choose first free block that fits simple and popular can increase search time because splinters can accumulate near the front of the list simplicity lends itself to tight inner loop might not scale well for large free lists Search starts here A1 1 First fit chooses this block class14 ppt 21 CS 213 F 98 Placement policies cont best fit choose free block that fits the best motivation is to try to keep fragments what s left over after splitting as small as possible can backfire if blocks almost fit but not quite Search starts here A1 1 Best fit chooses this block class14 ppt 22 CS 213 F 98 Placement policies cont next fit
View Full Document