Dynamic Memory Allocation Concepts
Dynamic Memory Allocation Concepts
Instructors:
Brian Railing
David Varodayan
Written Assignments
Written 5 and Peer Review 4 is due October 9
Written 5 is double credit
Treat it like an exam
Today
Basic concepts
Implicit free lists
malloc Example
#include <stdio.h>
#include <stdlib.h>
void foo(long n) {
long i, *p;
Highest address
within heap
Lowest address (“the break”, adjustable
within heap by sbrk system call)
Allocation Example
(Conceptual)
p1 = malloc(32)
p2 = malloc(40)
free(p2)
p4 = malloc(16)
Constraints
Applications
Can issue arbitrary sequence of malloc and free requests
free request must be to a malloc’d block
Explicit 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
16-byte (x86-64) alignment on 64-bit systems
Can manipulate and modify only free memory
Can’t move the allocated blocks once they are malloc’d
i.e., compaction is not allowed. Why not?
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
Benchmark Example
Step Command Delta Allocated Peak
1 a 0 9904 9904 9904 9904
Benchmark 2 a 1 50084 50084 59988 59988
syn-array-short 3 a 2 20 20 60008 60008
4 a 3 16784 16784 76792 76792
Trace provided with 5 f 3 -16784 60008 76792
malloc lab 6 a 4 840 840 60848 76792
7 a 5 3244 3244 64092 76792
Allocate & free 10 blocks 8 f 0 -9904 54188 76792
a = allocate 9 a 6 2012 2012 56200 76792
10 f 2 -20 56180 76792
f = free 11 a 7 33856 33856 90036 90036
Bias toward allocate at 12 f 1 -50084 39952 90036
beginning & free at end 13 a 8 136 136 40088 90036
14 f 7 -33856 6232 90036
Blocks number 1–10 15 f 6 -2012 4220 90036
Allocated: Sum of all 16 a 9 20 20 4240 90036
17 f 4 -840 3400 90036
allocated amounts 18 f 8 -136 3264 90036
Peak: Max so far of 19 f 5 -3244 20 90036
Allocated 20 f 9 -20 0 90036
Benchmark Visualization
Step Command Delta Allocated Peak 1
1 a 0 9904 9904 9904 9904
2 a 1 50084 50084 59988 59988
Peak
Allocated
Fragmentation
Poor memory utilization caused by fragmentation
Internal fragmentation
External fragmentation
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
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
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17
Carnegie Mellon
External Fragmentation
Occurs when there is enough aggregate heap memory,
but no single free block is large enough
p1 = malloc(32)
p2 = malloc(40)
p3 = malloc(48)
free(p2)
Implementation Issues
How do we know how much memory to free given just a
pointer?
p0
p0 = malloc(32)
48
Need space
32 48 32 16
for pointers
Method 3: Segregated free list
Different free lists for different size classes
Today
Basic concepts
Implicit free lists
End
Unused Block
Start
of 16/0 32/1 64/0 32/1 8/1
heap
heap_start heap_end
block size
End
Unused Block
Comparing Strategies
32 32 48 16 8
split_block(p, 32)
32 32 32 16 16 8
64 16 32 32 16
32 32 32 16 16 8
free(p) p
32 32 32 16 16 8
malloc(5*SIZ) Yikes!
There is enough contiguous
free space, but the allocator
won’t be able to find it
32 32 32 16 16 8
logically
p
free(p) gone
32 32 48 16 16 1
64 32 16 16 8
logically
p
free(p) gone
64 48 16 16 8
8 32 32 32 32 48 48 32 32 8
Quiz
[Link]
asize
asize
dsize
1 word
64 64 16 32 32 32 32 16
m1 1 m1 1
m1 1 m1 1
n 1 n 0
n 1 n 0
m2 1 m2 1
m2 1 m2 1
m1 1 m1 1
m1 1 m1 1
n 1 n+m2 0
n 1
m2 0
m2 0 n+m2 0
m1 0 n+m1 0
m1 0
n 1
n 1 n+m1 0
m2 1 m2 1
m2 1 m2 1
m1 0 n+m1+m2 0
m1 0
n 1
n 1
m2 0
m2 0 n+m1+m2 0
Heap Structure
Dummy Dummy
Footer Header
Start
of 8/1 16/0 32/1 64/0 32/1 8/1
heap
heap_start heap_end
if (block == NULL)
return NULL;
split_block(block, asize);
return header_to_payload(block);
}
coalesce_block(block);
}
Size a
1 word 1 word
Allocated Free
Block Block
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 52
Carnegie Mellon
block n 11 n 10
being
freed n 10
m2 11 m2 01
next
block
block n 11 n+m2 10
being
freed
m2 10
next
block m2 10 n+m2 10