Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Virtual Memory in OS

Last Updated on November 30, 2023 by Ankit Kochar

In the realm of modern computing, the concept of virtual memory serves as a pivotal component that enhances a system’s efficiency and capability. Operating Systems (OS) leverage virtual memory to manage the memory resources available, providing an illusion of a vast memory space while efficiently utilizing the physical RAM available in a computer. This article delves into the intricate workings of virtual memory, its significance in operating systems, and its impact on overall system performance.

What is Virtual Memory in OS?

Virtual memory in OS is a memory management technique that enables or allows the computer to execute larger programs or multiple programs together that would not be possible by using only the available physical memory. In simple words, we can also say that virtual memory in os makes an illusion of extra memory than available physical memory.
Virtual Memory in OS organizes the physical memory in various parts and keeps note of all the available and used memory and it is also known as the page table.

A computer can run the software by combining physical memory and hard disk space with virtual memory. Physical memory is loaded when a program is run, but if there is not enough space, the operating system will move some program components to the hard drive. This process is known as swapping.

The user is unaware of the swapping procedure because it takes place in the background. The operating system will swap the necessary portion of the program back into physical memory when the user tries to access a portion of it that has been moved to the hard drive.

How does Virtual Memory Work?

Virtual memory works by dividing a program into small sections called pages. Each page is typically 4 kilobytes in size. When a program is executed, the operating system loads some of the pages into physical memory. The pages that are not loaded into physical memory are stored on the hard disk.

The operating system uses a page table to keep track of which pages are in physical memory and which are on the hard disk. When a program attempts to access a page that is not in physical memory, the operating system generates a page fault. The page fault interrupts the program and transfers control to the operating system.

The operating system then reads the required page from the hard disk and stores it in physical memory. The operating system updates the page table to indicate that the page is now in physical memory, and then returns control to the program. The program can now access the required page, and the execution of the program continues.

Demand Paging

It is one of the memory management techniques that is used by operating systems. In this technique, only the pages that are required are loaded into the memory, rather than loading all the pages at the start of the program. This approach saves memory resources and allows the programs to run even if they require more memory than is available. It ulcer reduces the startup time of the programs and also makes the I/O faster. Because of this many programs can also run at the same time when one program gets the required page the other can get its required pages.

Page Frames

It is used to structure physical memory in OS. The size of the page frame is not fixed it varies between platforms in the power of 2.
Page frames are the blocks of memory used to store the pages of a program. Each page frame typically contains one page of memory. The total number of page frames available is determined by the amount of physical memory available in the system.

Page frames are used to store pages that are currently in use by a program. When a program needs to access a page that is not in memory, the operating system swaps a page frame out of memory to disk, freeing up the page frame for the required page.

Page Table

A page table is a data structure used by the operating system to keep track of the location of memory pages. In simple words, it can be referred to as a data structure used by the virtual memory in OS to record the relationship between physical and virtual addresses. The page table maps the virtual addresses used by the program to the physical addresses of the corresponding page frames. Each entry in the page table contains information about the page, including whether the page is currently in memory or on disk.

Swap In and Swap Out or Swapping

Swap in and swap out refer to the processes of moving pages of memory between physical memory and disk. When a process needs to access a page that is not currently in memory, the operating system swaps out a page frame from memory to make room for the required page. The swapped-out page is written to disk, freeing up the page frame for the required page. This process is called swap out.

When the required page is found on disk, the operating system swaps it into a free page frame in memory. This process is called swap-in. The operating system updates the page table to reflect the new location of the page in memory.

The frequency of swap-in and swap-out operations depends on the memory requirements of the running programs and the available physical memory. If the available physical memory is insufficient to hold all required pages, the operating system may need to swap pages in and out frequently, resulting in increased disk I/O and decreased system performance.


Thrashing is a phenomenon that occurs when the operating system spends more time swapping the pages in and out of the memory than it does in executing the program. There are many reasons for thrashing but the main reason is when a program has a high memory requirement, but the available physical memory is not enough to hold all the pages required together. And because of this, the operating system spends most of its time swapping the pages in and out of the memory instead of doing what it is required to do i.e, executing the program.
This results in a decrease in system performance and can lead to system instability. To avoid thrashing, the operating system may use techniques such as page replacement algorithms and increasing the available physical memory.

