Virtual Memory in Operating System

FREE Online Courses: Click, Learn, Succeed, Start Now!

Apart from the physically installed memory, a computer also has some extra memory that is known as virtual memory. It is a storage scheme that allows the secondary memory to be addressed as a part of main memory. It is a part of the hard disk, set up to copy the RAM.

This uses hardware and software to compensate for physical memory shortages and transfers data from RAM to disk storage temporarily. Though it is slower than RAM it increases system performance and multiprogramming.

It is a memory space where programs store themselves in the form of pages. There are two main purposes of virtual memory:

1. Use disk to extend the use of physical memory
2. Provides memory protection, as each virtual address is translated to a physical address.

The conditions, when an entire program needs not to be fully loaded in main memory are:

  • If an error occurs in data or computation, then user written error handling routines are used to dissolve this error.
  • Certain options and features of a program are quite rare to see and are not used commonly.
  • Multiple tables, even though only a small amount of these tables is used, have a fixed amount of address space addressed to them.
  • The ability to execute a program that is only partially in memory counters a lot of benefits.
  • It requires a lesser number of I/O to load or swap each user program into memory.
  • A program is no longer bound by the amount of available physical memory.
  • User programs take less physical memory, and multiple programs are run in one go. This increases CPU utilization and throughput.

How virtual memory works in OS

RAM helps store the data of a program in a physical address, when it is in use. A memory management unit maps this address to RAM and translates addresses automatically. When the RAM needs to be free for a more urgent task than the one currently running that data is swapped out of RAM into virtual memory.

The memory manager keeps track of these shifts between physical and virtual memory in case the data is needed again for resuming execution. When the OS copies virtual memory into physical memory, it divides the memory into either pagefiles or swap files and stores these pages on a disk. When needed, the OS copies the pages back into the main memory and translates the virtual addresses into real addresses.

How to manage virtual memory in Operating System

There are default settings in a system to manage as they can determine the amount of hard drive space to allocate for virtual memory. These settings work for almost all the applications and processes, but there are times when manual intervention is required to reset the amount of hard drive space.

It is necessary to specify the minimum and maximum amount of hard drive space while resetting as allocating too little space can make the system run out of RAM. But if the system continuously demands more virtual memory space, then it is better to add more RAM. Some OSes often recommend not increasing the virtual memory beyond 1 ½ times the amount of RAM.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Virtual memory management differs in every OS. RAM cells have a limited number of writes which may reduce the lifespan of the drive. To configure virtual memory manually:

1. Open the ‘Control Panel’ on your computer and double-click on the ‘System’. In the system, click on the ‘Performance’ tab and select ‘Virtual Memory’

2. There you will see an option saying, ‘Let me specify my own virtual memory settings’. Click on it and the options below it will become active.

3. A drop-down list beside ‘Hard disk:’ appears. Select any hard drive for which you want to configure virtual memory. Make sure that you divide the virtual memory equally.

4. Enter the smallest amount of hard drive space in the ‘Minimum:’ box and the value in ‘Maximum:’ can be anything less than twice the physical RAM.

5. 12 megabytes is usually the Windows default. Restart your computer to put the new settings into effect.

Virtual Memory vs Physical Memory in OS

Virtual RAMRAM
A part of RAMPhysical memory
SlowerFaster
Uses pagingUses segmentation
Indirect access to CPUDirect access to CPU
Limited by the size of RAM and computer hard driveLimited to the RAM chip size

Demand Paging

We can implement virtual memory using Demand Paging or Demand Segmentation. Demand paging is the process of loading a page into system memory on demand when page fault occurs.

During context switching the OS does not copy any of the old program’s pages out to the disk or any of the new program’s pages into the main memory. Instead, the OS starts executing the new program after loading the first page and then goes on fetching other pages.

Page fault occurs if a page is unavailable in the main memory during execution of a program. The processor gives control to the OS that demands the page back into the memory.When a page fault occurs, the OS follows the following steps to bring the required page into memory:

1. If the CPU refers to a page that is unavailable in the main memory, then an interrupt is generated that indicates memory access fault.

2. The OS then puts the interrupted process in a blocking state so that the execution can complete.

3. The OS then looks for this required page in the logical address space and brings it from logical address space to physical address space. This is done as it is necessary to bring the required page into memory for the continuation of the execution.

