Virtual memory in operating system is a memory management technique that enables secondary memory to be a part of the main memory. It enables main memory to accommodate large processes even if it is not big enough to accommodate them at once. It improves the performance of the system and helps in implementing a multi-programming environment.
With virtual memory, OS can load multiple pages of more than one process in the main memory. Thus, virtual memory utilizes both hardware and software to overcome the shortage of physical main memory. Let’s get into more detail to know how the virtual memory works to allow multiple large processes to execute simultaneously.
Virtual Memory in Operating System
- What is Virtual Memory?
- Why is Virtual Memory Needed?
- How Does Virtual Memory Work?
- Types of Virtual Memory
What is Virtual Memory?
Virtual memory is a technique that allows the system to use secondary memory as if it is a part of the main memory. The virtual memory creates an illusion that the system has a very big main memory. Thus, it enables the user to load programs that are bigger than the available main memory.
In this technique, the operating system divides the program/process into pages. And loads different pages of more than one process in the main memory to create a multiprogramming environment.
Why is Virtual Memory Needed?
How Does Virtual Memory Work?
We know that with the virtual memory technique, the operating system utilizes secondary memory as if it is a part of memory. But do you know how the operating system accomplishes this?
Virtual memory is commonly implemented in almost all systems nowadays. When some pages of a process need to be executed the OS tries to identify the free space in RAM so that it can load those pages for execution.
But if the RAM is fully occupied and has no space for any new pages then the OS searches for that area of RAM that is least accessed recently. Identifying the area in RAM that is least accessed in recent times OS copies the content of this area in the secondary memory.
In this way, it creates space for those pages of the processes that need to be executed instead of stopping them executing.
This entire process happens automatically and so fast that the system feels that it has unlimited RAM. The memory management unit keeps a record of this swapping of data from RAM to secondary memory as this information is required in the future. Thus, the operating system uses both hardware and software to create an illusion that the system has a very big RAM.
What if the replaced page is required again?
Now, the pages that OS has replaced from RAM into the secondary memory are needed again the OS swaps those pages from the secondary memory back to the virtual memory. We refer to this process as context switching. Here, the OS uses the information stored by the memory management unit to convert the virtual addresses into real addresses (physical addresses of main memory).
Although this is a fast process frequent use of virtual memory ultimately increases the overhead of context switching and swapping. Thereby it causes a noticeable reduction in the performance of your system.
That is why the system with the large size RAM is preferred over the concept of virtual memory. Nowadays the system has the possibility to extend the RAM which means to can add extra RAM to your system according to your requirement.
Types of Virtual Memory in Operating System
Well, there are two methods for implementing the technique of virtual memory:
- Demand Paging
The concept of paging is that the process is divided into a number of pages and the required pages are loaded into the main memory for execution.
When the next page to be processed is not found in the main memory a page fault occurs and the following steps are performed as a response to this fault.
- In response to this page fault, the operating system generates an interrupt (memory access fault).
- OS blocks the execution of the corresponding process for which the page fault has occurred.
- OS then searches for the required page in secondary memory.
- With the help of some page replacement algorithm, the required page is loaded from (secondary memory) into the RAM.
- Further, the OS updates the page table which has entries of all the pages of the process. And acknowledges the CPU that it can resume the execution of the blocked process.
We refer to this process of loading the page on demand as demand paging. The concept of paging is close to the operating system but it is far from the user’s point of view. As it is hard for the user to view their program or process the number of pages. So the next concept that we will study is segmentation which is closer to the user’s point of view.
In segmentation, the program is divided into various segments depending on its functionality. Like there will be a segment with the program’s main function, another segment with a stack requirement, etc.
When the program is executed, the operating system loads the required segment into the main memory and updates the segment table. The segment table keeps a record of the base address of the segment in the main memory (starting address of the segment in the main memory). And it’s limit (which indicates the end of the particular segment). The information of this segment table is with the segment table base register.
Advantages of Virtual Memory
- Allow main memory to execute multiple large-size programs by creating an illusion that the system has unlimited main memory.
- Improves the performance of the system.
- Helps in creating a multiprogramming environment.
Thus virtual memory offers an illusion that the system has unlimited RAM. And the system is capable of executing large-size programs simultaneously even if the system doesn’t have enough RAM to accommodate all the processes at once.