Lecture Slides 10 102-Memallocation-examples
Lecture Slides 10 102-Memallocation-examples
Memory Allocation
University of Washington
Memory Allocation
University of Washington
Allocation Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
Constraints
Applications
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., blocks can’t overlap
Must align blocks so they satisfy 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., compaction is not allowed. Why not?
Memory Allocation
University of Washington
Throughput:
Number of completed requests per unit time
Example:
5,000 malloc() calls and 5,000 free() calls in 10 seconds
Throughput is 1,000 operations/second
Memory Allocation
University of Washington
Fragmentation
Poor memory utilization is caused by fragmentation
internal fragmentation
external fragmentation
Memory Allocation
University of Washington
Internal Fragmentation
For a given block, internal fragmentation occurs if payload is smaller than
block size
block
Internal Internal
payload
fragmentation fragmentation
Caused by
overhead of maintaining heap data structures (inside block, outside payload)
padding for alignment purposes
explicit policy decisions (e.g., to return a big block to satisfy a small request)
why would anyone do that?
Depends only on the pattern of previous requests
thus, easy to measure
Memory Allocation
University of Washington
External Fragmentation
Occurs when there is enough aggregate heap memory, but no
single free block is large enough
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
Memory Allocation