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.
Demand Paging
2.2.
Why Is Virtual Memory Required?
2.3.
How does Virtual Memory Work?
2.4.
Advantages of Virtual Memory
2.5.
Disadvantages of Virtual Memory
3.
Frequently Asked Questions
3.1.
How does Demand Paging operate?
3.2.
In what way does the operating system move data between RAM and Virtual Memory?
3.3.
What is the role of Page replacement algorithms in virtual memory?
3.4.
What is an address space?
3.5.
What is a Memory Manager?
4.
Conclusion
Last Updated: Mar 27, 2024

Virtual Memory

Author Divyansh Jain
2 upvotes
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM
Operating Systems

Introduction 

Do you also think that your device lacks some memory storage, and a little up-gradation in memory may help your device run smoother? But you don’t want to go out and buy more RAM and let me tell you the good news that you don’t actually need any extra memory, your PC already has it. Let’s explore it!

Virtual Memory is a type of storage that gives the user the illusion of having a large main memory. It's done by considering a section of secondary memory as primary memory. Let’s understand the concept of virtual memory in detail.

Recommended Topic, Microinstruction in Computer Architecture, Multiprogramming vs Multitasking

Virtual Memory

Virtual memory is a segment of volatile memory generated on the storage drive for a limited time. It occurs when a computer's RAM gets depleted as a result of several processes executing at the same time. Part of the storage drive is made accessible for usage as RAM by the operating system. Since processing power is consumed by moving data around rather than performing instructions, virtual memory is substantially slower than main memory.

The most obvious benefit of this approach is that programs that may be larger than physical memory can easily be stored in the main memory having less available size. Virtual memory has two functions. For instance, it allows us to use a disk to extend the use of physical memory. Second, since each virtual address is converted to a physical address, it allows us to have memory protection.

A memory management unit, or MMU, is incorporated into the hardware of modern microprocessors intended for general-purpose usage. It is the job of the MMU to convert virtual addresses to physical addresses. The following is a simple example:

Virtual Memory

Demand paging is a common way to implement virtual memory. It may be used in a segmentation system as well. Virtual memory can also be provided through demand segmentation.

Demand Paging

A demand paging mechanism is similar to a paging system with swapping, except that processes are maintained in secondary memory, and pages are loaded only when needed, rather than in preparation.

As a result, when a context switch occurs, the OS does not copy any of the old program's pages from disks or any of the new program's pages into the main memory. Instead, after loading the first page, it will begin executing the new program and fetch the program's pages, which are referenced.

Demand Paging

If a program addresses a page that may not be available in the main memory because it was swapped during execution, the processor treats it as an invalid memory reference. This is because page faults and transfers send the control back to the OS, which requests that the page be stored back into memory.

You can also read about - Shift Registers in Digital Electronics

Why Is Virtual Memory Required?

The following are some of the perks of using virtual memory:

  • When your computer's physical memory is full, it writes everything it needs to remember to the hard drive in the form of a swap file, which serves as virtual memory.
  • If a system running Windows requires additional memory/RAM, the system uses a small fragment of the hard drive for this purpose.

How does Virtual Memory Work?

Virtual memory has become extremely popular in today's world. It's used when a large number of pages need to be loaded into the main memory for execution and there's not enough RAM to do so. Instead of blocking pages from accessing the main memory, the OS searches for RAM space that is hardly used or which has not been referred into the secondary memory to free up space for the new pages in the main memory.

Now, let’s understand this process with the help of an example.

For example:

Assume that an operating system needs 500 MB of RAM to hold all of the active programs. However, the RAM currently has just 80 MB of physical memory accessible.

  • The OS will then allocate 420 MB of virtual memory and manage it using a program called the Virtual Memory Manager (VMM).
     
  • In this situation, the VMM will create a 420 MB file on the disk space to contain the additional RAM that is necessary.
     
  • The OS will now proceed to address memory, even though only 80 MB of space is available, as it counts 500 MB of real memory to be stored in the RAM.
     
  • It is the task of the VMM to help in managing 500 MB of memory while there is just 80 MB of real memory space available.

Advantages of Virtual Memory

Let’s talk about the pros of using Virtual Memory:

  • It enables you to execute many applications at the same time.
  • It's quite useful for setting up a multiprogramming environment.
  • When only a portion of the program is required for execution, virtual memory helps in speeding up the process.
  • When data or code is requested, it should be fetched from the disk.
  • Without needing relocation, the code can be stored anywhere in physical memory.
  • Data/code can be shared between memories.
  • Processes might expand to be bigger than all of the physical memory.
  • Each page is saved on a drive until it is needed after that, it is deleted.
  • Since virtual address space is more than physical memory, large applications can be built.
  • More processes should be kept in the main memory, allowing the CPU to be used more efficiently.

Disadvantages of Virtual Memory

Here, are the cons of using virtual memory:

  • Switching between applications is likely to take longer.
  • If the system is using virtual memory, applications may execute slower.
  • It degrades the system's stability.
  • Allows the user to access less hard drive space.
  • Take up storage space that could otherwise be used to store long-term data.
  • It doesn't perform as well as comparable to RAM.
  • It permits bigger apps to operate on systems with insufficient physical RAM to do so.
  • It has a negative impact on a system's overall performance.
     

You can also read about the memory hierarchy.

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
Bootcamp

Frequently Asked Questions

How does Demand Paging operate?

Demand paging is similar to paging with swapping in that processes are maintained in secondary memory, and pages are loaded only when needed, rather than in advance.

In what way does the operating system move data between RAM and Virtual Memory?

The concept named Swapping is used to move data between RAM and virtual memory. The operating system moves data from processes that aren't in use right now out of RAM and then into virtual memory. When the process is needed again, it transfers the data back into RAM.

What is the role of Page replacement algorithms in virtual memory?

When the CPU generates the virtual address to access the page which is not present in the main memory, a page fault occurs. To overcome this, the pages which are already present in the main memory need to be replaced by the demanded page which is present in the secondary storage. Here, page replacement algorithms come into play, and using several techniques the high-priority page is loaded into the main memory. 

What is an address space?

The actual RAM of a computer contains an address space, which is a set of integers that allows a computer to access any data in memory. 

What is a Memory Manager?

The virtual memory manager is a program in the operating system that breaks down each application into pieces and puts them into virtual memory, storing some bits in RAM and the rest in a specific file on the hard drive. Because of the way virtual memory works, each software believes it has the entire computer to itself.

Conclusion

To summarize the article, we discussed virtual memory in depth. And we also talked about demand paging and how it relates to virtual memory, along with the working of virtual memory and its pros and cons. We also talked about various doubts which occur frequently and some definitions to understand the topic more precisely.

Recommended Reading:

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!

Live masterclass