Dynamic Memory MGT Class - 2023-24
Dynamic Memory MGT Class - 2023-24
Memory Management
Fundamental purpose of any program is to manipulate data
and storage in computer memory.
Memory is required to store programs into memory before
execution.
When program execution starts it takes memory through OS
and release of memory is return to OS. All these operation is
done by OS.
While designing a program the programmer should
concentrate on memory allocation and release the memory
when it no longer used.
Memory Management
The memory management perform following task:
1. It searches for the requested block of memory and allocate if
available.
2. Supervise the release block of memory
3. Combine tiny block of memory to bigger one.
Chapter 9 12
Variable Block Partitioning: example
First-fit allocation
Begins to scan the memory from starting of memory. This
allocation is fast.
Chooses the block that is big enough size to the request
block size.
This produces the smallest left over block.
This is fast
Dynamic Storage Allocation
2. Best-fit allocation
1. Chooses the block that is closest in requested block
size.
2. It requires more time for searching all the list to find the
free block.
3. Since smallest block is allocated, the smallest fragment
is left.
4. Memory compaction must be done more often
Dynamic Storage Allocation
3. Next-fit allocation
1. The OS searches the memory from the last allocation &
chooses the next available block that is large enough.
2. Results in quick fragmentation of the largest block of
free memory which usually appears at the end of the
memory.
3. Compaction is required to obtain a large block at the end
of memory.
Dynamic Storage Allocation
4. Worst-fit allocation
1. Scans the whole memory to find the biggest block
and allocate them.
2. Results in Block of substantial size are left.
3. Less fragmentation.
First fit allocation
OS
Initial memory P1 12 KB
mapping
<FREE> 10 KB
P2 20 KB
<FREE> 16 KB
P3 6 KB
<FREE> 4 KB
19
first fit
OS OS
P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 KB P4 3 KB
allocated
3KB here by <FREE> 7 KB
arrives FIRST FIT
P2 20 KB P2 20 KB
<FREE> 16 KB <FREE> 16 KB
P3 6 KB P3 6 KB
<FREE> 4 KB <FREE> 4 KB
20
first fit
OS
OS
P1 12 KB
P1 12 P5 of 15 KB
P5 of KB allocated here P4 3 KB
15KB by <FREE> 7 KB
P4 3
arrive KB FIRST FIT
s
<FREE> 7
P2 20 KB
KB
P5 15 KB
P2 20 <FREE> 1 KB
KB
<FREE> 16 P3 6 KB
KB
<FREE> 4 KB
P3 6
KB 21
Best fit Allocation
22
best fit
OS
P1 12
Initial KB
memor
y <FREE> 10
KB
mappin
g
P2 20
KB
<FREE> 16
KB
P3 6 KB
<FREE> 4
23
KB
best fit
OS OS
P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 <FREE> 10 KB
loaded here
3KB KB
by
arrives BEST FIT
P2 20 KB
P2 20 KB
<FREE> 16 KB
<FREE> 16
KB
P3 6 KB
P3 6 KB P4 3 KB
<FREE> 4 <FREE> 1 KB
KB
24
best fit
OS
OS
P1 12 KB
P1 12 KB P5 of 15 KB
loaded here <FREE> 10 KB
P5 of <FREE> 10
15KB KB by
arrives BEST FIT
P2 20 KB
P2 20 KB P5 15 KB
<FREE> 16 <FREE> 1 KB
KB
P3 6 KB
P4 3 KB
P3 6 KB <FREE> 1 KB
P4 3 KB
<FREE> 1 KB 25
worst fit
Worst Fit :
26
worst fit
OS
P1 12 KB
Initial <FREE> 10 KB
memory
mapping
P2 20 KB
<FREE> 16 KB
P3 6 KB
<FREE> 4 KB
27
worst fit
OS OS
P1 12 KB P1 12 KB
P4 of 3KB
P4 of <FREE> 10 <FREE> 10 KB
Loaded
3KB KB
here by
arrives WORST FIT
P2 20 KB
P2 20 KB
P4 3 KB
<FREE> 16
KB <FREE> 13 KB
P3 6 KB
P3 6 KB <FREE> 4 KB
<FREE> 4 KB
28
worst fit
OS
P1 12 KB
No place <FREE> 10 KB
to load
P5 of 15K
P2 20 KB
P4 3 KB
<FREE> 13 KB
P3 6 KB
<FREE> 4 KB
29
worst fit
OS
P1 12 KB
No place to <FREE> 10 KB
load P5 of 15K
P2 20 KB
P4 3 KB
<FREE> 13 KB
Compaction
is needed !! P3 6 KB
<FREE> 4 KB
30
Compaction
External fragmentation can be resolved through
compaction.
Compaction mean to move all allocated block to one side of
memory and all small sizes free block to another side of
memory and merge them into larger block so that a request
can be fulfilled.
Compaction algorithm is expensive, but so is not making
efficient use of memory, especially with a lot of concurrent
processes.
Chapter 9 31
compaction
OS
P1 12 KB
Memory <FREE> 10 KB
mapping
before
compacti
on P2 20 KB
P4 3 KB
<FREE> 13 KB
P3 6 KB
<FREE> 4 KB
32
compaction
OS OS
P1 12 P1 12 KB
KB
P2 20 KB Swap in
P2
Secondary
storage
P2 20 Swap out
KB P4 3 KB
P2
P4 3
KB
P3 6 KB
P3 6
KB
33
compaction
OS
P1 12 KB
P2 20 KB
Secondary
storage
P4 3 KB
Swap out
P4
P3 6 KB
34
compaction
OS
P1 12 KB
P2 20 KB
P4 3 KB
Swap in Secondary
P4 with a storage
different
starting
address
P3 6 KB
35
compaction
OS
P1 12 KB
P2 20 KB
P4 3 KB
Secondary
storage
Swap out
P3
P3 6 KB
36
compaction
OS
P1 12 KB
P2 20 KB
P4 3 KB Swap in
P3
P3 6 KB
Secondary
storage
37
compaction
OS
P1 12 KB
P2 20 KB
P4 3 KB
P3 6 KB
P5 12 KB
<FREE> 12 KB P5 of 15KB is
loaded
38
Garbage collection
The deallocation of unused memory is called garbage
collection. The deallocation is done by executing the specific
function.
The free() in C and delete() in C++ are used to release the
memory. Java have automatic garbage collection.
The garbage collector check the entire memory, marked
unused node and release the associated memory.
Garbage collection
Garbage collection consist of two phases:
Thanks