University of Washington
Today
¢ Dynamic memory alloca on § Size of data structures may only be known at run me § Need to allocate space on the heap § Need to de-allocate (free) unused memory so it can be re-allocated ¢ Implementa on § Implicit free lists § Explicit free lists – subject of next programming assignment § Segregated free lists ¢ Garbage collec on ¢ Common memory-related bugs in C programs
Autumn 2012 Memory Alloca on 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
unini alized data (.bss) ini alized data (.data) program text (.text)
0 Autumn 2012 Memory Alloca on 2 University of Washington Process Memory Image memory protected kernel virtual memory from user code stack %esp
Allocators request addi onal heap memory from the kernel using the sbrk() func on: the “brk” ptr error = sbrk(amt_more) run- me heap (via malloc)
unini alized data (.bss) ini alized data (.data) program text (.text)
0 Autumn 2012 Memory Alloca on 3
University of Washington Dynamic Memory Alloca on
¢ Memory allocator? Applica on § VM hardware and kernel allocate pages § Applica on objects are typically smaller Dynamic Memory Allocator § Allocator manages objects within pages Heap Memory
¢ How should the applica on code allocate memory?
Autumn 2012 Memory Alloca on 4 University of Washington Dynamic Memory Alloca on
¢ Memory allocator? Applica on § VM hardware and kernel allocate pages § Applica on objects are typically smaller Dynamic Memory Allocator § Allocator manages objects within pages Heap Memory
¢ Explicit vs. Implicit Memory Allocator § Explicit: applica on allocates and frees space § In C: malloc() and free() § Implicit: applica on allocates, but does not free space § In Java, ML, Lisp: garbage collec on
¢ Alloca on § A memory allocator doles out memory blocks to applica on § A “block” is a con guous range of bytes of the appropriate size § What is an appropriate size?
Autumn 2012 Memory Alloca on 5
University of Washington Malloc Package
¢ #include
¢ 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 Alloca on 6 University of Washington Malloc Package
¢ #include
¢ 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 Alloca on 7
University of Washington Malloc Package
¢ #include
¢ 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 Alloca on 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 /* 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 free(p); /* return p to available memory pool */ } Autumn 2012 Memory Alloca on 9 University of Washington Assump ons Made in This Lecture ¢ Memory is word addressed (each word can hold a pointer) § block size is a mul ple of words Allocated block Free block (4 words) (3 words) Free word Allocated word Autumn 2012 Memory Alloca on 10 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) Autumn 2012 Memory Alloca on 11 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) Autumn 2012 Memory Alloca on 12 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) Autumn 2012 Memory Alloca on 13 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) Autumn 2012 Memory Alloca on 14 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) Autumn 2012 Memory Alloca on 15 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) Autumn 2012 Memory Alloca on 16 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) Autumn 2012 Memory Alloca on 17 University of Washington Alloca on Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) Autumn 2012 Memory Alloca on 18 University of Washington How are going to implement that?!? ¢ Ideas? Autumn 2012 Memory Alloca on 19 University of Washington Constraints ¢ Applica ons § Can issue arbitrary sequence of malloc() and free() requests § free() requests must be made only for a previously malloc()’d block Autumn 2012 Memory Alloca on 20 University of Washington Constraints ¢ Applica ons § 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 Alloca on 21 University of Washington Constraints ¢ Applica ons § 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 Alloca on 22 University of Washington Constraints ¢ Applica ons § 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 Alloca on 23 University of Washington Constraints ¢ Applica ons § 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 sa sfy all alignment requirements § 8 byte alignment for GNU malloc (libc malloc) on Linux boxes Autumn 2012 Memory Alloca on 24 University of Washington Constraints ¢ Applica ons § 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 sa sfy 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., compac on is not allowed. Why not? Autumn 2012 Memory Alloca on 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 u liza on § These goals are o en conflic ng § What’s throughput? Autumn 2012 Memory Alloca on 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 u liza on § These goals are o en conflic ng ¢ Throughput: § Number of completed requests per unit me § Example: § 5,000 malloc() calls and 5,000 free() calls in 10 seconds § Throughput is 1,000 opera ons/second § How to do malloc() and free() in O(1)? What’s the problem? Autumn 2012 Memory Alloca on 27 University of Washington Performance Goal: Peak Memory U liza on ¢ 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 Alloca on 28 University of Washington Performance Goal: Peak Memory U liza on ¢ 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 Alloca on 29 University of Washington Performance Goal: Peak Memory U liza on ¢ 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 u liza on a er k requests § Uk = ( maxi