4. Page replacement algorithms help make decisions regarding the replacement of the page in physical address space.

5. Once the page table updates, a signal is sent to the CPU to continue the program execution and it places the process back into ready state.

Advantages:

  • Since only some of the pages of any process will be loaded into the memory, there is room for more processes. This increases utilization of the processor as there is a possibility that more processes can be in the ready state at a time.
  • Demand paging allows the execution of a process larger than the main memory. The OS loads the required pages of a process in main memory.
  • Less primary memory usage by each process allows greater multiprogramming levels.

Disadvantages:

  • Greater number of tables and processor overhead for handling page interrupts

Virtual memory in OS

A memory management unit is built into the modern microprocessor for general-purpose and helps translate virtual address into physical address.

Virtual memory in Operating System

Page Replacement Algorithm

Page replacement algorithms help the OS decide which memory page(s) to swap out and write to the disk when a page needs to be allocated. A free page cannot be allocated as the number of free pages is lower than the required pages. Thus, paging occurs whenever a page fault occurs.

If a paged out page needs to be read in from the disk, it is referenced again. This is required for I/O completion. The less the waiting time for page-ins, the better is the algorithm. A page replacement algorithm selects the pages that should be replaced to minimize the total number of page misses and balance it with the costs of primary storage and processor time of the algorithm itself. This is done with the help of the limited information known about the pages provided by the hardware.

First In First Out (FIFO) algorithm

In this algorithm, the oldest page in the main memory is selected for replacement. It is easy to implement, keeps a list, and replaces pages from the tail (end) and adds new pages at the head (start).

First In First Out algorithm

Optimal Page algorithm

This algorithm has the lowest page-fault rate among all the algorithms. It is also called OPT or MIN. It replaces the page that remains unused for the longest time.

Optimal Page algorithm

Least Recently Used (LRU) algorithm

In this algorithm, the page that has stayed unused for the longest time in main memory is the one to be replaced. It is easy to implement, maintains a list, and replaces pages by looking back into time.

Least Recently Used Algorithm

Page Buffering algorithm

In this algorithm, we need to maintain a pool of free frames to get a process. Then we select a page to be replaced on page fault and write the new page in the frame of the free pool. We mark the page table and restart the process. Then we write the dirty page out of disk and replace it with the frame holding page in the free pool.

Least frequently Used algorithm (LFU)

In this algorithm, we replace the page with the smallest count. During the initial phase of a program, a page is used heavily and then never used again.

Most frequently Used algorithm (MFU)

In this algorithm, the page which was recently brought has the smallest count and has not been used yet.

Thrashing in Operating System

Thrashing is when a process spends more time paging rather than executing. Thus, the frames that the process has can’t hold all the pages for its execution, resulting in frequent in and out swapping of pages. Sometimes swapping of pages that may be required in the near future also happens. The computer spends a large amount of time transferring pages to and fro the backing store, resulting in thrashing.

This can be reduced by adding real memory, improving application design, scheduling, and through memory usage. Sometimes, reducing the number of active tasks on the system also helps. It swaps out the entire working set of the processes by reducing demand on real memory

Advantages of Virtual Memory in OS

Following are the advantages of Virtual Memory in Operating System:

  • We can run multiple applications at the same time.
  • We can fit many large programs into smaller programs.
  • Implementing a multiprogramming environment is easier.
  • It ensures effective utilization of the processor.
  • We can read data from the disk when required.
  • It is easy to share common data between memories.
  • It speeds up a process when only a particular segment of the program is required for the execution.
  • The process size can be larger than that of the physical memory.

Disadvantages of Virtual Memory in Operating System

Following are the drawbacks of Virtual Memory in OS:

  • It reduces system stability.
  • Poor performance as compared to that of RAM.
  • Applications run slower.
  • It affects the overall performance of a system negatively.
  • This occupies the storage space that can be used for long term data storage
  • It takes a lot of time to switch between applications.

Summary

Virtual memory is a storage segment that often gives the illusion of main memory. It stores programs in the form of pages. It is implemented with the help of demand paging. Page replacement algorithm helps decide the page to be executed. There are other algorithms like FIFO, optimal page, LRU, page buffering algorithm, LFU, and MFU.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *