Dynamic Memory Allocation - PowerPoint PPT Presentation

1 / 137
About This Presentation
Title:

Dynamic Memory Allocation

Description:

Dynamic Memory Allocation – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 138
Provided by: Samue106
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Allocation


1
Dynamic Memory Allocation
2
Agenda
  • Process Layout
  • Memory Allocation Algorithms
  • Garbage Collection Algorithms

Reference Ch. 5.7 and 8.5
3
Process Layout
lower addresses
executable code (text)
static data (e.g. globals)
heap
stack
higher addresses
4
Process Layout
lower addresses
executable code (text)
valid addresses
static data (e.g. globals)
heap
valid addresses
stack
higher addresses
5
Process 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
6
sbrk()
  • 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

7
Example
  • 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

8
What happened
  • Initially, the heap ended at 0x00020908

executable code (text)
static data (e.g. globals)
0x00020908
stack
9
What 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
10
What 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
11
Agenda
  • Process Layout
  • Memory Allocation Algorithms
  • Fixed Size
  • Variable Size
  • First-Fit
  • Next-Fit
  • Best-Fit
  • The Buddy Algorithm
  • Garbage Collection Algorithms

12
Fixed 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

13
Fixed Size Algorithm
  • Obtain a large chunk of memory

14
Fixed Size Algorithm
  • Divide it up into blocks of the appropriate size

15
Fixed Size Algorithm
  • Give each of the blocks a four-byte header

16
Fixed Size Algorithm
front
  • Use these headers as next-pointers, and arrange
    the blocks into a linked list

17
Fixed Size Algorithm
front
  • To allocate a block, pop one off the front of the
    list, e.g.
  • p1 alloc()
  • p2 alloc()
  • p3 alloc()

18
Fixed Size Algorithm
front
p1
  • To allocate a block, pop one off the front of the
    list, e.g.
  • p1 alloc()
  • p2 alloc()
  • p3 alloc()

19
Fixed 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()

20
Fixed 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()

21
Fixed 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)

22
Fixed 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)

23
Fixed 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)

24
Fixed 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)

25
Fixed Size Algorithm
front
Note that the list is not usually kept sorted by
address
26
Agenda
  • Process Layout
  • Memory Allocation Algorithms
  • Fixed Size
  • Variable Size
  • First-Fit
  • Next-Fit
  • Best-Fit
  • The Buddy Algorithm
  • Garbage Collection Algorithms

27
Variable 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.
28
Variable 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
29
Variable 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
30
Variable Size Algorithms
  • Adjacent free blocks must be coalesced

unused
unused
next free block
4k
next free block
8k
unused
12k
next free block
31
Finding 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

32
Variable 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)

33
Variable 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)

34
Variable 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)

35
Variable 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)

36
Variable 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)

37
Variable 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)

38
Variable 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)

39
Variable 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)

40
Variable 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)

41
Variable 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)

42
Variable 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)

43
Variable 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)

44
Variable 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)

45
Variable 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)

46
Finding 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

47
Variable 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)

48
Variable 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)

49
Variable 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)

50
Variable 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)

51
Variable 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)

52
Variable 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)

53
Variable 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)

54
Variable 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)

55
Variable 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)

56
Variable 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)

57
Variable 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)

58
Variable 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)

59
Variable 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)

60
Variable 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)

61
Finding 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

62
Variable 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)

63
Variable 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)

64
Variable 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)

65
Variable 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)

66
Variable 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)

67
Variable 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)

68
Variable 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)

69
Variable 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)

70
Variable 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)

71
Variable 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)

72
Variable 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)

73
Variable 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)

74
Variable 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)

75
Variable 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!

76
Summary 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

77
Agenda
  • Process Layout
  • Memory Allocation Algorithms
  • Fixed Size
  • Variable Size
  • First-Fit
  • Next-Fit
  • Best-Fit
  • The Buddy Algorithm
  • Garbage Collection Algorithms

78
The 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

79
A 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
80
The 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

81
The 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

82
The 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

83
The Buddy Algorithm
Free Block
Allocated Block
user data
unused
allocated?
logarithmic size
allocated?
forward pointer
back pointer
logarithmic size
84
The 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)

85
The 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)

86
The 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)

87
The 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)

88
The 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)

89
The 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)

90
The 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)

91
The 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)

92
The 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)

93
The 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)

94
The 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)

95
The 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)

96
The 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)

97
The 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)

98
The 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)

99
Agenda
  • Process Layout
  • Memory Allocation Algorithms
  • Garbage Collection Algorithms
  • Reference Counting
  • Mark-and-Sweep

100
Garbage Collection
  • Automatically determining at runtime when an
    object may be deallocated

101
Reference 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

102
Reference Counting
p1
p2
p3
  • Example
  • p1 new
  • p1-gtf new
  • p1-gtg new
  • p2 p1
  • p3 p1-gtg
  • p1 NULL
  • p2 NULL

103
Reference 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

104
Reference 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

105
Reference 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

106
Reference 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

107
Reference 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

108
Reference 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

109
Reference 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

110
Reference 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

111
Reference 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

112
Reference Counting
p1
  • Example
  • p1 new
  • p1-gtf p1
  • p1 NULL

113
Reference Counting
Count
1
p1
f
  • Example
  • p1 new
  • p1-gtf p1
  • p1 NULL

114
Reference Counting
Count
2
p1
f
  • Example
  • p1 new
  • p1-gtf p1
  • p1 NULL

115
Reference Counting
Count
1
p1
f
  • Example
  • p1 new
  • p1-gtf p1
  • p1 NULL

116
Reference Counting
Count
1
p1
Will never be deallocated
f
  • Example
  • p1 new
  • p1-gtf p1
  • p1 NULL

117
Mark-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

118
Mark-and-Sweep
Mark

Mark

p1
f
g
p2
Mark

p3
Mark

p4
f
Mark

f
Mark

119
Mark-and-Sweep
Mark
0
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
120
Mark-and-Sweep
Mark
0
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
121
Mark-and-Sweep
Mark
1
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
122
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
123
Mark-and-Sweep
Mark
1
Mark
0
p1
f
g
p2
Mark
0
p3
Mark
0
p4
f
Mark
0
f
Mark
0
124
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
125
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
126
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
127
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
128
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
129
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
130
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
0
p4
f
Mark
0
f
Mark
0
131
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
132
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
133
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
134
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
135
Mark-and-Sweep
Mark
1
Mark
1
p1
f
g
p2
Mark
1
p3
Mark
1
p4
f
Mark
0
f
Mark
0
136
Problems 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

137
Reference 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
Write a Comment
User Comments (0)
About PowerShow.com