Main Memory Management
Main Memory Management
MANAGEMENT
4 MB
• One such strategy of memory allocation uses
fixed partitioning. 4 MB
OPERATING
4 MB
• For example, a 32-MB main SYSTEM
memory can be divided into 8 4 MB
equal-sized partitions. 4 MB
4 MB
• Each partition has a size of 4 MB.
4 MB
OPERATING
• Take note that partitions or SYSTEM
4 MB
boundaries are fixed. 4 MB
4 MB
• So once a process is loaded into
4 MB
a partition, any unused memory
locations within that partition 4 MB
cannot be assigned to other
4 MB
processes.
4 MB
5 MB
16 MB Main Memory
Following the best-fit available strategy, process P1, which has a size
of 2 MB, will be assigned to the 3-MB partition.
5 MB
16 MB Main Memory
5 MB
16 MB Main Memory
P4 5 MB 7
28 MB
This is initial state of the main memory.
P4 5 MB 7
28 MB
Process P1 will be loaded into the memory.
It can occupy the first 12 MB of the
available 28 MB space. 16 MB
P4 5 MB 7
28 MB
P2 can now enter the main memory. The P2 7 MB
first 7 MB of the 16-MB hole can now be
assigned to process P2. 16 MB
9 MB
This will leave a hole with a 9 MB capacity
for the remaining processes.
P4 5 MB 7
28 MB
P2 7 MB
When P3 is loaded next, it will occupy the
first 8 MB of the 9-MB hole. 16 MB
P3 9
8 MB
There is now a 1-MB hole left for
allocation. 1 MB
(b)
P4 5 MB 7
P4 5 MB 7
P4 5 MB 7
P4 28 MB
5 MB
Since there is now enough space, the 7- P2 7 MB
MB hole can now be assigned to P4. 2 MB
16 MB
P3
Since the size of P4 is only 5 MB, this will 9 MB
8
leave a 2-MB hole in addition to the 1-MB 1 MB
hole. (b)
(c)
P4 5 MB 7
28 MB
However, there is still external fragmentation. P2 7 MB
Example: OPERATING
4 MB
SYSTEM
If the first-fit strategy is used, P1 4 MB
then the incoming 2-MB process
P4 will be assigned to the 4-MB 4 MB
hole since it is the first available 2 MB P2 3 MB
hole large enough to P4
accommodate P4. 3 MB
Incoming
Process P3 8 MB
If best-fit is used, then P4 will be
placed in the 3-MB hole.
6 MB
And if worst-fit is used, then P4
will be assigned to the 6-MB
32 MB Main Memory
hole.
4 MB P2 3 MB
• The size of a page is equal to the size of a frame so that a page will
exactly fit in a frame.
Process 1
• As an example,
assume that there are
two processes and an
empty main memory.
Process 2
Main Memory
page 2 P2-1
frame 2
• Process P1 is divided
page 3 P3-1
frame 3
frame 4
into 4 pages (pages 0
Process 1
frame 5
to 3).
frame 6
page 0
page 1
P0-2
P1-2
frame 7 • Process P2 is divided
page 2 P2-2
frame 8
into 8 pages (pages 0
page 3 P3-2
frame 9
to 7).
frame 10
page 4 P4-2
frame 11
page 5 P5-2
frame 12 • The main memory is
page 6 P6-2
frame 13 divided into 16 frames
page 7 P7-2
Process 2
frame 14 (frames 0 to 15).
frame 15
Main Memory
Main Memory
page 6 P6-2
P2-1 frame 12
• The operating system
P7-2 frame 13 maintains a list of what frames
page 7 P7-2
P3-2 frame 14 are free or available for
Process 2
frame 15
allocation. This is called the
free frame list.
Main Memory
64050/64 = 1,000.8 1,001 pages. However, the last page will only
have 50 bytes in it therefore
leaving 14 bytes unoccupied. This
The first 1,000 pages will be completely results in an internal
filled with the first 64,000 bytes of the fragmentation of 14 bytes.
process.
• If the process size is 64,001 bytes, the last page will only
have one byte in it resulting in an internal fragmentation of
63 bytes, which is the worst case scenario for this process.
• Since page tables are needed for every instruction reference, they
must be stored in such a way that they can be accessed fast,
otherwise system throughput will drastically go down.
This option offers very fast access times. However, page tables
often contain a very large number of entries thus making this
option very expensive.
Process 3
P5-2
P2-1
frame 11
frame 12
• But since there are only 4
P7-2 frame 13
frames that are unused,
P3-2 frame 14
Process 3 cannot be loaded
frame 15
into the main memory and
Main Memory
therefore cannot execute.
• Virtual memory is an
extension of paging
which allows a process
to execute even though
not all of its pages are
inside the main memory.
• Take note that the pages that are inside the main
memory are just copies of the pages in the hard
disk.
for
Process P1
• A better alternative is to
use the proportional
allocation technique.
• As an example, assume that there are three processes. Process P1 has 1,000
pages, Process P2 has 3,000 pages, and process P3 has 6,000 pages.
The above computations show that P1 has 10% of the total pages,
P2 has 30%, and P3 has 60%.
• However, if the selected page has been modified, that page has to
be written back to the hard disk to update the copy there.
• For example:
valid-invalid
bit dirty bit
• If page 3 was selected for
replacement, it has to be
written back to the hard
0 13 1 0 disk since its dirty bit is 1,
1 -- 0 -- which means it was
2 -- 0 -- modified.
3 2 1 1
4 10 1 0 • If pages 0, 4, or 6 were to
5 -- 0 -- be replaced, they can
6 7 1 0 simply be overwritten
7 -- 0 -- since their dirty bits are all
0s (there were not
Page Table
modified).
• For example, suppose that a process needed a page that was not in
main memory (a page fault occurs).
– During the next reference, it may happen that the process may
request for the page that was recently removed.
1, 2, 3, 4, 3, 1, 4, 2, 5, 2, 6, 2, 3, 1
Main Memory Management
OPTIMAL ALGORITHM
• The simulation is as follows:
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
1
Frames
Page Fault
Page Replaced
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1
1
Frames
Page Fault ×
Page Replaced
At time = 1, page 1 was referenced by the process. Since the main memory is empty, the
reference results into a page fault and page 1 is brought into the main memory and placed in
frame 0.
Take note that this is an initialization page fault. These are the faults produced while the
memory frames are being filled-up.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1
1 2
Frames
Page Fault × ×
Page Replaced
At time = 2, page 2 was referenced. Since page 1 is the only page inside the main memory, this
will again lead to another initialization page fault.
So page 2 will be brought into the main memory and placed in frame 1.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1
1 2 2
Frames
2 3
3
Page Fault × × ×
Page Replaced
At time = 3, page 3 was referenced. Since it is not inside the main memory, this will generate
another initialization page fault.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1
1 2 2 2
Frames
2 3 3
3 4
Page Fault × × × ×
Page Replaced
At time = 4, page 4 was referenced. Since it is not inside the main memory, this will generate
another initialization page fault so there are now four initialization faults.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3
3 4 4 4 4 4
Page Fault × × × ×
Page Replaced
During the next four time intervals (time = 5, 6, 7, and 8), pages 3, 1, 4, and 2 are referenced
respectively.
Since these pages are already inside the main memory, no page faults are generated and
execution continues normally.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3
3 4 4 4 4 4
Page Fault × × × ×
Page Replaced
At time = 9, page 5 is referenced. Since it is not inside the main memory, this reference generates
a page fault. Page 5 has to be brought into the main memory but since there are no free frames
available, a page inside the memory has to be replaced.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3
3 4 4 4 4 4 5
Page Fault × × × × ×
Page Replaced 4
Page 1 will be used again at time = 14. Page 2 will be referenced again at time = 10 and time = 12.
Page 3 will be needed again at time = 13. Page 4 will not used again within the given time period.
So following the optimal strategy, page 4 is the one that will not be used or needed for the
longest period of time. So page 5 will be placed in frame 3, replacing page 4.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3 3
3 4 4 4 4 4 5 5
Page Fault × × × × ×
Page Replaced 4
At time = 10, page 2 was referenced again. This is not a page fault since page 2 is still inside the
main memory.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3 3 3
3 4 4 4 4 4 5 5 6
Page Fault × × × × × ×
Page Replaced 4 5
At time = 11, page 6 was referenced. Since it is not inside the main memory, the reference results
into a page fault. Possible choices for pages to be replaced are pages 1, 2, 3, and 5. Page 1 will
be used again at time = 14. Page 2 will be referenced again at time = 12 while page 3 at time =
13. Page 5 will not be used again within the given time period so page 6 will be placed in frame 3,
replacing page 5.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3 3 3 3 3 3
3 4 4 4 4 4 5 5 6 6 6 6
Page Fault × × × × × ×
Page Replaced 4 5
From time = 12 to time = 14, there are no page faults because the pages that are needed (pages
2, 3, and 1) are all inside the main memory.
So the total page faults for this reference pattern using the optimal algorithm is 6. If initialization
page faults are ignored, then there are only 2 page faults.
• However, it is impossible to
implement because it requires the
operating system to know what
pages will be referenced in the
future.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
1
Frames
Page Fault
Page Replaced
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1
1 2 2 2
Frames
2 3 3
3 4
Page Fault × × × ×
Page Replaced
As in the optimal algorithm, the first four memory references (from time = 1 to time = 4) will
result into initialization page faults and pages 1, 2, 3, and 4 are brought into the main memory in
that order.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3
3 4 4 4 4 4
Page Fault × × × ×
Page Replaced
From time = 5 to time = 8, no page faults are generated since all the pages needed (pages 3, 1, 4,
and 2) are already in the main memory.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5
1 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3
3 4 4 4 4 4 4
Page Fault × × × × ×
Page Replaced 1
The candidates for replacement are pages 1, 2, 3, and 4. Among them, page 1 is the oldest since
it entered the main memory at time = 1. The others were placed in the main memory after that.
So page 5 is placed in frame 0 replacing page 1.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5 5
1 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 3 3
3 4 4 4 4 4 4 4
Page Fault × × × × ×
Page Replaced 1
At time = 10, no page fault was generated since page 2 is still in the main memory.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5 5 5
1 2 2 2 2 2 2 2 2 2 6
Frames
2 3 3 3 3 3 3 3 3 3
3 4 4 4 4 4 4 4 4
Page Fault × × × × × ×
Page Replaced 1 2
The pages in memory that can be replaced are pages 2, 3, 4, and 5. Page 2 is the oldest since it
was placed inside the main memory at time = 2. Therefore it is replaced by page 6.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5 5 5 5
1 2 2 2 2 2 2 2 2 2 6 6
Frames
2 3 3 3 3 3 3 3 3 3 2
3 4 4 4 4 4 4 4 4 4
Page Fault × × × × × × ×
Page Replaced 1 2 3
Possible choices for replacement are pages 3, 4, 5, and 6. Among these, page 3 is the oldest since
it entered at time = 3. So it is replaced and page 2 is placed in frame 2.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5 5 5 5 5
1 2 2 2 2 2 2 2 2 2 6 6 6
Frames
2 3 3 3 3 3 3 3 3 3 2 2
3 4 4 4 4 4 4 4 4 4 3
Page Fault × × × × × × × ×
Page Replaced 1 2 3 4
At time = 13, page 3 was referenced therefore generating another page fault.
Among the pages in memory, page 4 is the oldest since it entered the memory at time = 4. So it
replaced by page 3.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 5 5 5 5 5 1
1 2 2 2 2 2 2 2 2 2 6 6 6 6
Frames
2 3 3 3 3 3 3 3 3 3 2 2 2
3 4 4 4 4 4 4 4 4 4 3 3
Page Fault × × × × × × × × ×
Page Replaced 1 2 3 4 5
At time = 14, page 1 was referenced and causing another page fault. Since page 5 entered the
memory at time= 9, it is the oldest so it will be replaced by page 1.
So the total page faults for the FIFO algorithm is 5 (not counting initialization page faults).
• The operating system must keep track of the time a page was
brought into the main memory in order to determine the oldest
page in memory.
• The Least Recently Used (LRU) algorithm selects the least recently
used page as the one to be replaced.
• Conversely, the page that has not been recently used is less likely to
be used again and is thus a good choice for the page to be
replaced.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
1
Frames
Page Fault
Page Replaced
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1
1 2 2 2
Frames
2 3 3
3 4
Page Fault × × × ×
Page Replaced
As in the first two algorithms, the first four memory references (from time = 1 to time = 4) will
result into initialization page faults and pages 1, 2, 3, and 4 are brought into the main memory in
that order.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3
3 4 4 4 4 4
Page Fault × × × ×
Page Replaced
From time = 5 to time = 8, no page faults are generated since all the pages needed (pages 3, 1, 4,
and 2) are already in the main memory.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5
3 4 4 4 4 4 4
Page Fault × × × × ×
Page Replaced 3
At time = 9, page 5 was referenced thereby causing a page fault. The candidates for replacement
are pages 1, 2, 3, and 4.
Page 1 was recently used at time = 6, page 2 at time = 8, page 3 at time = 5, and page 4 at time =
7. So among the four pages inside the main memory, page 3 is the least recently used. It is
therefore replaced by page 5.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5 5
3 4 4 4 4 4 4 4
Page Fault × × × × ×
Page Replaced 3
At time = 10, no page fault was generated since page 2 is still in the main memory.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 6
1 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5 5 5
3 4 4 4 4 4 4 4 4
Page Fault × × × × × ×
Page Replaced 3 1
At time = 11, a reference to page 6 resulted into a page fault. Among the pages in the main
memory, page 1 was last referenced at time = 6, page 2 at time = 10, page 4 at time = 7, and page
5 at time = 9.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 6 6
1 2 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5 5 5 5
3 4 4 4 4 4 4 4 4 4
Page Fault × × × × × ×
Page Replaced 3 1
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 6 6 6
1 2 2 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5 5 5 5 5
3 4 4 4 4 4 4 4 4 4 3
Page Fault × × × × × × ×
Page Replaced 3 1 4
Among the pages in the main memory, page 4 is the one that is the least recently used and will
thus be replaced by page 3.
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Page 1 2 3 4 3 1 4 2 5 2 6 2 3 1
Referenced
0 1 1 1 1 1 1 1 1 1 1 6 6 6 6
1 2 2 2 2 2 2 2 2 2 2 2 2 2
Frames
2 3 3 3 3 3 3 5 5 5 5 5 1
3 4 4 4 4 4 4 4 4 4 3 3
Page Fault × × × × × × × ×
Page Replaced 3 1 4 5
At time = 14, page 1 was referenced thus causing a page fault. Among the pages in the main
memory, page 5 is the one that is the least recently used. It will therefore be replaced by page 1.
The total number of page faults using the LRU algorithm is therefore 4 (not counting the
initialization faults).