Unit 4: Virtual Memory
Type Unit 4
Reviewed
Writing Not started
Introduction
Virtual memory is a memory management technique that allows a computer to use
more memory than is physically installed on the system. It does this by dividing the
computer's memory into two parts:
Physical memory: This is the actual RAM that is installed on the computer.
Virtual memory: This is a section of the hard drive that is used to store data that
is not currently being used by a program.
When a program is running, it accesses its data and instructions through a virtual
address space. This virtual address space is much larger than the physical memory
of the computer. The operating system (OS) maps the virtual address space to the
physical memory, using a technique called paging.
When a program accesses a virtual address that is not currently in physical memory,
a page fault occurs. The OS then loads the required page from virtual memory into
physical memory. If there is not enough space in physical memory, the OS will evict
a page that is not currently being used by any program.
Virtual memory allows a computer to run programs that are larger than the physical
memory of the computer. It also allows a computer to run multiple programs at the
same time, even if they do not all fit into physical memory at the same time.
Here are some of the benefits of virtual memory:
It allows a computer to run programs that are larger than the physical memory of
the computer.
Unit 4: Virtual Memory 1
It allows a computer to run multiple programs at the same time, even if they do
not all fit into physical memory at the same time.
It can improve the performance of a computer by reducing the number of times
that programs have to be swapped out of physical memory.
However, virtual memory also has some drawbacks:
It can slow down the performance of a computer, especially if the hard drive is
slow.
It can use more disk space, since virtual memory pages are stored on the hard
drive.
It can make it more difficult to debug programs, since the code that is running in
physical memory may not be the same code that the programmer wrote.
Overall, virtual memory is a valuable memory management technique that allows
computers to run more programs and larger programs than would be possible
without it. However, it is important to be aware of the potential drawbacks of virtual
memory before using it.
Overlays
Overlays are a memory management technique that was used before virtual memory
was developed. Overlays allow a program to be larger than the physical memory of
the computer by dividing the program into smaller sections, called overlays, that can
be loaded into memory as needed.
When a program is running, only the overlay that is currently being used is loaded
into memory. The other overlays are stored on disk. When the program needs to
access a different overlay, the operating system (OS) loads the required overlay into
memory and unloads the overlay that is no longer being used.
Overlays are less efficient than virtual memory because they require the OS to load
and unload overlays from disk, which can slow down the program. However,
overlays can be used on computers that do not have virtual memory support.
Virtual memory is a more efficient memory management technique than overlays
because it allows the OS to keep all of the pages of a program in memory, even if
they are not all being used at the same time. This can improve the performance of a
program by reducing the number of times that the OS has to load and unload pages
from disk.
Unit 4: Virtual Memory 2
In modern operating systems, overlays are rarely used because virtual memory is
more efficient. However, overlays can still be used in some embedded systems
where virtual memory is not supported.
Here is a table that summarizes the differences between overlays and virtual
memory:
Feature Overlays Virtual Memory
Efficiency Less efficient More efficient
Supported Computers without virtual
All computers
platforms memory support
Divides a program into smaller
Maps a program's virtual address
How it works sections that can be loaded into
space to physical memory
memory as needed
Improves performance by reducing the
Can be used on computers
Pros number of times that pages have to be
without virtual memory support
loaded from disk
Less efficient than virtual
Cons Requires more disk space
memory
Demand Paging
Demand paging is a memory management technique used in virtual memory
systems. In demand paging, a page is brought into main memory only when it is
needed by the CPU. This is in contrast to pre-paging, where pages are brought into
main memory in anticipation of being needed by the CPU.
Demand paging is a lazy loading technique, which means that pages are not brought
into main memory until they are actually needed. This can improve performance
because it reduces the amount of time that the CPU spends waiting for pages to be
loaded from disk. However, demand paging can also lead to increased page faults,
which can slow down the system.
The decision of which page to bring into main memory when a page fault occurs is
typically made by the operating system using a page replacement algorithm. The
goal of a page replacement algorithm is to minimize the number of page faults while
maximizing the amount of time that pages are in main memory.
Page Replacement Algorithms
Unit 4: Virtual Memory 3
A page replacement algorithm is a strategy used by an operating system to decide
which page of memory to replace when a new page needs to be loaded into
memory. This is necessary because the amount of physical memory available is
typically less than the amount of virtual memory that a process needs.
There are a number of different page replacement algorithms, each with its own
advantages and disadvantages. Some of the most common page replacement
algorithms include:
First-in, first-out (FIFO): This algorithm simply replaces the oldest page in
memory. This is a simple and easy-to-implement algorithm, but it can lead to
thrashing, which is a condition where the system is constantly swapping pages
in and out of memory.
Least recently used (LRU): This algorithm replaces the page that has not been
used in the longest time. This algorithm is more complex than FIFO, but it can
help to reduce thrashing.
Most recently used (MRU): This algorithm replaces the page that has been
used most recently. This algorithm is even more complex than LRU, but it can be
very effective at reducing thrashing.
The choice of page replacement algorithm is a trade-off between performance and
complexity. Simpler algorithms are easier to implement but may not be as efficient as
more complex algorithms.
Here is a table that summarizes the different page replacement algorithms:
Algorithm Description Advantage Disadvantage
Replaces the oldest page in Simple and easy to Can lead to
FIFO
memory. implement. thrashing.
Replaces the page that has
Can help to reduce More complex
LRU not been used in the longest
thrashing. than FIFO.
time.
Can be very Most complex of
Replaces the page that has
MRU effective at reducing the three
been used most recently.
thrashing. algorithms.
Ultimately, the best page replacement algorithm for a particular system will depend
on a number of factors, such as the size of the physical memory, the number of
processes running, and the behavior of the processes.
First In First Out Algorithm
Unit 4: Virtual Memory 4
First-in first-out (FIFO) page replacement algorithm is a simple and easy-to-
implement algorithm that replaces the oldest page in memory when a new page
needs to be loaded. This algorithm is easy to understand and implement, but it can
lead to thrashing, which is a condition where the system is constantly swapping
pages in and out of memory.
Here are the steps involved in the FIFO page replacement algorithm:
1. The operating system keeps track of all of the pages in memory in a queue.
2. When a page fault occurs, the operating system removes the oldest page from
the queue and replaces it with the new page.
3. The operating system then updates the queue so that the new page is at the
front of the queue.
The FIFO page replacement algorithm is not very efficient because it does not take
into account the recency of use of the pages in memory. This means that it is
possible for the operating system to replace a page that has just been used with a
page that has not been used in a long time. This can lead to thrashing because the
operating system will be constantly swapping pages in and out of memory, which can
slow down the system.
Despite its inefficiency, the FIFO page replacement algorithm is often used in
operating systems because it is simple and easy to implement. It is also a good
choice for systems where the amount of physical memory is very small and the
number of pages that need to be kept in memory is large.
Here are some of the advantages and disadvantages of the FIFO page replacement
algorithm:
Advantages:
Simple and easy to implement.
Requires little overhead.
Disadvantages:
Can lead to thrashing.
Does not take into account the recency of use of pages.
Not very efficient in systems with large number of pages.
Overall, the FIFO page replacement algorithm is a simple and easy-to-implement
algorithm that can be used in systems with limited physical memory. However, it is
not very efficient in systems with a large number of pages and can lead to thrashing.
Unit 4: Virtual Memory 5
Second-Chance Algorithm
The second chance page replacement algorithm is a page replacement algorithm
that is a modification of the first-in first-out (FIFO) algorithm. In the FIFO algorithm,
the oldest page in memory is replaced when a new page needs to be loaded. The
second chance algorithm works similarly, but it gives pages a second chance before
they are replaced.
Here are the steps involved in the second chance page replacement algorithm:
1. The operating system keeps track of all of the pages in memory in a queue.
2. When a page fault occurs, the operating system removes the oldest page from
the queue.
3. The operating system then checks the reference bit for the page. The reference
bit is a bit that is set to 1 if the page has been referenced since it was last
brought into memory.
4. If the reference bit is set, the page is given a second chance and is not replaced.
5. If the reference bit is not set, the page is replaced with the new page.
The second chance page replacement algorithm is more efficient than the FIFO
algorithm because it gives pages a second chance before they are replaced. This
means that pages that have just been used are less likely to be replaced, which can
help to reduce thrashing.
Here are some of the advantages and disadvantages of the second chance page
replacement algorithm:
Advantages:
More efficient than FIFO algorithm.
Does not suffer from thrashing as much as FIFO algorithm.
Simple and easy to implement.
Disadvantages:
Not as efficient as other algorithms such as LRU and MRU.
Can still lead to thrashing in some cases.
Overall, the second chance page replacement algorithm is a good compromise
between efficiency and simplicity. It is more efficient than the FIFO algorithm and
does not suffer from thrashing as much. However, it is not as efficient as other
algorithms such as LRU and MRU.
Unit 4: Virtual Memory 6
Least Recently Used Algorithm
The least recently used (LRU) page replacement algorithm is a page replacement
algorithm that replaces the page that has not been used in the longest time. This
algorithm is more complex than FIFO, but it can help to reduce thrashing.
Here are the steps involved in the LRU page replacement algorithm:
1. The operating system keeps track of all of the pages in memory in a list.
2. When a page fault occurs, the operating system removes the page from the list
that has not been used in the longest time.
3. The operating system then replaces the page with the new page.
4. The operating system then updates the list so that the new page is at the front of
the list.
The LRU page replacement algorithm is more efficient than the FIFO algorithm
because it takes into account the recency of use of the pages in memory. This
means that pages that have not been used in a long time are more likely to be
replaced, which can help to reduce thrashing.
Here are some of the advantages and disadvantages of the LRU page replacement
algorithm:
Advantages:
More efficient than FIFO algorithm.
Does not suffer from thrashing as much as FIFO algorithm.
Can be very efficient in systems with a large number of pages.
Disadvantages:
More complex than FIFO algorithm.
Requires more overhead.
Overall, the LRU page replacement algorithm is a more efficient page replacement
algorithm than the FIFO algorithm. However, it is also more complex and requires
more overhead.
Here are some of the applications of LRU page replacement algorithm:
Operating systems: LRU page replacement algorithm is used in many
operating systems, such as Linux, Windows, and macOS.
Unit 4: Virtual Memory 7
Caches: LRU page replacement algorithm is also used in caches, such as the
CPU cache and the web browser cache.
Databases: LRU page replacement algorithm is also used in databases, such
as MySQL and PostgreSQL.
The LRU page replacement algorithm is a very efficient algorithm that can be used in
a variety of applications. It is a good choice for systems where it is important to
reduce thrashing and improve performance.
Least Frequently Used Algorithm
The least frequently used (LFU) page replacement algorithm is a page replacement
algorithm that replaces the page that has been used the least number of times. This
algorithm is more complex than FIFO and LRU, but it can be more efficient in some
cases.
Here are the steps involved in the LFU page replacement algorithm:
1. The operating system keeps track of all of the pages in memory in a table.
2. Each page in the table has a counter that is incremented every time the page is
used.
3. When a page fault occurs, the operating system removes the page from the
table that has the lowest counter value.
4. The operating system then replaces the page with the new page.
5. The operating system then updates the counter for the new page to 1.
The LFU page replacement algorithm is more efficient than FIFO and LRU because
it takes into account the frequency of use of the pages in memory. This means that
pages that are used less frequently are more likely to be replaced, which can help to
reduce thrashing.
However, the LFU page replacement algorithm can be more complex than FIFO and
LRU, and it can also require more overhead.
Here are some of the advantages and disadvantages of the LFU page replacement
algorithm:
Advantages:
More efficient than FIFO and LRU in some cases.
Can be very efficient in systems with a large number of pages.
Unit 4: Virtual Memory 8
Can help to reduce thrashing.
Disadvantages:
More complex than FIFO and LRU.
Requires more overhead.
Can be less efficient than FIFO and LRU in some cases.
Overall, the LFU page replacement algorithm is a more efficient page replacement
algorithm than FIFO and LRU in some cases. However, it is also more complex and
requires more overhead.
Here are some of the applications of LFU page replacement algorithm:
Operating systems: LFU page replacement algorithm is used in some
operating systems, such as Linux.
Caches: LFU page replacement algorithm is also used in caches, such as the
web browser cache.
Databases: LFU page replacement algorithm is also used in databases, such as
MySQL and PostgreSQL.
The LFU page replacement algorithm is a very efficient algorithm that can be used in
a variety of applications. It is a good choice for systems where it is important to
reduce thrashing and improve performance.
Optimal Page Replacement Algorithm
The optimal page replacement algorithm (OPRA) is a page replacement algorithm
that replaces the page that will not be used for the longest period of time in the
future. This algorithm is the most efficient page replacement algorithm in theory, but
it is also the most complex and requires the most overhead.
Here are the steps involved in the OPRA page replacement algorithm:
1. The operating system keeps track of all of the pages in memory in a table.
2. Each page in the table has a timestamp that is updated every time the page is
used.
3. When a page fault occurs, the operating system removes the page from the
table that has the farthest timestamp in the future.
4. The operating system then replaces the page with the new page.
Unit 4: Virtual Memory 9
5. The operating system then updates the timestamp for the new page to the
current time.
The OPRA page replacement algorithm is the most efficient page replacement
algorithm because it takes into account the future use of the pages in memory. This
means that pages that will not be used for the longest period of time are more likely
to be replaced, which can help to reduce thrashing.
However, the OPRA page replacement algorithm is also the most complex and
requires the most overhead. This is because the operating system needs to keep
track of the timestamps for all of the pages in memory, which can be a lot of data.
Here are some of the advantages and disadvantages of the OPRA page
replacement algorithm:
Advantages:
Most efficient page replacement algorithm in theory.
Can help to reduce thrashing.
Disadvantages:
Most complex page replacement algorithm.
Requires the most overhead.
Cannot be implemented in real-world systems.
Overall, the OPRA page replacement algorithm is the most efficient page
replacement algorithm in theory. However, it is also the most complex and requires
the most overhead. It cannot be implemented in real-world systems because it is
impossible to predict the future use of pages in memory.
Unit 4: Virtual Memory 10