Virtual Memory

It is most commonly used nowadays. Virtual Memory is a technique that provides an illusion to the user that a very large size of main memory. It is done by using some part of secondary memory as the main memory.

In this technique, those processes which have a larger size than RAM can also be loaded. For this purpose, OS does not load the entire process in the main memory. But it loads some parts of each process in the main memory.

In this way, more and more processes are accommodated in the main memory, and the main target of the degree of multi-programming is achieved.

Working of Virtual Memory 

Suppose some pages need to be loaded in RAM for the execution, and RAM is already full. Then the question is, how will these pages enter into RAM? The answer is that OS uses some Page Replacement algorithms (i.e., LRU) to load these pages in RAM. According to these algorithms, some of the RAM pages are swapped out as the entering of new pages. The SWAP OUT pages are shifted to secondary memory.

All this gives the user an illusion of unlimited RAM because all processes (even bigger than RAM) are executed successfully.

As we see in virtual memory, OS loads some pages of a process in the main memory instead of loading the entire process in the main memory.

The question is, which part of the process is loaded in RAM? Because we cannot decide in advance which part should be kept in the main memory and which should be in the secondary memory?

For this purpose, OS uses the Demand Paging Concept. Demand paging uses the Page replacement algorithms. All of this is explained in detail in the later lectures.

Explain Through Example

Suppose 2 processes, P1 and P2, each process contains 8 pages. The size of each page is 1 KB. The main memory contains 16 frames of 1 KB each. The OS resides in the first partitions. The first page of P1 is stored in the fifth partition, and the other partitions are also filled with the different pages of processes in RAM.

The page tables of both processes are also shown in the diagram

CPU registers contain the base address of each frame. So, in the diagram, the base address of Page Table 1 in the main memory is 5, and the base address of Page Table 2 is 6. The base address of any page table is added to the page number of the Logical address. In this way, OS finds the actual corresponding entry.

Virtual memory in OS

Advantages of Virtual Memory

  1. A degree of Multi-programming is achieved.
  2. Processes having even bigger the size of RAM are executed successfully.
  3. Cost-effective because the System can work through with smaller RAM size.

Disadvantages of Virtual Memory

  1. The system becomes slower because of swapping, switching, compaction, etc.
  2. Virtual memory consumes some space on the hard disk.