Your computer has 60 KB of memory free, and a program asks for 35 KB. It is refused. How can 60 be less than 35?
That is not a typo. It is the central problem of giving programs variable-sized blocks, and this lesson shows how it happens.
Splitting a program into segments
With one base and one bounds register (Lesson 6.1) a program gets a single block, including the empty gap between its heap and its stack. A better idea: give the program several blocks, one for each part that has a meaning. These are called segments. A typical program has a code segment, a heap segment and a stack segment (Lesson 2.1), and each has its own base and its own bounds.
An address now has two parts: which segment, and the offset inside it. The hardware looks up the segment’s base and bounds, checks that the offset is below the bounds, and adds the base. The segments can sit anywhere in memory, they can have different sizes, and the empty gap costs nothing because there is no segment for it. A segment can be marked read-only (code) or shared between processes.
Here is a process P with three segments placed in memory. The table under the strip is the segment table: the base (“Start”) and size (“bounds”) of each.
One process, three segments
-
Memory is empty
There are 100 KB of memory and no blocks yet, so there is one hole: all of it. The fit policy is first fit. Allocations are checked against the holes in order of address.
- Free in total
- 100 KB
- Largest hole
- 100 KB
- Holes
- 1
- Allocations refused
- 0
-
P.code gets 20 KB
Holes before: 100 KB at 0. P.code needs 20 KB, and first fit takes the lowest hole that is big enough: 100 KB at 0. So P.code now holds 0 to 19.
- Free in total
- 80 KB
- Largest hole
- 80 KB
- Holes
- 1
- Allocations refused
- 0
-
P.heap gets 30 KB
Holes before: 80 KB at 20. P.heap needs 30 KB, and first fit takes the lowest hole that is big enough: 80 KB at 20. So P.heap now holds 20 to 49.
- Free in total
- 50 KB
- Largest hole
- 50 KB
- Holes
- 1
- Allocations refused
- 0
-
P.stack gets 10 KB
Holes before: 50 KB at 50. P.stack needs 10 KB, and first fit takes the lowest hole that is big enough: 50 KB at 50. So P.stack now holds 50 to 59.
- Free in total
- 40 KB
- Largest hole
- 40 KB
- Holes
- 1
- Allocations refused
- 0
-
P.heap uses offset 5
The hardware checks the offset against the size: 5 is below 30. It adds the block's start: 20 + 5 = 25.
- Free in total
- 40 KB
- Largest hole
- 40 KB
- Holes
- 1
- Allocations refused
- 0
-
Fault: offset 10 is past the end of P.stack
The offset 10 is not below the size 10, so it is outside the block. The hardware traps to the OS and nothing is touched.
- Free in total
- 40 KB
- Largest hole
- 40 KB
- Holes
- 1
- Allocations refused
- 0
-
P.code uses offset 3
The hardware checks the offset against the size: 3 is below 20. It adds the block's start: 0 + 3 = 3.
- Free in total
- 40 KB
- Largest hole
- 40 KB
- Holes
- 1
- Allocations refused
- 0
The same two checks as before, now per segment: offset below the segment’s size, then add the segment’s start. A fault in one segment does not touch the others.
Holes
Segments solved the empty-gap problem, but they have one disadvantage: they have different sizes, and the OS has to find a free hole big enough for each. Blocks are allocated and freed in no particular order. Look at what that does to memory.
Memory here is 100 KB. A, B, C and D are given blocks one after the other, then B and D finish and give theirs back. Then E asks for 35 KB. Watch the picture of the strip, and the number of holes.
Free memory, but no hole big enough
-
Memory is empty
There are 100 KB of memory and no blocks yet, so there is one hole: all of it. The fit policy is first fit. Allocations are checked against the holes in order of address.
- Free in total
- 100 KB
- Largest hole
- 100 KB
- Holes
- 1
- Allocations refused
- 0
-
A gets 20 KB
Holes before: 100 KB at 0. A needs 20 KB, and first fit takes the lowest hole that is big enough: 100 KB at 0. So A now holds 0 to 19.
- Free in total
- 80 KB
- Largest hole
- 80 KB
- Holes
- 1
- Allocations refused
- 0
-
B gets 30 KB
Holes before: 80 KB at 20. B needs 30 KB, and first fit takes the lowest hole that is big enough: 80 KB at 20. So B now holds 20 to 49.
- Free in total
- 50 KB
- Largest hole
- 50 KB
- Holes
- 1
- Allocations refused
- 0
-
C gets 20 KB
Holes before: 50 KB at 50. C needs 20 KB, and first fit takes the lowest hole that is big enough: 50 KB at 50. So C now holds 50 to 69.
- Free in total
- 30 KB
- Largest hole
- 30 KB
- Holes
- 1
- Allocations refused
- 0
-
D gets 20 KB
Holes before: 30 KB at 70. D needs 20 KB, and first fit takes the lowest hole that is big enough: 30 KB at 70. So D now holds 70 to 89.
- Free in total
- 10 KB
- Largest hole
- 10 KB
- Holes
- 1
- Allocations refused
- 0
-
B is freed
B ends and its 30 KB (20 to 49) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are.
- Free in total
- 40 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
D is freed
D ends and its 20 KB (70 to 89) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are. Freed blocks leave holes behind them. A hole next to another hole is one bigger hole, but C is between these two.
- Free in total
- 60 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
E does not fit
Holes: 30 KB at 20, 30 KB at 70. E needs 35 KB in one piece, but the largest hole is 30 KB. In total 60 KB are free, enough for E, but they are scattered in small holes. That is external fragmentation.
- Free in total
- 60 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 1
This is external fragmentation: enough memory is free in total, but it is broken into pieces that are each too small. It gets worse the longer a system runs, because blocks of every size keep coming and going. (Compare internal fragmentation, where memory is wasted inside an allocated block. We meet it in the next lesson.)
Choosing a hole
When several holes are big enough, which one should the OS use? There are three classic policies, which differ in what they leave behind.
- First fit takes the first hole (lowest address) that is big enough. It is fast: it stops looking as soon as it finds one.
- Best fit takes the smallest hole that is big enough. It tries to leave the big holes for big requests, but it tends to leave tiny, useless slivers.
- Worst fit takes the biggest hole, hoping the leftover is still big enough to use.
Ties go to the lowest address. Below, the two holes are 10 KB and 30 KB and F asks for 8 KB, then G asks for 25 KB. The first pane uses first fit; the second is worst fit on exactly the same requests.
First fit against worst fit
-
Memory is empty
There are 100 KB of memory and no blocks yet, so there is one hole: all of it. The fit policy is first fit. Allocations are checked against the holes in order of address.
- Free in total
- 100 KB
- Largest hole
- 100 KB
- Holes
- 1
- Allocations refused
- 0
-
A gets 20 KB
Holes before: 100 KB at 0. A needs 20 KB, and first fit takes the lowest hole that is big enough: 100 KB at 0. So A now holds 0 to 19.
- Free in total
- 80 KB
- Largest hole
- 80 KB
- Holes
- 1
- Allocations refused
- 0
-
B gets 10 KB
Holes before: 80 KB at 20. B needs 10 KB, and first fit takes the lowest hole that is big enough: 80 KB at 20. So B now holds 20 to 29.
- Free in total
- 70 KB
- Largest hole
- 70 KB
- Holes
- 1
- Allocations refused
- 0
-
C gets 30 KB
Holes before: 70 KB at 30. C needs 30 KB, and first fit takes the lowest hole that is big enough: 70 KB at 30. So C now holds 30 to 59.
- Free in total
- 40 KB
- Largest hole
- 40 KB
- Holes
- 1
- Allocations refused
- 0
-
D gets 30 KB
Holes before: 40 KB at 60. D needs 30 KB, and first fit takes the lowest hole that is big enough: 40 KB at 60. So D now holds 60 to 89.
- Free in total
- 10 KB
- Largest hole
- 10 KB
- Holes
- 1
- Allocations refused
- 0
-
E gets 10 KB
Holes before: 10 KB at 90. E needs 10 KB, and first fit takes the lowest hole that is big enough: 10 KB at 90. So E now holds 90 to 99.
- Free in total
- 0 KB
- Largest hole
- 0 KB
- Holes
- 0
- Allocations refused
- 0
-
B is freed
B ends and its 10 KB (20 to 29) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are.
- Free in total
- 10 KB
- Largest hole
- 10 KB
- Holes
- 1
- Allocations refused
- 0
-
D is freed
D ends and its 30 KB (60 to 89) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are.
- Free in total
- 40 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
F gets 8 KB
Holes before: 10 KB at 20, 30 KB at 60. F needs 8 KB, and first fit takes the lowest hole that is big enough: 10 KB at 20. So F now holds 20 to 27. First fit used the small hole for F, and so kept the big one for G. Worst fit cut F out of the big hole, and left holes that are too small for G.
- Free in total
- 32 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
G gets 25 KB
Holes before: 2 KB at 28, 30 KB at 60. G needs 25 KB, and first fit takes the lowest hole that is big enough: 30 KB at 60. So G now holds 60 to 84.
- Free in total
- 7 KB
- Largest hole
- 5 KB
- Holes
- 2
- Allocations refused
- 0
The result depends on the requests, so no policy always wins. Open Try it, pick a different policy, or edit the list of operations. Make a request fail even though plenty of memory is free.
Compaction
If the holes are the problem, why not slide everything together? That is compaction: the OS copies the blocks down to the start of memory, updates each block’s base register, and the holes join into one. Here is the earlier example with a compaction before E asks again.
Compaction
-
Memory is empty
There are 100 KB of memory and no blocks yet, so there is one hole: all of it. The fit policy is first fit. Allocations are checked against the holes in order of address.
- Free in total
- 100 KB
- Largest hole
- 100 KB
- Holes
- 1
- Allocations refused
- 0
-
A gets 20 KB
Holes before: 100 KB at 0. A needs 20 KB, and first fit takes the lowest hole that is big enough: 100 KB at 0. So A now holds 0 to 19.
- Free in total
- 80 KB
- Largest hole
- 80 KB
- Holes
- 1
- Allocations refused
- 0
-
B gets 30 KB
Holes before: 80 KB at 20. B needs 30 KB, and first fit takes the lowest hole that is big enough: 80 KB at 20. So B now holds 20 to 49.
- Free in total
- 50 KB
- Largest hole
- 50 KB
- Holes
- 1
- Allocations refused
- 0
-
C gets 20 KB
Holes before: 50 KB at 50. C needs 20 KB, and first fit takes the lowest hole that is big enough: 50 KB at 50. So C now holds 50 to 69.
- Free in total
- 30 KB
- Largest hole
- 30 KB
- Holes
- 1
- Allocations refused
- 0
-
D gets 20 KB
Holes before: 30 KB at 70. D needs 20 KB, and first fit takes the lowest hole that is big enough: 30 KB at 70. So D now holds 70 to 89.
- Free in total
- 10 KB
- Largest hole
- 10 KB
- Holes
- 1
- Allocations refused
- 0
-
B is freed
B ends and its 30 KB (20 to 49) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are.
- Free in total
- 40 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
D is freed
D ends and its 20 KB (70 to 89) become a hole. If a neighbour is also free, the two holes are one bigger hole. The other blocks stay exactly where they are.
- Free in total
- 60 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 0
-
E does not fit
Holes: 30 KB at 20, 30 KB at 70. E needs 35 KB in one piece, but the largest hole is 30 KB. In total 60 KB are free, enough for E, but they are scattered in small holes. That is external fragmentation.
- Free in total
- 60 KB
- Largest hole
- 30 KB
- Holes
- 2
- Allocations refused
- 1
-
Compaction: slide everything down
The OS copies every block that has a gap below it down to the lowest address, 20 KB in all, and fixes each block's base register. The holes join into one. It works, but the programs must stop while their memory is copied. Only C had to move: A was already at the bottom.
- Free in total
- 60 KB
- Largest hole
- 60 KB
- Holes
- 1
- Allocations refused
- 1
-
E gets 35 KB
Holes before: 60 KB at 40. E needs 35 KB, and first fit takes the lowest hole that is big enough: 60 KB at 40. So E now holds 40 to 74.
- Free in total
- 25 KB
- Largest hole
- 25 KB
- Holes
- 1
- Allocations refused
- 1
Compaction works, but it has a price: copying memory takes time, and the programs whose blocks are moving must be paused while it happens. A machine with a lot of memory in use cannot stop to copy it all, so systems avoid it when they can. The real cure is to stop requiring that a program’s memory be one big piece. If memory were handed out in small fixed-size pieces, any free piece would fit any request, and there would be no external fragmentation at all. That is paging, the subject of the next lesson.
Quick check
Quick check
-
The free memory exists, but it is scattered. The program needs it in one piece.
-
The data really has to be copied to its new place, and the base registers fixed, while the programs are not running.
-
That leaves the larger holes for larger requests. First fit takes the lowest, and worst fit takes the biggest.
-
The size of every piece is the same. A request for more memory takes several pieces, which need not be next to each other.
Recap
- A segment is a block with its own base and bounds. A program uses a segment for its code, one for its heap and one for its stack, and an address is a segment plus an offset.
- Segments can be anywhere in memory and have different sizes. An offset not below the size is a fault.
- Variable-sized blocks leave holes when they are freed. External fragmentation is free memory that is split into pieces too small to use.
- First fit, best fit and worst fit choose which hole to use. None wins every time.
- Compaction joins the holes by copying the blocks, which costs time. Paging avoids the problem with fixed-size pieces.
Next: paging. Memory and programs are cut into equal-sized pieces, and a table says where each one is.
Join the discussion