Memory Allocation
Total Page:16
File Type:pdf, Size:1020Kb
University of Washington Today ¢ Dynamic memory allocaon § Size of data structures may only be known at run 6me § Need to allocate space on the heap § Need to de-allocate (free) unused memory so it can be re-allocated ¢ Implementaon § Implicit free lists § Explicit free lists – subject of next programming assignment § Segregated free lists ¢ Garbage collecon ¢ Common memory-related bugs in C programs Autumn 2012 Memory Alloca7on 1 University of Washington Process Memory Image memory protected kernel virtual memory from user code stack %esp What is the heap for? How do we use it? run-me heap uninialized data (.bss) inialized data (.data) program text (.text) 0 Autumn 2012 Memory Alloca7on 2 University of Washington Process Memory Image memory protected kernel virtual memory from user code stack %esp Allocators request addional heap memory from the kernel using the sbrk() funcon: the “brk” ptr error = sbrk(amt_more) run-me heap (via malloc) uninialized data (.bss) inialized data (.data) program text (.text) 0 Autumn 2012 Memory Alloca7on 3 University of Washington Dynamic Memory Alloca7on ¢ Memory allocator? Applica7on § VM hardware and kernel allocate pages § Applicaon objects are typically smaller Dynamic Memory Allocator § Allocator manages objects within pages Heap Memory ¢ How should the applica4on code allocate memory? Autumn 2012 Memory Alloca7on 4 University of Washington Dynamic Memory Alloca7on ¢ Memory allocator? Applica7on § VM hardware and kernel allocate pages § Applicaon objects are typically smaller Dynamic Memory Allocator § Allocator manages objects within pages Heap Memory ¢ Explicit vs. Implicit Memory Allocator § Explicit: applicaon allocates and frees space § In C: malloc() and free() § Implicit: applicaon allocates, but does not free space § In Java, ML, Lisp: garbage collec6on ¢ Alloca7on § A memory allocator doles out memory blocks to applicaon § A “block” is a con6guous range of bytes of the appropriate size § What is an appropriate size? Autumn 2012 Memory Alloca7on 5 University of Washington Malloc Package ¢ #include <stdlib.h> ¢ void *malloc(size_t size) § Successful: § Returns a pointer to a memory block of at least size bytes (typically) aligned to 8-byte boundary § If size == 0, returns NULL § Unsuccessful: returns NULL (0) and sets errno (a global variable) ¢ Is this enough? That’s it? J Autumn 2012 Memory Alloca7on 6 University of Washington Malloc Package ¢ #include <stdlib.h> ¢ void *malloc(size_t size) § Successful: § Returns a pointer to a memory block of at least size bytes (typically) aligned to 8-byte boundary § If size == 0, returns NULL § Unsuccessful: returns NULL (0) and sets errno (a global variable) ¢ void free(void *p) § Returns the block pointed at by p to the pool of available memory § p must come from a previous call to malloc or realloc ¢ anything_else()? J Autumn 2012 Memory Alloca7on 7 University of Washington Malloc Package ¢ #include <stdlib.h> ¢ void *malloc(size_t size) § Successful: § Returns a pointer to a memory block of at least size bytes (typically) aligned to 8-byte boundary § If size == 0, returns NULL § Unsuccessful: returns NULL (0) and sets errno (a global variable) ¢ void free(void *p) § Returns the block pointed at by p to the pool of available memory § p must come from a previous call to malloc or realloc ¢ void *realloc(void *p, size_t size) § Changes size of block p and returns pointer to new block § Contents of new block unchanged up to min of old and new size § Old block has been free'd (logically, if new != old) Autumn 2012 Memory Alloca7on 8 University of Washington Malloc Example void foo(int n, int m) { int i, *p; /* allocate a block of n ints */ p = (int *)malloc(n * sizeof(int)); if (p == NULL) { Why? perror("malloc"); exit(0); } for (i=0; i<n; i++) p[i] = i; /* add m bytes to end of p block */ if ((p = (int *)realloc(p, (n+m) * sizeof(int))) == NULL) { perror("realloc"); exit(0); } for (i=n; i < n+m; i++) p[i] = i; /* print new array */ for (i=0; i<n+m; i++) printf("%d\n", p[i]); free(p); /* return p to available memory pool */ } Autumn 2012 Memory Alloca7on 9 University of Washington Assump7ons Made in This Lecture ¢ Memory is word addressed (each word can hold a pointer) § block size is a mul6ple of words Allocated block Free block (4 words) (3 words) Free word Allocated word Autumn 2012 Memory Alloca7on 10 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) Autumn 2012 Memory Alloca7on 11 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) Autumn 2012 Memory Alloca7on 12 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) Autumn 2012 Memory Alloca7on 13 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) Autumn 2012 Memory Alloca7on 14 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) Autumn 2012 Memory Alloca7on 15 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) Autumn 2012 Memory Alloca7on 16 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) Autumn 2012 Memory Alloca7on 17 University of Washington Alloca7on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) Autumn 2012 Memory Alloca7on 18 University of Washington How are going to implement that?!? ¢ Ideas? Autumn 2012 Memory Alloca7on 19 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block Autumn 2012 Memory Alloca7on 20 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block ¢ Allocators § Can’t control number or size of allocated blocks Autumn 2012 Memory Alloca7on 21 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block ¢ Allocators § Can’t control number or size of allocated blocks § Must respond immediately to malloc() requests § i.e., can’t reorder or buffer requests Autumn 2012 Memory Alloca7on 22 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block ¢ Allocators § Can’t control number or size of allocated blocks § Must respond immediately to malloc() 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, why? Autumn 2012 Memory Alloca7on 23 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block ¢ Allocators § Can’t control number or size of allocated blocks § Must respond immediately to malloc() 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 sasfy all alignment requirements § 8 byte alignment for GNU malloc (libc malloc) on Linux boxes Autumn 2012 Memory Alloca7on 24 University of Washington Constraints ¢ Applica7ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block ¢ Allocators § Can’t control number or size of allocated blocks § Must respond immediately to malloc() 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 sasfy all alignment requirements § 8 byte alignment for GNU malloc (libc malloc) on Linux boxes § Can’t move the allocated blocks once they are malloc()’d § i.e., compac6on is not allowed. Why not? Autumn 2012 Memory Alloca7on 25 University of Washington Performance Goal: Throughput ¢ Given some sequence of malloc and free requests: § R0, R1, ..., Rk, ... , Rn-1 ¢ Goals: maximize throughput and peak memory u7liza7on § These goals are o^en conflic6ng § What’s throughput? Autumn 2012 Memory Alloca7on 26 University of Washington Performance Goal: Throughput ¢ Given some sequence of malloc and free requests: § R0, R1, ..., Rk, ... , Rn-1 ¢ Goals: maximize throughput and peak memory u7liza7on § These goals are o^en conflic6ng ¢ Throughput: § Number of completed requests per unit 6me § Example: § 5,000 malloc() calls and 5,000 free() calls in 10 seconds § Throughput is 1,000 operaons/second § How to do malloc() and free() in O(1)? What’s the problem? Autumn 2012 Memory Alloca7on 27 University of Washington Performance Goal: Peak Memory U7liza7on ¢ Given some sequence of malloc and free requests: § R0, R1, ..., Rk, ... , Rn-1 ¢ Def: Aggregate payload Pk § malloc(p) results in a block with a payload of p bytes § A^er request Rk has completed, the aggregate payload Pk is the sum of currently allocated payloads Autumn 2012 Memory Alloca7on 28 University of Washington Performance Goal: Peak Memory U7liza7on ¢ Given some sequence of malloc and free requests: § R0, R1, ..., Rk, ... , Rn-1 ¢ Def: Aggregate payload Pk § malloc(p) results in a block with a payload of p bytes § A^er request Rk has completed, the aggregate payload Pk is the sum of currently allocated payloads ¢ Def: Current heap size = Hk § Assume Hk is monotonically nondecreasing § Allocator can increase size of heap using sbrk() Autumn 2012 Memory Alloca7on 29 University of Washington Performance Goal: Peak Memory U7liza7on ¢ Given some sequence of malloc and free requests: § R0, R1, ..., Rk, ... , Rn-1 ¢ Def: Aggregate payload Pk § malloc(p) results in a block with a payload of p bytes § A^er request Rk has completed, the aggregate payload Pk is the sum of currently allocated payloads ¢ Def: Current heap size = Hk § Assume Hk is monotonically nondecreasing § Allocator can increase size of heap using sbrk() ¢ Def: Peak memory uIlizaIon aJer k requests § Uk = ( maxi<k Pi ) / Hk § Goal: maximize u6lizaon for a sequence of requests.