1
play

1 Allocation Example Assumptions Made in These Slides Memory is - PDF document

Today Basic concepts Dynamic Memory Allocation: Performance concerns Approach 1: implicit free lists Basic Concepts CSci 2021: Machine Architecture and Organization April 24th-27th, 2020 Your instructor: Stephen McCamant Based on


  1. Today  Basic concepts Dynamic Memory Allocation:  Performance concerns  Approach 1: implicit free lists Basic Concepts CSci 2021: Machine Architecture and Organization April 24th-27th, 2020 Your instructor: Stephen McCamant Based on slides originally by: Randy Bryant, Dave O’Hallaron 1 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Dynamic Memory Allocation Dynamic Memory Allocation  Programmers use Application  Allocator maintains heap as collection of variable sized dynamic memory Dynamic Memory Allocator blocks , which are either allocated or free allocators (such as Heap malloc ) to acquire VM  Types of allocators at run time.  Explicit allocator : application allocates and frees space  For data structures whose  E.g., malloc and free in C User stack size is only known at  Implicit allocator: application allocates, but does not free space runtime.  E.g. garbage collection in Java, ML, and Lisp Top of heap  Dynamic memory ( brk ptr) Heap (via malloc ) allocators manage an  Will discuss simple explicit memory allocation today Uninitialized data (. bss ) area of process virtual memory known as the Initialized data ( .data ) heap . Program text ( .text ) 0 3 4 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition malloc Example The malloc Package #include <stdlib.h> #include <stdio.h> #include <stdlib.h> void *malloc(size_t size)  Successful: void foo(int n) { int i, *p;  Returns a pointer to a memory block of at least size bytes aligned to an 8-byte (x86) or 16-byte (x86-64) boundary /* Allocate a block of n ints */ p = (int *) malloc(n * sizeof(int));  If size == 0 , returns NULL if (p == NULL) {  Unsuccessful: returns NULL (0) and sets errno perror("malloc"); exit(0); void free(void *p) }  Returns the block pointed at by p to pool of available memory /* Initialize allocated block */  p must come from a previous call to malloc or realloc for (i=0; i<n; i++) p[i] = i; Other functions  calloc : Version of malloc that initializes allocated block to zero. /* Return allocated block to the heap */  realloc: Changes the size of a previously allocated block. free(p);  sbrk : Used internally by allocators to grow or shrink the heap } 5 6 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1

  2. Allocation Example Assumptions Made in These Slides  Memory is word addressed. p1 = malloc(4)  Words are int-sized. p2 = malloc(5) p3 = malloc(6) Allocated block Free block (4 words) (3 words) Free word Allocated word free(p2) p4 = malloc(2) 7 8 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Constraints Today  Applications  Basic concepts  Can issue arbitrary sequence of malloc and free requests  free request must be to a malloc ’d block  Performance concerns  Approach 1: implicit free lists  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 satisfy all alignment requirements  8-byte (x86) or 16-byte (x86-64) alignment on Linux boxes  Can manipulate and modify only free memory  Can’t move the allocated blocks once they are malloc ’d  i.e ., compaction is not allowed 9 13 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Performance Goal: Peak Memory Utilization Performance Goal: Throughput  Given some sequence of malloc and free requests:  Given some sequence of malloc and free requests:  R 0 , R 1 , ..., R k , ... , R n-1  R 0 , R 1 , ..., R k , ... , R n-1  Def: Aggregate payload P k  malloc(p) results in a block with a payload of p bytes  Goals: maximize throughput and peak memory utilization  After request R k has completed, the aggregate payload P k is the sum of  These goals are often conflicting currently allocated payloads  Def: Current heap size H k  Throughput:  Assume H k is monotonically nondecreasing  Number of completed requests per unit time  i.e., heap only grows when allocator uses sbrk  Example:  Def: Peak memory utilization after k+1 requests  5,000 malloc calls and 5,000 free calls in 10 seconds  U k = ( max i<=k P i ) / H k  Throughput is 1,000 operations/second 14 15 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

  3. Internal Fragmentation Fragmentation  For a given block, internal fragmentation occurs if payload is  Poor memory utilization caused by fragmentation smaller than block size  internal fragmentation: inside a block  external fragmentation: between blocks Block Internal Internal Payload fragmentation fragmentation  Caused by  Overhead of maintaining heap data structures  Padding for alignment purposes  Explicit policy decisions (e.g., to return a big block to satisfy a small request)  Depends only on the pattern of previous requests  Thus, easy to measure 16 17 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition External Fragmentation Implementation Issues  Occurs when there is enough aggregate heap memory,  How do we know how much memory to free given just a but no single free block is large enough pointer? p1 = malloc(4)  How do we keep track of the free blocks? p2 = malloc(5)  What do we do with the extra space when allocating a p3 = malloc(6) structure that is smaller than the free block it is placed in? free(p2)  How do we pick a block to use for allocation -- many Oops! (what would happen now?) p4 = malloc(6) might fit?  Depends on the pattern of future requests  Thus, difficult to measure  How do we reinsert freed block? 18 19 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Knowing How Much to Free Keeping Track of Free Blocks  Method 1: Implicit list using length — links all blocks  Standard method  Keep the length of a block in the word preceding the block.  This word is often called the header field or header 5 4 6 2  Requires an extra word for every allocated block  Method 2: Explicit list among the free blocks using pointers p0 5 4 6 2 p0 = malloc(4) 5  Method 3: Segregated free list  Different free lists for different size classes block size payload  Method 4: Blocks sorted by size free(p0)  Can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key 20 21 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend