Virtual Memory in the Operating System


Virtual memory in an operating system 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. It is a technique that allows the execution of processes that are not completely in the physical memory.This technique is useful as a large virtual memory is provided for user programs when a very small physical memory is there. It is a part of the secondary storage that gives the user an illusion that is a part of the main memory.

Why do we need virtual memory?

There can be two reasons for using virtual memory −

  • If a computer runs on windows and requires more memory or ram, it uses a small portion of the hard drive for this purpose.

  • When your computer does not have space in the physical memory it writes what it needs to remember to the hard disk in a swap file as virtual memory.

How does virtual memory work?

Virtual memory is used whenever some pages require to be loaded in the main memory for the execution, and the memory is not available for those pages.

So, in that case, instead of preventing pages from entering in the main memory, the operating system searches for the RAM space that is at least used in the recent times or that is not referenced into the secondary memory to make the space for the new pages in the main memory.

Since all this procedure happens automatically, therefore it makes the computer feel like it is having unlimited RAM.

Types of virtual memory

The two ways computers handle virtual memory are through paging and segmentation.

Paging

It is a technique of memory management that breaks the process address space into various blocks of similar sizes, known as pages. Here, we measure the size of a process in the total number of pages.

Segmentation

In this technique every job gets divided into various blocks of varied sizes, known as segments. This way we get one segment for every module with pieces performing related functions. These segments act as different spaces of the logical address of any program.

Virtual memory is commonly implemented using demand paging.

Demand Paging

Demand paging is a popular method of virtual memory management. On demand paging, the pages of a process that are least used, get stored in the secondary memory. A page is copied to the main memory when its demand is made or page fault occurs.

There are various page replacement algorithms which are used to determine the pages which will be replaced.

Types of Page Replacement Methods

Here are some important page replacement methods

  • FIFO

  • Optimal algorithm

  • LRU page replacement

FIFO Page Replacement

FIFO(First -in-first-out) is a simple implementation method that selects the page for a replacement that has been in the virtual address of the memory for the longest time.

Features

  • The page that recently comes in the memory is removed whenever a new page is loaded. So it is easy to decide which page requires to be removed as its identification number is always at the FIFO stack.

  • The oldest page in the main memory is one that should be selected for replacement first.

Optimal Algorithm

Optimal page replacement algorithm that we use for replacing pages. This algorithm replaces the page whose demand in the future is least as compared to other pages from frames. The replacement occurs when the page fault appears.

Features

  • Optimal algorithm results in the fewest number of page faults.This algorithm is difficult to implement.

  • Replace the page which is unlikely to be used for a longer period of time. It only uses the time when a page needs to be used.

LRU Page Replacement

This method helps the operating system to find page usage over a short period of time.This algorithm should be implemented by associating a counter with an even page.

Features

  • It keeps a list and replaces pages by looking back into time.

  • The LRU replacement method has the highest count. This counter is also called the aging register which specifies their age and how much their associated pages should also be referenced.

Advantages of virtual memory

  • It allows more applications to be run at the same time.

  • Each page is stored on a disk until it is required after that, it will be removed.

  • Data/code should be read from disk whenever required.

  • Large programs should be written, as virtual address space available is more compared to physical memory.

  • It helps you to fit many large programs into smaller programs.

  • It allows you to run more applications at once.

Disadvantages of virtual memory

  • It negatively affects the overall performance of a system.

  • It reduces system stability.

  • It does not offer the same performance as RAM.

  • Likely takes more time to switch between applications.

  • Occupy the storage space, which may be used otherwise for long term data storage.

  • Applications may run slower if the system is using virtual memory.

  • It offers less hard drive space for your use.

Conclusion

Virtual memory is an elegant scheme for giving programs a private memory space (starting at address 0) with size limited only by available disk space, and with security between running programs.

Updated on: 23-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements