What is Virtual Memory?
Virtual Memory is a storage technique that creates an illusion to the user of a very large main memory. It creates an illusion that the secondary memory is also a part of the main memory. The extra memory other than the main memory which is used for storing the pages of processes is referred to as the Virtual Memory.
It helps in loading bigger-size processes than the size of the main memory. It also helps in increasing the degree of multiprogramming by storing more and more processes inside the main memory.
The whole process is not required to be loaded in the main memory for the execution. Only a part of a process is required at a time. It means a page of a process is loaded in the main memory if required else not. Virtual Memory is implemented using Demand Paging. It can also be implemented using Demand Segmentation.
Demand Paging
CPU generates a logical address which is converted to a physical address by MMU (Memory Management Unit). When a page referenced by CPU is not found in the main memory, this is called Page Fault. Demand Paging is a process of copying a page from the disk into the main memory whenever required or when a Page Fault occurs.
This is done by the Operating System through the following steps:
- When a CPU references to a Page that is not present in the main memory, a Page Fault occurs.
- To execute the process, the Operating System needs to bring the reference page in the main memory.
- The Operating System then searches for the required page in the Disk Storage. It is a very slow process.
- Then it stores the page in one of the vacant frames of the main memory.
- Then Page table is updated according to that frame number for future references.
- Then the CPU executes the program again.
.png)
Swapping
Swapping is a process done by the Operating System for swapping in and out of the process in the main memory. When the pages of a process are not required then it is swapped out from the main memory and when they are required they are swapped in the memory again.
Thrashing
Thrashing is a condition in which the CPU utilization decreases drastically. It happens when the CPU is too busy Swapping in and Swapping out the pages from the main memory. One of the major reasons for this is the high degree of multiprogramming.
When there are too many processes in the main memory, the number of frames allocated to a process goes down, and the swapping of pages in and out becomes more frequent. This leads to more number of page faults and the CPU gets busy servicing those page faults which ultimately leads to thrashing.
Advantages of Virtual Memory
- It helps in executing bigger processes with less RAM.
- Large Applications can be run with less RAM.
- It increases the degree of multiprogramming.
Disadvantages of Virtual Memory
- It is more time-consuming as accessing disk storage is very slow.
- The Applications may run slower.
- Part of hard disk storage is also used in this process.