Escolar Documentos
Profissional Documentos
Cultura Documentos
Management – Part 1
CPT212 – Design & Analysis of Algorithms
Learning Outcomes
• In this topic, we will learn about
–Memory management
–Sequential-fit methods
Heap:
Unused memory
6 3 10 15 14 4
Link Link Link Link Link Link
Link Link Link Link Link Link
Allocates
the first Best-fit Next-fit
memory
block large
enough to Finds the largest block on the list
meet the so that the remaining parts are
request large enough to be used later on
3 12 25 29 34 47
01 3 12 15 23 25 29 34 47 49
1 2
1 2 3 4
1 2 3 4
Lastref 1
Blocks
1 2 3 4
Lastref 3 2
Blocks
1 2 3 4
Lastref 3 2
Blocks
1 2 3 4
Lastref 3 4
Blocks
1 2 3 4
1 2 3 4
Lastref 13 4
Blocks
1 2 5 4
return b;
Possible Solutions
Compact memory after a Liquidate and renew the
certain number of size-list after a certain
allocations/deallocations period
Status 1
indicates
the block is
reserved
Example (𝑚 = 7)
𝑏𝑙𝑜𝑐𝑘 = avail[7];
detach 𝑏𝑙𝑜𝑐𝑘 from list avail[7]
while (𝑟𝑜𝑢𝑛𝑑𝑒𝑑𝑆𝑖𝑧𝑒 = 5 < availSize = 6)
𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒 − −;
∴ 𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒 = 5
𝑏𝑙𝑜𝑐𝑘 = left half of 𝑏𝑙𝑜𝑐𝑘;
insert buddy of 𝑏𝑙𝑜𝑐𝑘 in list avail[5]
𝑏𝑙𝑜𝑐𝑘 = avail[7];
detach 𝑏𝑙𝑜𝑐𝑘 from list avail[7]
while (𝑟𝑜𝑢𝑛𝑑𝑒𝑑𝑆𝑖𝑧𝑒 = 5 < availSize = 5)
return block;
reserve(17)
𝑟𝑜𝑢𝑛𝑑𝑒𝑑𝑆𝑖𝑧𝑒=⌈log 2 17 ⌉=5;
𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒 = min(5,6,7) where avail[𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒]>-1=6;
if no such 𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒 exists
return failure;
𝑏𝑙𝑜𝑐𝑘 = avail[6];
detach 𝑏𝑙𝑜𝑐𝑘 from list avail[𝟔]
while (𝟓 < 𝟔)
𝑎𝑣𝑎𝑖𝑙𝑆𝑖𝑧𝑒 −-;
𝑏𝑙𝑜𝑐𝑘 = left half of 𝑏𝑙𝑜𝑐𝑘;
insert buddy of 𝑏𝑙𝑜𝑐𝑘 in list avail[𝟓]
return 𝑏𝑙𝑜𝑐𝑘;
Buddy is free
again, coalesce
one more time
Advantage: Disadvantage:
Causes internal fragmentation
due to the rounding process to
the nearest larger power of 2
mark(𝑟𝑜𝑜𝑡)
if markedBit(𝑟𝑜𝑜𝑡) = 𝑓𝑎𝑙𝑠𝑒 then
markedBit(𝑟𝑜𝑜𝑡) = 𝑡𝑟𝑢𝑒
for each 𝑣 referenced by 𝑟𝑜𝑜𝑡
mark(𝑣)
• Downsides
–Increased garbage collection duration
𝑙𝑜 ℎ𝑖
T T F T F F
𝑙𝑜 ℎ𝑖
T T F T F F
𝑙𝑜 ℎ𝑖
T T F
T T
F F F
p is
No unchanged Yes
and in
Request for p over cache?
the internet and store Load p from
in cache. cache.