Download Dynamic Memory Allocation - Introduction to Computer Systems - Lecture Slides and more Slides Computer System Design and Architecture in PDF only on Docsity!
Dynamic Memory Allocation
Objectives
Be able to analyze a memory allocator’s
performance
Memory usage efficiency (fragmentation)
Speed of allocation and deallocation operations
Locality of allocations
Robustness
Be able to implement your own efficient
memory allocator (Malloc Project)
Be able to analyze the advantages and
disadvantages of different garbage collector
designs
Memory Allocation
Static size, static allocation
Global variables
Linker allocates final virtual addresses
Executable stores these allocated addresses
Static size, dynamic allocation
Local variables
Compiler directs stack allocation
Stack pointer offsets stored directly in the code
Dynamic size, dynamic allocation
Programmer controlled
Allocated in the heap – how?
Dynamic Memory Allocation
Explicit vs. implicit memory allocator
Explicit: application allocates and frees space
- e.g., malloc and free in C Implicit: application allocates, but does not free space
- e.g., garbage collection in Java or Python
Allocation
In both cases the memory allocator provides an abstraction of memory as a set of blocks Doles out free memory blocks to application
We will first discuss simple explicit memory allocation
Application
Dynamic Memory Allocator
Heap Memory
Malloc Package
#include <stdlib.h>
void *malloc(size_t size)
If successful:
- Returns a pointer to a memory block of at least size bytes, (typically) aligned to 8-byte boundary
- If size == 0, returns NULL If unsuccessful: returns NULL (0) and sets errno
void free(void *ptr)
Returns the block pointed at by ptr to pool of available memory ptr must come from a previous call to malloc or realloc
void *realloc(void *ptr, size_t size)
Changes size of block pointed at by ptr and returns pointer to new block Contents of new block unchanged up to the minimum of the old and new sizes
malloc Example
*void foo(int n, int m) { int i, p;
/ allocate a block of n ints / if ((p = malloc(n * sizeof(int))) == NULL) { perror("malloc"); exit(0); } for (i = 0; i < n; i++) p[i] = i; / add m bytes to end of p block / if ((p = 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]); / return p to available memory pool / free(p); }
Allocation Examples
p1 = malloc(4sizeof(int))*
p2 = malloc(5sizeof(int))*
p3 = malloc(6sizeof(int))*
free(p2)
p4 = malloc(2sizeof(int))*
Constraints
Applications:
Can issue arbitrary sequence of malloc and free requests Free requests must correspond to an allocated block
Allocators
Can’t control number or size of allocated blocks Must respond immediately to all allocation 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 alignment for libc malloc on many systems Can only manipulate and modify free memory Can’t move the allocated blocks once they are allocated
- i.e., compaction is not allowed
Maximizing Throughput
Given some sequence of malloc and free
requests:
R 0 , R 1 , ..., Rk, ... , Rn-
Want to maximize throughput and peak
memory utilization
These goals are often conflicting
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
Maximizing Memory Utilization
Given some sequence of malloc and free
requests:
R 0 , R 1 , ..., Rk, ... , Rn-
Def: Aggregate payload Pk :
malloc(p) results in a block with a payload of p
bytes
After request Rk has completed, the aggregate
payload Pk is the sum of currently allocated
payloads
Def: Current heap size is denoted by Hk
Assume that Hk is monotonically increasing
Def: Peak memory utilization:
After k requests, peak memory utilization is:
External Fragmentation
p4 = malloc(7sizeof(int))*
oops!
Occurs when there is enough aggregate heap memory, but no single free block is large enough
External fragmentation depends on the pattern of future requests, and thus is difficult to measure
p1 = malloc(4sizeof(int))*
p2 = malloc(5sizeof(int))*
p3 = malloc(6sizeof(int))*
free(p2)
Implementation Issues
How do we know how much memory to free just
given a pointer?
How do we keep track of the free blocks?
What do we do with the extra space when
allocating a structure that is smaller than the free
block it is placed in?
How do we pick a block to use for allocation –
many might fit?
How do we reinsert a freed block?
p1 = malloc(1)
p
free(p0)
Keeping Track of Free Blocks
Method 1: Implicit list using lengths – links all blocks
Method 2: Explicit list among the free blocks using
pointers within the free blocks
Method 3: Segregated free list
Different free lists for different size classes
Method 4: Blocks sorted by size
Can use a balanced tree (e.g., Red-Black tree) with pointers within each free block, and the length used as a key
(^5 4 )
(^5 4 )
Method 1: Implicit List
Need to identify whether each block is free or
allocated
Can use extra bit
Bit can be put in the same word as the size if block
sizes are always multiples of two (mask out low
order bit when reading size)
size
1 word
Format of allocated and free blocks
payload
a = 1: allocated block a = 0: free block
size: block size
payload: application data (allocated blocks only)
a
optional padding