In this tutorial, we will be covering the concept of Virtual Memory in an Operating System.
Virtual Memory is a space where large programs can store themselves in form of pages while their execution and only the required pages or portions of processes are loaded into the main memory. This technique is useful as a large virtual memory is provided for user programs when a very small physical memory is there. Thus Virtual memory is a technique that allows the execution of processes that are not in the physical memory completely.
Virtual Memory mainly gives the illusion of more physical memory than there really is with the help of Demand Paging.
In real scenarios, most processes never need all their pages at once, for the following reasons :
Error handling code is not needed unless that specific error occurs, some of which are quite rare.
Arrays are often over-sized for worst-case scenarios, and only a small fraction of the arrays are actually used in practice.
Certain features of certain programs are rarely used.
In an Operating system, the memory is usually stored in the form of units that are known as pages. Basically, these are atomic units used to store large programs.
Virtual memory can be implemented with the help of:-
Following are the reasons due to which there is a need for Virtual Memory:
In case, if a computer running the Windows operating system needs more memory or RAM than the memory installed in the system then it uses a small portion of the hard drive for this purpose.
Suppose there is a situation when your computer does not have space in the physical memory, then it writes things that it needs to remember into the hard disk in a swap file and that as virtual memory.
Large programs can be written, as the virtual space available is huge compared to physical memory.
Less I/O required leads to faster and easy swapping of processes.
More physical memory available, as programs are stored on virtual memory, so they occupy very less space on actual physical memory.
Therefore, the Logical address space can be much more larger than that of physical address space.
Virtual memory allows address spaces to be shared by several processes.
During the process creation, virtual memory allows: copy-on-write and Memory-mapped files
With the help of the Operating system few pieces of the program are brought into the main memory:
Whenever an address is needed that is not in the main memory, then it generates an interrupt. The process is placed in the blocked state by the Operating system. Those pieces of the process that contains the logical address are brought into the main memory.
The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at once. Rather they are swapped in only when the process needs them(On-demand). Initially, only those pages are loaded which will be required by the process immediately.
The pages that are not moved into the memory, are marked as invalid in the page table. For an invalid entry, the rest of the table is empty. In the case of pages that are loaded in the memory, they are marked as valid along with the information about where to find the swapped out page.
As studied in Demand Paging, only certain pages of a process are loaded initially into the memory. This allows us to get more number of processes into memory at the same time. but what happens when a process requests for more pages and no free memory is available to bring them in. Following steps can be taken to deal with this problem :
Put the process in the wait queue, until any other process finishes its execution thereby freeing frames.
Or, remove some other process completely from the memory to free frames.
Or, find some pages that are not being used right now, move them to the disk to get free frames. This technique is called Page replacement and is most commonly used. We have some great algorithms to carry on page replacement efficiently.
A process that is spending more time paging than executing is said to be thrashing. In other words, it means, that the process doesn't have enough frames to hold all the pages for its execution, so it is swapping pages in and out very frequently to keep executing. Sometimes, the pages which will be required in the near future have to be swapped out.
Initially, when the CPU utilization is low, the process scheduling mechanism, to increase the level of multiprogramming loads multiple processes into the memory at the same time, allocating a limited amount of frames to each process. As the memory fills up, the process starts to spend a lot of time for the required pages to be swapped in, again leading to low CPU utilization because most of the processes are waiting for pages. Hence the scheduler loads more processes to increase CPU utilization, as this continues at a point of time the complete system comes to a stop.
Given below are the advantages of using Virtual Memory:
Virtual Memory allows you to run more applications at a time.
With the help of virtual memory, you can easily fit many large programs into smaller programs.
With the help of Virtual memory, a multiprogramming environment can be easily implemented.
As more processes should be maintained in the main memory which leads to the effective utilization of the CPU.
Data should be read from disk at the time when required.
Common data can be shared easily between memory.
With the help of virtual memory, speed is gained when only a particular segment of the program is required for the execution of the program.
The process may even become larger than all of the physical memory.
Given below are the drawbacks of using Virtual Memory:
Virtual memory reduces the stability of the system.
The performance of Virtual memory is not as good as that of RAM.
If a system is using virtual memory then applications may run slower.
Virtual memory negatively affects the overall performance of a system.
Virtual memory occupies the storage space, which might be otherwise used for long term data storage.
This memory takes more time in order to switch between applications.