In the above diagram the degree of multiprogramming is increasing and with the CPU utilization is also increasing up to a certain point i.e, lambda(λ) at this point the degree of multiprogramming is maximum as the CPU is utilized is also maximum and all the resources are utilized 100%. Increasing the degree of multiprogramming after this will lead to a decrease in CPU utilization. And the time taken for page replacement will increase and the time for executing the program will increase this situation is known as thrashing.
For studying more about thrashing you can refer to thrashing.

Frame Allocation

Frame allocation is nothing but a process of allocating physical memory to a process or program. Simply speaking there are two types of frame allocations one is static in which the number of frames allocated to the process is fixed whereas in a dynamic number of frames allocated to the process is not fixed.
The operating system maintains a pool of free page frames that can be used to store pages of memory. When a process needs to access a page of memory that is not currently in memory, the operating system selects a free page frame and copies the required page from the disk into the selected frame. The selected frame is then marked as being in use by the process.

Paging Policies

Paging policies refer to the algorithms used by the operating system to manage page frames and decide which pages should be swapped in and out of memory. The most commonly used paging policies are the Least Recently Used (LRU) policy, the First In First Out (FIFO) policy, and the Clock policy.

The LRU policy selects the page frame that has not been accessed for the longest time and swaps it out to make room for the new page. The FIFO policy selects the oldest page frame and swaps it out. The Clock policy uses a circular buffer to maintain a list of page frames and swaps out the first frame that has not been accessed since the last time the buffer was scanned.

Benefits of Virtual Memory in OS

There are various benefits provided by virtual memory in OS some of them are given below:

  • It will enable the efficient use of physical memory by swapping the pages between the hard disk and physical memory.
  • We can also execute large programs which are larger than the available physical memory with the help of pages and only use the required pages at a given time.
  • We can run multiple programs simultaneously by swapping we can make sure only the required pages of each program stays in the memory.
  • It will make the use of the CPU effective as more processes are maintained in the memory which will result in the better use of resources.

Limitations of Virtual Memory in OS

Although virtual memory in os provides various benefits but align with that there are certain limitations of it that we have discusses in this blog section.

  • It will require significant overhead in terms of CPU cycles and memory usage. This overhead sometimes impacts the system’s performance.
  • It will lead to fragmentation which decreases the efficiency of memory usage. This will also result in slower system performance and larger memory requirements.
  • Frequent swapping will result in decreased system performance due to increase I/O.
  • It will lead to thrashing at certain times the operating system will be dealing with swapping the pages in and out of the memory and will not be focusing on executing the program.

In conclusion, virtual memory stands as an indispensable feature in contemporary operating systems, facilitating efficient memory management and enabling multitasking capabilities. Its ability to bridge the gap between physical RAM and storage devices ensures smooth operation by allowing the system to handle larger applications and multiple processes concurrently. Understanding the principles and mechanisms behind virtual memory is crucial for developers, system administrators, and computer enthusiasts, as it directly influences system performance and responsiveness.

Frequently Asked Questions Related to Virtual Memory in OS

Here are some of the frequently asked questions about virtual memory in os.

1. How is virtual memory managed in operating systems?
Virtual memory is managed by the operating system using a combination of page tables, page frames, and swapping.

2. How does virtual memory work?
When the physical RAM gets filled, the OS moves data from RAM to a space on the hard drive called the swap file or page file. This movement allows the RAM to be freed up for other tasks while still providing the illusion to running programs that they have access to a larger memory space.

3. What are the benefits of virtual memory?
Virtual memory enables efficient multitasking, allowing multiple programs to run simultaneously without overwhelming the physical RAM. It also provides a larger virtual memory space for applications than the available physical memory, which aids in running more extensive programs.

4. Can virtual memory affect system performance?
While virtual memory is essential for system stability, excessive use of it (heavy swapping between RAM and disk) can lead to performance degradation. When the system relies heavily on virtual memory due to insufficient physical RAM, it may experience slower response times because accessing data from the hard drive is significantly slower than from RAM.

5. How can one optimize virtual memory usage?
Optimizing virtual memory involves ensuring sufficient physical RAM, managing background applications, and adjusting the size of the page file to meet the system’s needs. Balancing these factors can help minimize excessive swapping and improve overall system performance.

Leave a Reply

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