Title: Dynamic Memory Allocation
1Dynamic Memory Allocation
2Agenda
- Process Layout
- Memory Allocation Algorithms
- Garbage Collection Algorithms
Reference Ch. 5.7 and 8.5
3Process Layout
lower addresses
executable code (text)
static data (e.g. globals)
heap
stack
higher addresses
4Process Layout
lower addresses
executable code (text)
valid addresses
static data (e.g. globals)
heap
valid addresses
stack
higher addresses
5Process Layout
lower addresses
executable code (text)
rigid
valid addresses
static data (e.g. globals)
rigid
heap
changed with sbrk()
changed by calling and returning from functions
valid addresses
stack
higher addresses
6sbrk()
- include ltunistd.hgt
- Prototype
- void sbrk(ptrdiff_t increment)
- Increases or decreases the address of the bottom
of the heap - Returns the previous address
- sbrk(0) can be used to obtain the current address
- Implementation of sbrk() is operating system
dependent
7Example
- Compiled on tux using gcc, this program produces
the following output - 0x00020908
- 0x00022908
- include ltstdio.hgt
- include ltstdlib.hgt
- include ltunistd.hgt
- int main()
-
- printf("0x08p\n",sbrk(0))
- malloc(1024)
- printf("0x08p\n",sbrk(0))
- return 0
8What happened
- Initially, the heap ended at 0x00020908
executable code (text)
static data (e.g. globals)
0x00020908
stack
9What happened
- Initially, the heap ended at 0x00020908
- Then I said that I needed an additional 1k
executable code (text)
static data (e.g. globals)
0x00020908
stack
10What happened
- Initially, the heap ended at 0x00020908
- Then I said that I needed an additional 1k
- So malloc() increased the heap size by 8k
executable code (text)
static data (e.g. globals)
0x00020908
0x00022908
stack
11Agenda
- Process Layout
- Memory Allocation Algorithms
- Fixed Size
- Variable Size
- First-Fit
- Next-Fit
- Best-Fit
- The Buddy Algorithm
- Garbage Collection Algorithms
12Fixed Size Algorithm
- Maintain a list of free blocks (the free-list)
- To allocate a block, pop one off the front of the
list - To free a block, push it back onto the front of
the list
13Fixed Size Algorithm
- Obtain a large chunk of memory
14Fixed Size Algorithm
- Divide it up into blocks of the appropriate size
15Fixed Size Algorithm
- Give each of the blocks a four-byte header
16Fixed Size Algorithm
front
- Use these headers as next-pointers, and arrange
the blocks into a linked list
17Fixed Size Algorithm
front
- To allocate a block, pop one off the front of the
list, e.g. - p1 alloc()
- p2 alloc()
- p3 alloc()
18Fixed Size Algorithm
front
p1
- To allocate a block, pop one off the front of the
list, e.g. - p1 alloc()
- p2 alloc()
- p3 alloc()
19Fixed Size Algorithm
front
p1
p2
- To allocate a block, pop one off the front of the
list, e.g. - p1 alloc()
- p2 alloc()
- p3 alloc()
20Fixed Size Algorithm
front
p1
p2
p3
- To allocate a block, pop one off the front of the
list, e.g. - p1 alloc()
- p2 alloc()
- p3 alloc()
21Fixed Size Algorithm
front
p1
p2
p3
- To free a block, push it back onto the front of
the list, e.g. - free(p1)
- free(p3)
- free(p2)
22Fixed Size Algorithm
front
p1
p2
p3
- To free a block, push it back onto the front of
the list, e.g. - free(p1)
- free(p3)
- free(p2)
23Fixed Size Algorithm
front
p1
p2
p3
- To free a block, push it back onto the front of
the list, e.g. - free(p1)
- free(p3)
- free(p2)
24Fixed Size Algorithm
front
p1
p2
p3
- To free a block, push it back onto the front of
the list, e.g. - free(p1)
- free(p3)
- free(p2)
25Fixed Size Algorithm
front
Note that the list is not usually kept sorted by
address
26Agenda
- Process Layout
- Memory Allocation Algorithms
- Fixed Size
- Variable Size
- First-Fit
- Next-Fit
- Best-Fit
- The Buddy Algorithm
- Garbage Collection Algorithms
27Variable Size Algorithms
- Maintain a list of free blocks (the free-list)
- To allocate a block, find a block of sufficient
size and remove it from the list - To free a block, insert it into the list, keeping
the list sorted by address
The Buddy Algorithm does not adhere to this
overall scheme.
28Variable Size Algorithms
- Both free and allocated blocks have headers
- The composition of the headers will vary with the
implementation - Example
user data
Free Block
Allocated Block
unused
size
next free block
size
29Variable Size Algorithms
From the memory managers perspective, the block
starts before the headers
user data
Allocated Block
size
From the users perspective, the block starts
after the headers
30Variable Size Algorithms
- Adjacent free blocks must be coalesced
unused
unused
next free block
4k
next free block
8k
unused
12k
next free block
31Finding a block of sufficient size
- At least three approaches
- First-Fit
- Find the first block in the list of sufficient
size - Next-Fit
- Best-Fit
32Variable Size First-Fit
front
16k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
33Variable Size First-Fit
front
14k
2k
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
34Variable Size First-Fit
front
13k
2k
1k
p1
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
35Variable Size First-Fit
front
8k
2k
1k
5k
p1
p3
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
36Variable Size First-Fit
front
7k
2k
1k
5k
1k
p1
p4
p2
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
37Variable Size First-Fit
front
3k
2k
1k
5k
1k
4k
p1
p5
p2
p3
p4
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
38Variable Size First-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
39Variable Size First-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
40Variable Size First-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
41Variable Size First-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p4
p5
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
42Variable Size First-Fit
front
2k
2k
1k
2k
1k
4k
1k
3k
p1
p6
p2
p4
p5
p7
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
43Variable Size First-Fit
front
Coalesced
7k
2k
1k
2k
1k
3k
p1
p6
p2
p4
p5
p7
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
44Variable Size First-Fit
front
7k
1k
1k
2k
1k
3k
1k
p6
p2
p4
p5
p7
p8
p3
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
45Variable Size First-Fit
front
1k
1k
1k
2k
1k
3k
1k
6k
p6
p2
p4
p7
p8
p9
p3
p1
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
46Finding a block of sufficient size
- At least three approaches
- First-Fit
- Next-Fit
- There is an additional pointer into the free-list
called the roving pointer - The search starts at the block pointed to by the
roving pointer - Once a block is found, the roving pointer is
pointed to the next block in the free-list - Best-Fit
47Variable Size Next-Fit
front
rp
16k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
48Variable Size Next-Fit
front
rp
14k
2k
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
49Variable Size Next-Fit
front
rp
13k
2k
1k
p1
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
50Variable Size Next-Fit
front
rp
8k
2k
1k
5k
p1
p3
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
51Variable Size Next-Fit
front
rp
7k
2k
1k
5k
1k
p1
p4
p2
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
52Variable Size Next-Fit
front
rp
3k
2k
1k
5k
1k
4k
p1
p5
p2
p3
p4
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
53Variable Size Next-Fit
front
rp
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
54Variable Size Next-Fit
front
rp
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
55Variable Size Next-Fit
front
rp
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
56Variable Size Next-Fit
front
rp
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p4
p5
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
57Variable Size Next-Fit
front
rp
2k
2k
1k
2k
1k
4k
1k
3k
p1
p6
p2
p4
p5
p7
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
58Variable Size Next-Fit
front
rp
Coalesced
7k
2k
1k
2k
1k
3k
p1
p6
p2
p4
p5
p7
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
59Variable Size Next-Fit
front
rp
7k
2k
1k
1k
1k
3k
1k
p1
p6
p2
p4
p5
p7
p8
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
60Variable Size Next-Fit
front
rp
1k
2k
1k
1k
1k
3k
1k
6k
p1
p6
p2
p4
p7
p8
p9
p3
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
61Finding a block of sufficient size
- At least three approaches
- First-Fit
- Next-Fit
- Best-Fit
- Find the smallest block in the list of sufficient
size
62Variable Size Best-Fit
front
16k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
63Variable Size Best-Fit
front
14k
2k
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
64Variable Size Best-Fit
front
13k
2k
1k
p1
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
65Variable Size Best-Fit
front
8k
2k
1k
5k
p1
p3
p2
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
66Variable Size Best-Fit
front
7k
2k
1k
5k
1k
p1
p4
p2
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
67Variable Size Best-Fit
front
3k
2k
1k
5k
1k
4k
p1
p5
p2
p3
p4
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
68Variable Size Best-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
69Variable Size Best-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
70Variable Size Best-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p3
p4
p5
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
71Variable Size Best-Fit
front
2k
2k
1k
5k
1k
4k
1k
p1
p6
p2
p4
p5
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
72Variable Size Best-Fit
front
2k
2k
1k
5k
1k
1k
1k
3k
p1
p6
p2
p4
p5
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
73Variable Size Best-Fit
front
Coalesced
4k
2k
1k
5k
1k
3k
p1
p6
p2
p4
p5
p3
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
74Variable Size Best-Fit
front
4k
1k
5k
1k
3k
1k
1k
p6
p2
p4
p5
p3
p8
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k)
75Variable Size Best-Fit
front
4k
1k
5k
1k
3k
1k
1k
p6
p2
p4
p5
p3
p8
p1
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- p6 alloc(1k)
- free(p1)
-
- free(p3)
- free(p5)
- p7 alloc(3k)
- free(p6)
- p8 alloc(1k)
- p9 alloc(6k) FAIL!
76Summary of the three approaches
- First-Fit
- Bad if you get a cluster of small blocks at the
front of the free-list - Best-Fit
- Visits every free block during an allocation
- Tends to leave a lot of small blocks in the
free-list - Next-Fit
- Generally considered the best of the three
77Agenda
- Process Layout
- Memory Allocation Algorithms
- Fixed Size
- Variable Size
- First-Fit
- Next-Fit
- Best-Fit
- The Buddy Algorithm
- Garbage Collection Algorithms
78The Buddy Algorithm
- The size of all blocks (both free and allocated)
are powers of 2 - Wastes a lot of space
- But makes the algorithm much faster
79A blocks buddy
- A block may or may not have a buddy
- A block and its buddy are always the same size
- If a block of size 2k resides at binary address
- 0000
- then its buddy (if it has one) resides at binary
address - 1000
k ? 0s
k-1 ? 0s
80The Buddy Algorithm
- There is one free-list for each size of block
- The lists are doubly-linked
- The lists are not kept sorted by address
81The Buddy Algorithm
- To allocated a block of size n
- Determine the smallest integer k such that 2k n
- Find a block of size 2k, and remove the block
from its free-list - If there are no blocks of size 2k
- Find the next larger block, and remove it from
its free-list - Divide the block into two buddies
- Keep one and insert the other into the
appropriate free-list - Continue in this manner until you have a block of
size 2k
82The Buddy Algorithm
- To deallocate a block of size 2k
- Check the blocks buddy
- If they are both free, coalesce them into one
block of size 2k1 - Continue in this manner until you cannot coalesce
any more blocks - Insert the block that you are left with into the
appropriate free-list
83The Buddy Algorithm
Free Block
Allocated Block
user data
unused
allocated?
logarithmic size
allocated?
forward pointer
back pointer
logarithmic size
84The Buddy Algorithm
16k
16k
8k
4k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
85The Buddy Algorithm
16k
8k
8k
4k
4k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
86The Buddy Algorithm
16k
8k
8k
4k
4k
p1
2k
2k
p2
1k
1k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
87The Buddy Algorithm
16k
p3
5k
8k
3k
4k
4k
p1
2k
2k
p2
1k
1k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
88The Buddy Algorithm
16k
p3
5k
8k
3k
4k
4k
p1
2k
2k
p2
p4
1k
1k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
89The Buddy Algorithm
16k
p3
5k
8k
3k
p5
4k
4k
p1
2k
2k
p2
p4
1k
1k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
90The Buddy Algorithm
16k
p3
5k
8k
3k
p5
4k
4k
p1
2k
2k
p4
1k
1k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
91The Buddy Algorithm
16k
p3
5k
8k
3k
p5
4k
4k
p1
2k
2k
2k
1k
Coalesced
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
92The Buddy Algorithm
16k
8k
8k
p5
4k
4k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
93The Buddy Algorithm
16k
8k
p5
p6
4k
4k
3k
1k
4k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
94The Buddy Algorithm
16k
8k
p5
p6
p7
4k
4k
3k
1k
3k
1k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
95The Buddy Algorithm
16k
8k
p6
p7
4k
4k
3k
1k
3k
1k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
96The Buddy Algorithm
16k
8k
p7
4k
4k
4k
3k
1k
p1
2k
2k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
97The Buddy Algorithm
16k
8k
8k
p7
4k
4k
3k
1k
Coalesced
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
98The Buddy Algorithm
16k
16k
8k
Coalesced
4k
2k
1k
- Example
- p1 alloc(2k)
- p2 alloc(1k)
- p3 alloc(5k)
- p4 alloc(1k)
- p5 alloc(4k)
- free(p2)
- free(p4)
-
- free(p3)
- p6 alloc(3k)
- p7 alloc(3k)
- free(p5)
- free(p6)
- free(p1)
- free(p7)
99Agenda
- Process Layout
- Memory Allocation Algorithms
- Garbage Collection Algorithms
- Reference Counting
- Mark-and-Sweep
100Garbage Collection
- Automatically determining at runtime when an
object may be deallocated
101Reference Counting
- Every object is given an additional integer field
- At any point in time, this field holds the total
number of references to the object - When a reference is redirected to the object, the
field is incremented - When a reference is redirected away from the
object, the field is decremented - When the field becomes zero, the object is
deallocated
102Reference Counting
p1
p2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
103Reference Counting
Count
1
p1
f
g
p2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
104Reference Counting
Count
1
Count
1
p1
f
g
p2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
105Reference Counting
Count
1
Count
1
p1
f
g
p2
Count
1
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
106Reference Counting
Count
2
Count
1
p1
f
g
p2
Count
1
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
107Reference Counting
Count
2
Count
1
p1
f
g
p2
Count
2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
108Reference Counting
Count
1
Count
1
p1
f
g
p2
Count
2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
109Reference Counting
Count
0
Count
1
p1
f
g
p2
Count
2
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
110Reference Counting
Count
0
Count
0
p1
f
g
p2
Count
1
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
111Reference Counting
Count
0
Count
0
p1
f
g
p2
Count
1
p3
- Example
- p1 new
- p1-gtf new
- p1-gtg new
- p2 p1
- p3 p1-gtg
- p1 NULL
- p2 NULL
112Reference Counting
p1
- Example
- p1 new
- p1-gtf p1
- p1 NULL
113Reference Counting
Count
1
p1
f
- Example
- p1 new
- p1-gtf p1
- p1 NULL
114Reference Counting
Count
2
p1
f
- Example
- p1 new
- p1-gtf p1
- p1 NULL
115Reference Counting
Count
1
p1
f
- Example
- p1 new
- p1-gtf p1
- p1 NULL
116Reference Counting
Count
1
p1
Will never be deallocated
f
- Example
- p1 new
- p1-gtf p1
- p1 NULL
117Mark-and-Sweep
- Every object is given an additional single bit
- If the bit is set, then the object is said to be
marked - If the bit is not set, then the object is said to
be unmarked - Objects are allocated until all available memory
is exhausted - When this happens, the bits of all objects are
cleared - The registers, stack, and static data are then
traversed to determine which objects are
referenced - When a reference to an object is found, the
object becomes marked - This process is recursive, in that when an object
becomes marked, all of the objects to which it
refers also become marked - Once all accessible objects have been marked, the
objects that are unmarked are deallocated
118Mark-and-Sweep
Mark
Mark
p1
f
g
p2
Mark
p3
Mark
p4
f
Mark
f
Mark
119Mark-and-Sweep
Mark
0
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
120Mark-and-Sweep
Mark
0
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
121Mark-and-Sweep
Mark
1
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
122Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
123Mark-and-Sweep
Mark
1
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
124Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
125Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
126Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
127Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
128Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
129Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
130Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
131Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
132Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
133Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
134Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
135Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
136Problems with Mark-and-Sweep
- Often, the marking process causes a noticeable
pause in execution - Often, Mark-and-Sweep has to be conservative,
treating fields as references, even when they are
not
137Reference Counting vs. Mark-and-Sweep
- Nonetheless, Mark-and-Sweep is generally
considered the superior algorithm - Consider the following program
- include ltgt
- int main()
-
- T p1 new
- T p2 new
- T p3
- for (int i 0 i lt 1000000000
- i)
-
- p3 p1
- p1 p2
- p2 p3
- p3 NULL
-
- return 0