Last Updated on March 23, 2023 by Prepbytes
The modern world is running on data. Without data, we will not be able to do anything. The computers use operating systems that use the data from the memory. Virtual memory in OS is one of the main reasons for the rapid growth and advancement of technology as virtual memory in os allows the computer to use more memory than is physically available. While moving further in this article we will learn all about Virtual memory in OS, how it works, its benefits, limitations, and various processes and techniques associated with it.
What is Virtual Memory in OS?
Virtual memory in OS is a memory management technique that enables or allows the computer to execute larger programs or multiple programs together that would not be possible by using only the available physical memory. In simple words, we can also say that virtual memory in os makes an illusion of extra memory than available physical memory.
Virtual Memory in OS organizes the physical memory in various parts and keeps note of all the available and used memory and it is also known as the page table.
A computer can run the software by combining physical memory and hard disk space with virtual memory. Physical memory is loaded when a program is run, but if there is not enough space, the operating system will move some program components to the hard drive. This process is known as swapping.
The user is unaware of the swapping procedure because it takes place in the background. The operating system will swap the necessary portion of the program back into physical memory when the user tries to access a portion of it that has been moved to the hard drive.
How does Virtual Memory Work?
Virtual memory works by dividing a program into small sections called pages. Each page is typically 4 kilobytes in size. When a program is executed, the operating system loads some of the pages into physical memory. The pages that are not loaded into physical memory are stored on the hard disk.
The operating system uses a page table to keep track of which pages are in physical memory and which are on the hard disk. When a program attempts to access a page that is not in physical memory, the operating system generates a page fault. The page fault interrupts the program and transfers control to the operating system.
The operating system then reads the required page from the hard disk and stores it in physical memory. The operating system updates the page table to indicate that the page is now in physical memory, and then returns control to the program. The program can now access the required page, and the execution of the program continues.
It is one of the memory management techniques that is used by operating systems. In this technique, only the pages that are required are loaded into the memory, rather than loading all the pages at the start of the program. This approach saves memory resources and allows the programs to run even if they require more memory than is available. It ulcer reduces the startup time of the programs and also makes the I/O faster. Because of this many programs can also run at the same time when one program gets the required page the other can get its required pages.
It is used to structure physical memory in OS. The size of the page frame is not fixed it varies between platforms in the power of 2.
Page frames are the blocks of memory used to store the pages of a program. Each page frame typically contains one page of memory. The total number of page frames available is determined by the amount of physical memory available in the system.
Page frames are used to store pages that are currently in use by a program. When a program needs to access a page that is not in memory, the operating system swaps a page frame out of memory to disk, freeing up the page frame for the required page.
A page table is a data structure used by the operating system to keep track of the location of memory pages. In simple words, it can be referred to as a data structure used by the virtual memory in OS to record the relationship between physical and virtual addresses. The page table maps the virtual addresses used by the program to the physical addresses of the corresponding page frames. Each entry in the page table contains information about the page, including whether the page is currently in memory or on disk.
Swap In and Swap Out or Swapping
Swap in and swap out refer to the processes of moving pages of memory between physical memory and disk. When a process needs to access a page that is not currently in memory, the operating system swaps out a page frame from memory to make room for the required page. The swapped-out page is written to disk, freeing up the page frame for the required page. This process is called swap out.
When the required page is found on disk, the operating system swaps it into a free page frame in memory. This process is called swap-in. The operating system updates the page table to reflect the new location of the page in memory.
The frequency of swap-in and swap-out operations depends on the memory requirements of the running programs and the available physical memory. If the available physical memory is insufficient to hold all required pages, the operating system may need to swap pages in and out frequently, resulting in increased disk I/O and decreased system performance.
Thrashing is a phenomenon that occurs when the operating system spends more time swapping the pages in and out of the memory than it does in executing the program. There are many reasons for thrashing but the main reason is when a program has a high memory requirement, but the available physical memory is not enough to hold all the pages required together. And because of this, the operating system spends most of its time swapping the pages in and out of the memory instead of doing what it is required to do i.e, executing the program.
This results in a decrease in system performance and can lead to system instability. To avoid thrashing, the operating system may use techniques such as page replacement algorithms and increasing the available physical memory.
In the above diagram the degree of multiprogramming is increasing and with the CPU utilization is also increasing up to a certain point i.e, lambda(λ) at this point the degree of multiprogramming is maximum as the CPU is utilized is also maximum and all the resources are utilized 100%. Increasing the degree of multiprogramming after this will lead to a decrease in CPU utilization. And the time taken for page replacement will increase and the time for executing the program will increase this situation is known as thrashing.
For studying more about thrashing you can refer to thrashing.
Frame allocation is nothing but a process of allocating physical memory to a process or program. Simply speaking there are two types of frame allocations one is static in which the number of frames allocated to the process is fixed whereas in a dynamic number of frames allocated to the process is not fixed.
The operating system maintains a pool of free page frames that can be used to store pages of memory. When a process needs to access a page of memory that is not currently in memory, the operating system selects a free page frame and copies the required page from the disk into the selected frame. The selected frame is then marked as being in use by the process.
Paging policies refer to the algorithms used by the operating system to manage page frames and decide which pages should be swapped in and out of memory. The most commonly used paging policies are the Least Recently Used (LRU) policy, the First In First Out (FIFO) policy, and the Clock policy.
The LRU policy selects the page frame that has not been accessed for the longest time and swaps it out to make room for the new page. The FIFO policy selects the oldest page frame and swaps it out. The Clock policy uses a circular buffer to maintain a list of page frames and swaps out the first frame that has not been accessed since the last time the buffer was scanned.
Benefits of Virtual Memory in OS
There are various benefits provided by virtual memory in OS some of them are given below:
- It will enable the efficient use of physical memory by swapping the pages between the hard disk and physical memory.
- We can also execute large programs which are larger than the available physical memory with the help of pages and only use the required pages at a given time.
- We can run multiple programs simultaneously by swapping we can make sure only the required pages of each program stays in the memory.
- It will make the use of the CPU effective as more processes are maintained in the memory which will result in the better use of resources.
Limitations of Virtual Memory in OS
Although virtual memory in os provides various benefits but align with that there are certain limitations of it that we have discusses in this blog section.
- It will require significant overhead in terms of CPU cycles and memory usage. This overhead sometimes impacts the system’s performance.
- It will lead to fragmentation which decreases the efficiency of memory usage. This will also result in slower system performance and larger memory requirements.
- Frequent swapping will result in decreased system performance due to increase I/O.
- It will lead to thrashing at certain times the operating system will be dealing with swapping the pages in and out of the memory and will not be focusing on executing the program.
Modern operating systems require the idea of virtual memory because it allows for the efficient and effective use of physical memory. A computer can run more complex programs or several programs at once using virtual memory than it would be able to do with just physical memory alone. A program is divided into smaller sections called pages in virtual memory, which uses physical memory and hard disk space to keep and access these pages. The efficient use of physical memory, the execution of large programs, the execution of numerous programs, and program protection are all advantages of virtual memory.
Frequently Asked Questions
Here are some of the frequently asked questions about virtual memory in os.
1. How is virtual memory managed in operating systems?
Virtual memory is managed by the operating system using a combination of page tables, page frames, and swapping.
2. How does the operating system handle page faults?
When a page fault occurs, the operating system brings the required page into physical memory from secondary storage.
3. How can thrashing be avoided?
Thrashing can be avoided by optimizing paging policies, increasing physical memory, or reducing the number of running programs.
4. What is a pre-paged system?
A pre-paged system is an operating system that brings pages into physical memory before they are needed.
5. What is the Clock paging policy?
The Clock paging policy maintains a circular list of page frames and swaps out the first-page frame that has not been accessed since the last scan.