Professional Documents
Culture Documents
SYSTEM
MODULE 3
MEMORY MANAGEMENT
Upon completion of this module, the student would be able to:
• Define memory management;
• Discuss the concept of address binding;
• Differentiate logical and physical address space;
• Apply memory management techniques in solving memory
management problems;
Memory management is the functionality of an operating system
which handles or manages primary memory and moves processes
back and forth between main memory and disk during execution.
Compiler or compile
Assembler time
Object
Other Module
Object
Modules
Linkage
Editor
Load load
Module time
System
Library
Loader
Dynamically
Loade d
Sys te m
Library
E.F. = 4K + 4K
= 8K
Therefore:
Memory Utilization = (total process size
inside partitions / total memory available)
= (4K + 6K)/22K x 100
= 45.5%
Using FIRST-FIT algorithm, only
Jobs 1 and 2 can enter memory at What if the system partitions
Partitions 1 and 2. memory as 10:8:4 or 7:3:6:6?
During this time:
I.F. = (8K-6K)+(6K-6K)+(4K-4K)+(4K-2K)
Example: = 4K
E.F. = 0
(since all partitions are containing
processes)
Therefore:
Memory Utilization = (total process size
inside partitions / total memory available)
= (6K + 6K + 4K + 2K)/22K x 100
Using BEST-FIT algorithm, ALL = 81.81%
JOBS can be allocated to all
partitions. What if the system partitions memory
as 10:8:4 or 7:3:6:6?
• In Multiple Variable Partition Technique (MVT), the
system allows the region sizes to vary dynamically. It is
therefore possible to have a variable number of tasks in
memory simultaneously.
• Initially, the OS views memory as one large block of
available memory called a hole. When a job arrives and
needs memory, the system searches for a hole large enough
for this job. If one exists, the OS allocates only as much as
is needed, keeping the rest available to satisfy future
requests.
• Assume that the memory size is Example:
256K with the OS residing at the first
Run
40K memory locations. Job Memory
Time
1 60K 10ms
• Assume further that the following
2 100K 5ms
jobs are in the job queue:
3 30K 20ms
• The system again follows the FCFS 4 70K 8ms
algorithm in scheduling processes. 5 50K 15ms
0 0 0
• Assume that all jobs OS OS OS
time Job 5
Job 1 Job 1 Job 1 out, Job 5 in after
next 5 time units
90K
Run 100K 100K 100K
Job Memory
Time
1 60K 10ms Job 4 Job 4
Job 2 out, Job 4 in after
2 100K 5ms Job 2 5 time units
4 70K 8ms
200K 200K 200K
5 50K 15ms
Job 3 Job 3 Job 3
Note that using FIRST-FIT algorithm, the last job finished at 25ms.
This example illustrates several points about :
1. In general, there is at any time a set of holes, of various
sizes, scattered throughout memory.
2. When a job arrives, the operating system searches this
set for a hole large enough for the job (using the first-fit,
best-fit, or worst fit algorithm).
First Fit
• Allocate the first hole that is large enough. This algorithm is generally
faster and empty spaces tend to migrate toward higher memory.
However, it tends to exhibit external fragmentation.
Best Fit
• Allocate the smallest hole that is large enough. This algorithm
produces the smallest leftover hole. However, it may leave many
holes that are too small to be useful.
Worst Fit
• Allocate the largest hole. This algorithm produces the largest leftover
hole. However, it tends to scatter the unused portions over non-
contiguous areas of memory.
3. If the hole is too large for a job, the system splits it into two:
the operating system gives one part to the arriving job and it
returns the other the set of holes.
4. When a job terminates, it releases its block of memory and
the operating system returns it in the set of holes.
5. If the new hole is adjacent to other holes, the system merges
these adjacent holes to form one larger hole. This is also
known as coalescing.
• Internal fragmentation does not exist in MVT but external
fragmentation is still a problem. It is possible to have
several holes with sizes that are too small for any pending
job.
OS OS OS OS OS
if relocation is dynamic, and 40K 40K
40K 40K 40K
is done at execution time.
Job 5
Job 1 Job 1 Job 1 out, Job 5 in after
Job 1
next 5 time units
Job 1
90K
Run 100K 100K 100K 100K 100K
Job Memory
Time
1 60K 10ms Job 4 Job 4
Job 2 out, Job 4 in after Job 4 Job 4
2 100K 5ms Job 2 5 time units
COMPACTION
Job 3 Job 3 Job 3
Note that using BEST-FIT algorithm, the last job finished at 20ms which is
earlier than FIRST-FIT algorithm thus making it able to use memory efficiently.
• MVT still suffers from external fragmentation when
available memory is not contiguous, but fragmented into
many scattered blocks.
Frame number
Page number
page 1 1 page 0
1 4
• The page table contains the 2 3
page 2 2
base address of each page in 3 7
Physical
Memory
• The page size (like the frame size) is defined by the
hardware. The size of a page is typically a power of 2
varying between 512 bytes and 16 MB per page, depending
on the computer architecture.
• If the size of a logical address space is 2m, and a page size is
2n addressing units (bytes or words), then the high-order m –
n bits of a logical address designate the page number, and
the n lower-order bits designate the page offset. Thus, the
logical address is as follows: page number page offset
p d
m-n n
• Example
A4 A3 A2 A1 A0
Physical Address Format:
page offset
frame number
• Logical Memory Address for 16 bytes Process Size
(No. of pages = 4 pages; Page Size = 4 bytes)
A3 A2 A1 A0
Logical Address Format:
page offset
page number
0
1
2
3
4 i
5 j
6 k
0 a a 7 0 l
0 0
1 b 1 b 8 1 m 1
2 c Frame 9 2 n
2 c 2
3 d 3 d number 10
3 o
3
11 p
4 e 4 e 0 5 0 5 12 4 i 4 i
number
5 f 5 f 1 6 13 5 j 5 j
Page
1 6 14
6 g 6 g 6 k 6 k
2 1 2 1 15
7 h 7 l
7 h 3 2 16 7 l
8 i 3 2 17 8 m
8 i 9 j 18
8 m 9 n
9 j 10 k Page Table 19 9 n 10 o
10 k 11 l
Page Table 20 10 a o 11 p
11 l m
21 11 b p
12 22 c
12
12 m 13 n 23 12 d 13
13 n 14 o 24 13 e 14
o 15 p 25 f 15
14 26
14 g
15 p Logical
27 15 h
16
M emory 28
17
Logical 16 18
29
Memory 30
17 19
31 18 20 a
19 21 b
Physical Memory c
20 a 22
23 d
0 a
1 b
2 c
3 d
• Logical address 0 (a) is page 0, offset 0. 4 e
5 f
• Indexing into the page table, it is seen that page 0 is in frame 5. 6 g
7 h
Thus, logical address 0 (a) maps to physical address 20. 8 i
j
• Physical address = frame no x page size + offset = (5 x 4 + 0).
9
10 k
11 l
• Logical address 3 (d) is page 0, offset 3 maps to physical 12 m
address 23 (5 x 4 + 3). 13 n
o
14
• Logical address 4 (e) is page 1, offset 0; according
0 a to the page
15 p
Logical
table, page 1 is mapped to frame 6. 1 b M emory
2 c
• Logical address 4 (e) maps to physical address324 (6 d x 4 + 0).
e 0 5
• Logical address 13 (n) maps to physical address 4
5 9 (2
f x 4 + 1). 1 6
6 g
2 1
7 h
3 2
8 i
9 j
10 k Page Table
logical memory physical memory
0000 a 00000
0001 b 00001
page 0 frame 0
0010 c 00010
0011 d 00011
0100 e 00100 i
0101 f 00101 j
page 1 frame 1
0110 g 00110 k
Given: 0111 h 00111 l
1000 i 01000 m
Main Memory Size = 32 bytes 1001 j 01001 n
page 2 frame 2
Process Size = 16 bytes 1010 k 01010 o
page table
1011 l 01011 p
1100 m 00 101 01100
Page or Frame Size = 4 bytes 1101 n 01 110 01101
page 3 frame 3
1110 o 10 001 01110
No. of Process Pages= 4 pages 1111 p 11 010 01111
10000
No. of MM Frames = 8 frames
10001
frame 4
10010
10011
10100 a
CP U sends logical address 01 01 10101 b
frame 5
10110 c
That address is translated to 10111 d
physical address 110 01
11000 e
11001 f
frame 6
11010 g
11011 h
11100
11101
frame 7
11110
11111
logical memory physical memory
0000 a 00000
0001 b 00001
page 0 frame 0
0010 c 00010
Frame number
0011 d 00011
0100 e 00100 i
Page number
0101 f 00101 j
page 1 frame 1
0110 g 00110 k
0111 h 00111 l
1000 i 01000 m
1001 j 01001 n
page 2 frame 2
1010 k 01010 o
page table
1011 l 01011 p
1100 m 00 101 01100
1101 n 01 110 01101
page 3 frame 3
1110 o 10 001 01110
1111 p 11 010 01111
10000
10001
frame 4
10010
Page size (offset) 10011
10100 a
CP U sends logical address 01 01 10101 b
frame 5
10110 c
That address is translated to 10111 d
physical address 110 01
11000 e
11001 f
frame 6
11010 g
11011 h
11100
11101
frame 7
11110
11111
• In the worst case, a process would need n pages plus one byte. It would
be allocated n + 1 frames, resulting in an internal fragmentation of almost
an entire frame.
• Segmentation is a memory management technique in which each
job is divided into several segments of different sizes, one for
each module that contains pieces that perform related functions.
Each segment is actually a different logical address space of the
program.
• When a process is to be executed, its corresponding segmentation
are loaded into non-contiguous memory though every segment is
loaded into a contiguous block of available memory.
• Segmentation memory management works very similar to paging
but here segments are of variable-length where as in paging pages
are of fixed size.
• A program segment contains the stack
program's main function, utility subrouti
functions, data structures, and so on. ne
symbol
table
• A logical address space is a
collection of segments. Each segment Sqrt
main
has a name and a length. Addresses program
5700
stores the starting address of the
segment (base) and the length LOGICAL ADDRESS SPACE 6300
6700
PHYSICAL
MEMORY
1400
segment 0
• A reference to segment 3, stack 2400
segment 1
6700
PHYSICAL
MEMORY
• Siberschatz, A. (2018). Operating System Concepts,
Wiley.
• Tomsho, G. (2019). Guide to Operating Systems,
Cengage Learning.