In Practice
The following is an example of what happens when a program makes requests for memory. Let's say in this system, the smallest possible block is 64 kilobytes in size, and the upper limit for the order is 4, which results in a largest possible allocatable block, 24 times 64K = 1024K in size. The following shows a possible state of the system after various memory requests.
Step | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K | 64K |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 24 | |||||||||||||||
2.1 | 23 | 23 | ||||||||||||||
2.2 | 22 | 22 | 23 | |||||||||||||
2.3 | 21 | 21 | 22 | 23 | ||||||||||||
2.4 | 20 | 20 | 21 | 22 | 23 | |||||||||||
2.5 | A: 20 | 20 | 21 | 22 | 23 | |||||||||||
3 | A: 20 | 20 | B: 21 | 22 | 23 | |||||||||||
4 | A: 20 | C: 20 | B: 21 | 22 | 23 | |||||||||||
5.1 | A: 20 | C: 20 | B: 21 | 21 | 21 | 23 | ||||||||||
5.2 | A: 20 | C: 20 | B: 21 | D: 21 | 21 | 23 | ||||||||||
6 | A: 20 | C: 20 | 21 | D: 21 | 21 | 23 | ||||||||||
7.1 | A: 20 | C: 20 | 21 | 21 | 21 | 23 | ||||||||||
7.2 | A: 20 | C: 20 | 21 | 22 | 23 | |||||||||||
8 | 20 | C: 20 | 21 | 22 | 23 | |||||||||||
9.1 | 20 | 20 | 21 | 22 | 23 | |||||||||||
9.2 | 21 | 21 | 22 | 23 | ||||||||||||
9.3 | 22 | 22 | 23 | |||||||||||||
9.4 | 23 | 23 | ||||||||||||||
9.5 | 24 |
This allocation could have occurred in the following manner
- The initial situation.
- Program A requests memory 34K, order 0.
- No order 0 blocks are available, so an order 4 block is split, creating two order 3 blocks.
- Still no order 0 blocks available, so the first order 3 block is split, creating two order 2 blocks.
- Still no order 0 blocks available, so the first order 2 block is split, creating two order 1 blocks.
- Still no order 0 blocks available, so the first order 1 block is split, creating two order 0 blocks.
- Now an order 0 block is available, so it is allocated to A.
- Program B requests memory 66K, order 1. An order 1 block is available, so it is allocated to B.
- Program C requests memory 35K, order 0. An order 0 block is available, so it is allocated to C.
- Program D requests memory 67K, order 1.
- No order 1 blocks are available, so an order 2 block is split, creating two order 1 blocks.
- Now an order 1 block is available, so it is allocated to D.
- Program B releases its memory, freeing one order 1 block.
- Program D releases its memory.
- One order 1 block is freed.
- Since the buddy block of the newly freed block is also free, the two are merged into one order 2 block.
- Program A releases its memory, freeing one order 0 block.
- Program C releases its memory.
- One order 0 block is freed.
- Since the buddy block of the newly freed block is also free, the two are merged into one order 1 block.
- Since the buddy block of the newly formed order 1 block is also free, the two are merged into one order 2 block.
- Since the buddy block of the newly formed order 2 block is also free, the two are merged into one order 3 block.
- Since the buddy block of the newly formed order 3 block is also free, the two are merged into one order 4 block.
As you can see, what happens when a memory request is made is as follows:
- If memory is to be allocated
- Look for a memory slot of a suitable size (the minimal 2k block that is larger or equal to that of the requested memory)
- If it is found, it is allocated to the program
- If not, it tries to make a suitable memory slot. The system does so by trying the following:
- Split a free memory slot larger than the requested memory size into half
- If the lower limit is reached, then allocate that amount of memory
- Go back to step 1 (look for a memory slot of a suitable size)
- Repeat this process until a suitable memory slot is found
- If memory is to be freed
- Free the block of memory
- Look at the neighboring block - is it free too?
- If it is, combine the two, and go back to step 2 and repeat this process until either the upper limit is reached (all memory is freed), or until a non-free neighbour block is encountered
Read more about this topic: Buddy Memory Allocation
Famous quotes containing the word practice:
“Know how to be content and you will never be disgraced; practice self-restraint and you will never be in danger.”
—Chinese proverb.
Laozi.
“It is not always possible to predict the response of a doting Jewish mother. Witness the occasion on which the late piano virtuoso Oscar Levant telephoned his mother with some important news. He had proposed to his beloved and been accepted. Replied Mother Levant: Good, Oscar, Im happy to hear it. But did you practice today?”
—Liz Smith (20th century)