Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Virtual Memory
2.1.
How Virtual Memory works?
2.2.
Demand Paging 
2.2.1.
Advantages
2.2.2.
Disadvantages
2.3.
Page Replacement
2.4.
Thrashing 
2.4.1.
Causes of Thrashing
2.4.2.
Recovery for Trashing
3.
Advantages of Virtual Memory
4.
Disadvantages of Virtual Memory
5.
Frequently Asked Questions
6.
Key Takeaways 
Last Updated: Jun 30, 2023

Virtual Memory

Author Jainish Patel
0 upvote

Introduction

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 Drive.

Source:memegenerator

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

Virtual Memory

As the complexity and functionalities of desktop applications are increasing, the primary memory(RAM) requirements are also increasing. Like PC games, photo editing software like Photoshop and even the Chrome browser require a lot of primary memory(RAM). The virtual memory concept helps in overcoming this dilemma without crashing the OS.

The main objective of virtual memory is to increase the storage capacity of primary memory. Virtual memory is the operating system's sizable secondary memory. It allows the computer system's hardware and software to support physical memory while transferring data from primary memory to secondary memory, such as a hard drive. At any time, the primary memory is not available, but at the same time, the pages need some extra memory, then requests are forwarded to the hard disk for swapping files like virtual memory. It occupies the small block of memory in the hard disk.

How Virtual Memory works?

P1, P2, and P3 are the processes that need to be executed they are currently available in the logical Address Space. As there is limited memory space available only some part of each process is stored in the memory line Page’0’ of P1 and Page Table of P1. 

As per the degree of programming is concern, more and more processes should be included in the main memory. But as you know, the main memory is limited. So, the Main Memory is divided into parts called as 'Frames' and the process is divided into 'Pages' so that a part of the process(a page) can be accommodated in a frame(part of Main Memory). A Page Table keeps track of the pages and where they are present in the Main Memory. So, only a few pages from each process are loaded in the main memory from the Logical Address Space. After that whenever the process is executed by the CPU the required pages are loaded into the primary memory using the Swap IN and Swap OUT function also known as Roll IN and Roll OUT function. So whenever a user calls for any process it creates an illusion that all the processes are available in the primary memory but this is not the case only a few pages of the process are available in the main memory. The page replacement is done through the Page replacement Algorithms. 

Consider that the CPU is executing Process P1 and looking to P1 in Page Table but the P1 page is not available this is known as Page Fault. It can be solved using Page Fault Handling read further about it in this article.

Demand Paging 

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). Only those pages are loaded at first which will be required by the procedure right away.

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.

Advantages

  • Large virtual memory.
  • More efficient use of memory.
  • There is no limit on the degree of multiprogramming.

Disadvantages

  • 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. We have some fantastic algorithms for efficiently replacing pages.

Thrashing 

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 level 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 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.

  1. Lacks of Frames: 

When a process has fewer frames, fewer pages may 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.
  • If the system is already thrashing, tell the mid-term scheduler to pause some of the processes until the system is no longer thrashing.

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.

Frequently Asked Questions

1. What purpose does paging serve in an operating system?

In an operating system, paging is used to solve the problem of external fragmentation. This method ensures that the data you require is provided as soon as possible.

2. When does thrashing take place?

Thrashing signifies a high level of paging activity. When it spends more time paging than executing, this happens.

3. What is demand paging and how does it work?

If a section of memory is not currently in use, it is swapped to a Hard disc to make place for an application's needs.

Key Takeaways 

This article discusses what virtual memory is in the operating system and related concepts. 

To get hang on and practice 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

Live masterclass