Virtual memory is a memory management capability of an operating system (OS) which uses hardware and software to allow a computer to compensate for physical memory shortages, by temporarily transferring data from random access memory to disk storage. Computers have a finite amount of RAM, so memory can run out, especially when multiple programs or large programs run at the same time. A system using virtual memory uses a section of the hard drive to increase the virtual address space of RAM
Working of Virtual Memory
In the modern world, virtual memory has become quite common these days. It is used whenever some pages require to be loaded in the main memory for the execution, and the memory is not available for those many pages.
So, in that case, instead of preventing pages from entering in the main memory, the OS searches for the RAM space that is minimum used in the recent times or that are not referenced into the secondary memory to make the space for the new pages in the main memory.
- A request is made to the CPU for a page that may not be available in the main/primary memory in an active state. Thus, it has to generate an interrupt and a page fault takes place.
- This is when the Operating system moves the process to a blocked state as an interrupt has occurred.
- The Operating system then searches the given page in the Logical address space
- Finally, with the help of page replacement algorithms, replacements are made in the physical address space. Simultaneously, page tables are updated.
- CPU is informed about the update and asked to go ahead with the execution.
Page Replacement Algorithms
- When there is a page fault, the referenced page must be loaded.
- If there is no available frame in memory, then one page is selected for replacement
- If the selected page has been modified, it must be copied back to disk.
Types of Page Replacement Algorithms
- FIFO – First In First Out
- LRU – Least Recently Used
- Optimal Page Replacement Algorithm
- LFU – Least Frequently Used
- MFU – Most Frequently Used
First-In-First-Out (FIFO) Replacement
When a page fault occurs, the frame that has been in memory the longest is replaced.
Least Recently Used (LRU) Replacement
On a page fault, the frame that was least recently used in replaced.
Virtual Memory and Belady’s Anomaly
Operating systems (OS) use Virtual memory when the programs executed by them need more space than the physical memory can allocate. The pages are loaded into the physical memory using the method of demand paging. In demand paging, during the execution of a particular process, whenever a page is required, a page fault occurs, and then the required page gets loaded into the memory replacing some other page. The page replacement algorithm specifies the choice of the page which is to be replaced. Belady’s Anomaly is said to occur when the number of page faults increases significantly.
The rate of page faults varies directly with the number of frames allocated to the individual process. The increase in the number of frames considerably decreases the number of page faults. However, sometimes reverse action occurs when the increased number of frames results in increased page faults. This exception is known as the Belady’s Anomaly.
Thrashing occurs when processes on system require more memory than it has. If processes do not have “enough” pages, the page fault rate is very high. This leads to:
– low CPU utilization
– operating system spends most of its time swapping to disk
Virtual memory is illusion of large main memory.