Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Virtual Memory
How Virtual Memory works?
Demand Paging 
Page Replacement
Causes of Thrashing
Recovery for Trashing
Advantages of Virtual Memory
Disadvantages of Virtual Memory
Frequently Asked Questions
What purpose does paging serve in an operating system?
When does thrashing take place?
What is demand paging and how does it work?
Last Updated: Mar 27, 2024

Virtual Memory

Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM
Operating Systems


Sometimes you might open too many chrome tabs in a system that consumes a lot of RAM which eventually leads to slowing down the performance of the Operating system. Although, it will not crash the whole OS. The reason behind it is that whenever the OS RAM gets full, it transfers some data into secondary memory, which helps the OS prevent a sudden crash. Virtual Memory is a storage allocation scheme that allows secondary memory to be addressed as if it were the main memory. Yet, this process will make the system slow so to avoid this situation it is recommended to upgrade RAM as well as Solid-state drive as it performs better than Hard drives.


In this article, we will learn more about virtual memory and its uses. 

Also See, FCFS Scheduling Algorithm Multiprogramming vs Multitasking

Virtual Memory

Virtual memory enables a computer to compensate for physical memory shortages by temporarily transferring data from random access memory (RAM) to disc storage. Mapping memory chunks to disc files allows a computer to treat secondary memory as if it were the main memory.

Virtual Memory

Source: wikipedia

The main objective of virtual memory is to increase the storage capacity of primary memory. Most PCs nowadays have at least 8 GB (gigabytes) of RAM. However, this is not always enough to run multiple programs at the same time. This is where virtual memory enters the picture. Virtual memory frees up RAM by transferring data that hasn't been used in a while to a storage device like a hard drive or solid-state drive (SSD).

Virtual memory was created at a time when physical memory, also known as RAM, was prohibitively expensive. Since computers have a limited amount of RAM, memory will eventually run out if multiple programs are running at the same time. A virtual memory system emulates RAM by using a section of the hard drive. A system can use virtual memory to load larger or multiple programs at the same time, allowing each one to operate as if it had more space without having to buy more RAM.

How Virtual Memory works?

Most computers today have like 4 to 8 GB of RAM available. Although that amount of RAM is enough to do daily tasks a user expects, if a user opens up a lot of programs at the same time then even that amount of RAM is insufficient to run all of those programs at the same time. 

For example, if a user opens up a number of programs more than the available amount of RAM can handle. At that time, If there was no such thing as virtual  memory, your computer would have to notify you, "I'm out of RAM." "You are unable to load any additional applications. Please abandon another  application before loading a new one.

This undoubtedly will irritate you. 

Source: WDRfree

With virtual memory, the computer can scan RAM for areas that haven't been used in a while and copy them to the hard disk. This frees up RAM for the new application to load. 😍

Since this copying occurs automatically, you are unaware of it, and it gives your computer the illusion of having unlimited RAM space despite the fact that it only has a limited amount of RAM installed. It also has a nice economic benefit because hard disc space is much cheaper than RAM chips.

The read/write speed of HDD is much slower than RAM, and the technology of a hard drive is not designed to access small chunks of data at a time. You will notice a significant performance drop if your system is forced to rely too heavily on virtual memory. The key is to have enough RAM to handle everything you tend to work on at the same time; then, the only time you "feel" the slowness of virtual memory is when you switch tasks. When this is the case, virtual memory is ideal.

The area of the hard disk that stores the RAM picture is known as a page file. It holds RAM pages on the hard disk, and the OS moves data back and forth between the RAM and the page file. 

You can also read about the Multilevel Feedback Queue Scheduling and Open Source Operating System.

Demand Paging 

Virtual memory can be implemented by Demand Paging or Demand Segmentation. Demand paging works on the principle that when a process is swapped in, its pages are not swapped in all at once. Instead, they are only used when the process requires them (On-demand) or a page fault occurs. 

In the page table, pages are not moved into memory but are designated as invalid. The rest of the table is empty when an entry is invalid. Pages loaded into memory are marked as valid, along with information about where the swapped-out page can be found.

