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

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.