Suppose a program addresses a page not present in the main memory because it was swapped out recently. In that case, the processor recognizes the invalid memory reference as a page fault and switches control from the program to the operating system, which requests the page back into memory.


  • Since only a portion of each process's page is loaded into memory, there is room for additional processes. This increases processor utilization because more processes can be in the ready state at the same time
  • Because each process uses less primary memory, multiprogramming levels can be increased.
  • Demand paging enables the execution of a process that consumes more memory than the main memory. The operating system loads the required pages of a process into the main memory.


  • Compared to simple paged management solutions, the number of tables and the amount of processing overhead for managing page interrupts are higher.

Page Replacement

Only a few pages of a process are loaded into memory at first, as discussed in Demand paging. This allows us to store more processes in memory at once. What happens, though, when a process wants more pages and there isn't enough free RAM to accommodate them? 

To address this issue, take some steps:

  • Place the process in the wait queue until any other process has completed its execution, freeing up frames.
  • Alternatively, to release frames, eliminate some other process from memory.
  • Alternatively, discover any pages that aren't being used at the moment and move them to the disc to free up frames. This is known as Page replacement, and it is the most widely used strategy. 


Thrashing refers to a process that spends more time paging than executing. To put it another way, the process doesn't have enough frames to hold all of the pages it needs to run, so it swaps pages in and out often to keep running. Occasionally, pages that will be needed shortly must be swapped out.

When the CPU utilization is low, the process scheduling mechanism loads numerous processes into memory simultaneously, allocating a limited number of frames to each process to improve the degree of multiprogramming. As the memory fills up, the process begins to take a long time to swap in the required pages, resulting in low CPU utilization because most processes are waiting for pages. As a result, the long-term scheduler loads more processes to increase CPU use; as a result, the entire system eventually comes to a halt. 

Causes of Thrashing

  1. High degree of multiprogramming:

If the number of processes in memory continues to grow, the number of frames assigned to each process will shrink. As a result, each process will have fewer frames available. As a result, page faults will become more frequent, more CPU time will be wasted switching in and out of pages, and utilization will continue to decline.

2. Lacks of Frames: 

When a process has fewer frames, fewer pages can be stored in memory, necessitating more frequent shifting in and out. This could result in thrashing. As a result, each process must be given a sufficient number of frames to avoid thrashing.

Recovery for Trashing

  • You can prevent the system from thrashing by ordering the long-term scheduler not to put the processes into memory after the threshold.
  • There are several techniques by which the thrashing can be avoided like working-set model, page fault frequency. 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Advantages of Virtual Memory

  • You can use virtual memory to run many applications at the same time and more processes can be kept in the main memory, resulting in better CPU use.
  • You may easily fit numerous huge programs into smaller programs using virtual memory.
  • A multiprogramming environment can be simply constructed with the use of virtual memory.
  • Data should be read from the Hard disk only when it is needed.
  • Common data can be easily exchanged between memory devices.
  • When only a portion of a program is required for execution, virtual memory increases speed.
  • The process may grow to be larger than all of the physical memory.

Disadvantages of Virtual Memory

  • Virtual memory's performance is inferior to that of RAM.
  • Applications may run slower if a system uses virtual memory it also harms the stability of the system and also decreases system overall performance.
  • Switching between applications takes longer with this memory.
  • Virtual memory takes up storage space that could otherwise be utilized to store long-term data.

You can also read about the interleave memory.

Frequently Asked Questions

What purpose does paging serve in an operating system?

Paging is a method of gaining faster access to data. When a program requires a page, it is available in main memory because the operating system copies a set number of pages from your storage device to main memory. Paging allows a process's physical address space to be noncontiguous.

When does thrashing take place?

Thrashing occurs in computer science when a computer's virtual memory resources are overutilized, resulting in a constant state of paging and page faults, which inhibits most application-level processing.

What is demand paging and how does it work?

Demand paging is a swapping process in the Virtual Memory system. In this process, all data is not transferred from the hard drive to main memory because, when using demand paging, data is transferred only when some programs receive demand.


To wrap up the session, Virtual memory is a type of storage that frequently gives the appearance of main memory. It stores programs in the form of pages. It is accomplished through the use of demand paging. The page replacement algorithm aids in determining which page should be executed. 

Recommended Reading:

To get hands-on practice with different types of questions on OS, you can visit the Operating System Track, a FREE Guided path. If you want to learn OS from an industry expert, check out a fantastic course on Operating systems

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy learning Ninja!

Previous article
Thrashing in OS (Operating System)
Next article
Memory Interleaving
Live masterclass