Virtual Memory in Operating System - GeeksforGeeks (2024)

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of the main memory. The addresses a program may use to reference memory are distinguished from the addresses the memory system uses to identify physical storage sites and program-generated addresses are translated automatically to the corresponding machine addresses.

What is Virtual Memory?

Virtual memory is a memory management technique used by operating systems to give the appearance of a large, continuous block of memory to applications, even if the physical memory (RAM) is limited. It allows the system to compensate for physical memory shortages, enabling larger applications to run on systems with less RAM.

A memory hierarchy, consisting of a computer system’s memory and a disk, enables a process to operate with only some portions of its address space in memory. A virtual memory is what its name indicates- it is an illusion of a memory that is larger than the real memory. We refer to the software component of virtual memory as a virtual memory manager. The basis of virtual memory is the noncontiguous memory allocation model. The virtual memory manager removes some components from memory to make room for other components.

The size of virtual storage is limited by the addressing scheme of the computer system and the amount of secondary memory available not by the actual number of main storage locations.

Working of Virtual Memory

It is a technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.

  • All memory references within a process are logical addresses that are dynamically translated into physical addresses at run time. This means that a process can be swapped in and out of the main memory such that it occupies different places in the main memory at different times during the course of execution.
  • A process may be broken into a number of pieces and these pieces need not be continuously located in the main memory during execution. The combination of dynamic run-time address translation and the use of a page or segment table permits this.

If these characteristics are present then, it is not necessary that all the pages or segments are present in the main memory during execution. This means that the required pages need to be loaded into memory whenever required. Virtual memory is implemented using Demand Paging or Demand Segmentation.

Types of Virtual Memory

In a computer, virtual memory is managed by the Memory Management Unit (MMU), which is often built into the CPU. The CPU generates virtual addresses that the MMU translates into physical addresses.

There are two main types of virtual memory:

  • Paging
  • Segmentation

Paging

Paging divides memory into small fixed-size blocks called pages. When the computer runs out of RAM, pages that aren’t currently in use are moved to the hard drive, into an area called a swap file. The swap file acts as an extension of RAM. When a page is needed again, it is swapped back into RAM, a process known as page swapping. This ensures that the operating system (OS) and applications have enough memory to run.

Demand Paging: The process of loading the page into memory on demand (whenever a page fault occurs) is known as demand paging. The process includes the following steps are as follows:

Virtual Memory in Operating System - GeeksforGeeks (1)

Demand Paging

  • If the CPU tries to refer to a page that is currently not available in the main memory, it generates an interrupt indicating a memory access fault.
  • The OS puts the interrupted process in a blocking state. For the execution to proceed the OS must bring the required page into the memory.
  • The OS will search for the required page in the logical address space.
  • The required page will be brought from logical address space to physical address space. The page replacement algorithms are used for the decision-making of replacing the page in physical address space.
  • The page table will be updated accordingly.
  • The signal will be sent to the CPU to continue the program execution and it will place the process back into the ready state.

Hence whenever a page fault occurs these steps are followed by the operating system and the required page is brought into memory.

What is Page Fault Service Time?

The time taken to service the page fault is called page fault service time. The page fault service time includes the time taken to perform all the above six steps.

Let Main memory access time is: mPage fault service time is: sPage fault rate is : pThen, Effective memory access time = (p*s) + (1-p)*m

Segmentation

Segmentation divides virtual memory into segments of different sizes. Segments that aren’t currently needed can be moved to the hard drive. The system uses a segment table to keep track of each segment’s status, including whether it’s in memory, if it’s been modified, and its physical address. Segments are mapped into a process’s address space only when needed.

Combining Paging and Segmentation

Sometimes, both paging and segmentation are used together. In this case, memory is divided into pages, and segments are made up of multiple pages. The virtual address includes both a segment number and a page number.

Virtual Memory vs Physical Memory

When talking about the differences between virtual memory and physical memory, the biggest distinction is speed. RAM is much faster than virtual memory, but it is also more expensive.

When a computer needs storage for running programs, it uses RAM first. Virtual memory, which is slower, is used only when the RAM is full.

FeatureVirtual MemoryPhysical Memory (RAM)
DefinitionAn abstraction that extends the available memory by using disk storageThe actual hardware (RAM) that stores data and instructions currently being used by the CPU
LocationOn the hard drive or SSDOn the computer’s motherboard
SpeedSlower (due to disk I/O operations)Faster (accessed directly by the CPU)
CapacityLarger, limited by disk spaceSmaller, limited by the amount of RAM installed
CostLower (cost of additional disk storage)Higher (cost of RAM modules)
Data AccessIndirect (via paging and swapping)Direct (CPU can access data directly)
VolatilityNon-volatile (data persists on disk)Volatile (data is lost when power is off)

What is Swapping?

Swapping is a process out means removing all of its pages from memory, or marking them so that they will be removed by the normal page replacement process. Suspending a process ensures that it is not runnable while it is swapped out. At some later time, the system swaps back the process from the secondary storage to the main memory. When a process is busy swapping pages in and out then this situation is called thrashing.

Virtual Memory in Operating System - GeeksforGeeks (2)

Swappinghierar

What is Thrashing?

At any given time, only a few pages of any process are in the main memory, and therefore more processes can be maintained in memory. Furthermore, time is saved because unused pages are not swapped in and out of memory. However, the OS must be clever about how it manages this scheme. In the steady state practically, all of the main memory will be occupied with process pages, so that the processor and OS have direct access to as many processes as possible. Thus when the OS brings one page in, it must throw another out. If it throws out a page just before it is used, then it will just have to get that page again almost immediately. Too much of this leads to a condition called Thrashing. The system spends most of its time swapping pages rather than executing instructions. So a good page replacement algorithm is required.

In the given diagram, the initial degree of multiprogramming up to some extent of point(lambda), the CPU utilization is very high and the system resources are utilized 100%. But if we further increase the degree of multiprogramming the CPU utilization will drastically fall down and the system will spend more time only on the page replacement and the time taken to complete the execution of the process will increase. This situation in the system is called thrashing.

Causes of Thrashing

Virtual Memory in Operating System - GeeksforGeeks (3)

1. High Degree of Multiprogramming: If the number of processes keeps on increasing in the memory then the number of frames allocated to each process will be decreased. So, fewer frames will be available for each process. Due to this, a page fault will occur more frequently and more CPU time will be wasted in just swapping in and out of pages and the utilization will keep on decreasing.

For example:
Let free frames = 400
Case 1: Number of processes = 100
Then, each process will get 4 frames.

Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per process are decreased. Hence CPU time will be consumed just by swapping pages.

2. Lacks of Frames: If a process has fewer frames then fewer pages of that process will be able to reside in memory and hence more frequent swapping in and out will be required. This may lead to thrashing. Hence a sufficient amount of frames must be allocated to each process in order to prevent thrashing.

Recovery of Thrashing

  • Do not allow the system to go into thrashing by instructing the long-term scheduler not to bring the processes into memory after the threshold.
  • If the system is already thrashing then instruct the mid-term scheduler to suspend some of the processes so that we can recover the system from thrashing.

Performance in Virtual Memory

  • Let p be the page fault rate( 0 <= p <= 1).
  • if p = 0 no page faults
  • if p =1, every reference is a fault.
Effective access time (EAT) = (1-p)* Memory Access Time + p * Page fault time.
Page fault time = page fault overhead + swap out + swap in +restart overhead

The performance of a virtual memory management system depends on the total number of page faults, which depend on “paging policies” and “frame allocation

Frame Allocation

A number of frames allocated to each process in either static or dynamic.

  • Static Allocation: The number of frame allocations to a process is fixed.
  • Dynamic Allocation: The number of frames allocated to a process changes.

Paging Policies

  • Fetch Policy: It decides when a page should be loaded into memory.
  • Replacement Policy: It decides which page in memory should be replaced.
  • Placement Policy: It decides where in memory should a page be loaded.

Applications of Virtual memory

Virtual memory has the following important characteristics that increase the capabilities of the computer system. The following are five significant characteristics of Lean.

  • Increased Effective Memory: One major practical application of virtual memory is, virtual memory enables a computer to have more memory than the physical memory using the disk space. This allows for the running of larger applications and numerous programs at one time while not necessarily needing an equivalent amount of DRAM.
  • Memory Isolation: Virtual memory allocates a unique address space to each process and that also plays a role in process segmentation. Such separation increases safety and reliability based on the fact that one process cannot interact with and or modify another’s memory space through a mistake, or even a deliberate act of vandalism.
  • Efficient Memory Management: Virtual memory also helps in better utilization of the physical memories through methods that include paging and segmentation. It can transfer some of the memory pages that are not frequently used to disk allowing RAM to be used by active processes when required in a way that assists in efficient use of memory as well as system performance.
  • Simplified Program Development: For case of programmers, they don’t have to consider physical memory available in a system in case of having virtual memory. They can program ‘as if’ there is one big block of memory and this makes the programming easier and more efficient in delivering more complex applications.

How to Manage Virtual Memory?

Here are 5 key points on how to manage virtual memory:

1. Adjust the Page File Size

  • Automatic Management: All contemporary operating systems including Windows contain the auto-configuration option for the size of the empirical page file. But depending on the size of the RAM, they are set automatically, although the user can manually adjust the page file size if required.
  • Manual Configuration: For tuned up users, the setting of the custom size can sometimes boost up the performance of the system. The initial size is usually advised to be set to the minimum value of 1. To set the size of the swap space equal to 5 times the amount of physical RAM and the maximum size 3 times the physical RAM.

2. Place the Page File on a Fast Drive

  • SSD Placement: If this is feasible, the page file should be stored in the SSD instead of the HDD as a storage device. It has better read and write times, and the virtual memory may prove benefecial in an SSD.
  • Separate Drive: Regarding systems having multiple drives involved, the page file needs to be placed on a different drive than the os and that shall in turn improve its performance.

3. Monitor and Optimize Usage

  • Performance Monitoring: Employ the software tools used in monitoring the performance of the system in tracking the amounts of virtual memory. High page file usage may signify that there is a lack of physical RAM or that virtual memory needs a change of settings or addition in physical RAM.
  • Regular Maintenance: Make sure there is no toolbar or other application running in the background, take time and uninstall all the tool bars to free virtual memory.

4. Disable Virtual Memory for SSDs (with Sufficient RAM)

  • Sufficient RAM: If for instance your system has a big physical memory, for example 16GB and above then it would be advised to freeze the page file in order to minimize SSD usage. But it should be done, in my opinion, carefully and only if the additional signals that one decides to feed into his applications should not likely use all the available RAM.

5. Optimize System Settings

  • System Configuration: Change some general properties of the system concerning virtual memory efficiency. This also involves enabling additional control options in Windows such as adjusting additional system setting option on the operating system, or using other options in different operating systems such as Linux that provides different tools and commands to help in adjusting how virtual memory is utilized.
  • Regular Updates: Ensure that your drivers are run in their newest version because new releases contain some enhancements and issues regarding memory management.

Advantagesof Virtual Memory

  • More processes may be maintained in the main memory: Because we are going to load only some of the pages of any particular process, there is room for more processes. This leads to more efficient utilization of the processor because it is more likely that at least one of the more numerous processes will be in the ready state at any particular time.
  • A process may be larger than all of the main memory: One of the most fundamental restrictions in programming is lifted. A process larger than the main memory can be executed because of demand paging. The OS itself loads pages of a process in the main memory as required.
  • It allows greater multiprogramming levels by using less of the available (primary) memory for each process.
  • It has twice the capacity for addresses as main memory.
  • It makes it possible to run more applications at once.
  • Users are spared from having to add memory modules when RAM space runs out, and applications are liberated from shared memory management.
  • When only a portion of a program is required for execution, speed has increased.
  • Memory isolation has increased security.
  • It makes it possible for several larger applications to run at once.
  • Memory allocation is comparatively cheap.
  • It doesn’t require outside fragmentation.
  • It is efficient to manage logical partition workloads using the CPU.
  • Automatic data movement is possible.

Disadvantages of Virtual Memory

  • It can slow down the system performance, as data needs to be constantly transferred between the physical memory and the hard disk.
  • It can increase the risk of data loss or corruption, as data can be lost if the hard disk fails or if there is a power outage while data is being transferred to or from the hard disk.
  • It can increase the complexity of the memory management system, as the operating system needs to manage both physical and virtual memory.

Conclusion

In conclusion, virtual memory is a crucial feature in operating systems that allows computers to run larger applications and handle more processes than the physical RAM alone can support. By using techniques like paging and segmentation, the system extends the available memory onto the hard drive, ensuring that the operating system and applications can operate smoothly. Although virtual memory can introduce some performance overhead due to the slower speed of hard drives compared to RAM, it provides significant benefits in terms of memory management, efficiency, and multitasking capabilities.

Frequently Asked Questions on Virtual Memory – FAQs

Why is virtual memory important?

Virtual memory is important because it allows the system to handle larger applications and multiple applications simultaneously without running out of physical memory. It helps in optimizing the use of available RAM and provides a buffer against memory shortages.

What is a page file or swap space in virtual memory?

Page file is also known as Virtual memory file in windows while Swap space is known as page file in Unix/Linux It is a disk area assigned by the operating system to act as extension of physical memory. If RAM is saturated, then finally inactive page of memory is swapped to this area, to provide the occupied RAM for more active processes. This process is known as paging or swapping.

Can virtual memory affect system performance?

Virtual memory may affect the efficiency of the operation of the computer. Too much usage of the virtual memory will result in what is referred to as a thrashing where the system spends most of its time swapping data between the RAM and the disk with little time executing applications thus, slows down the system. This can be solved by having enough physical RAM and to correctly setup the page file or swap space.

What is a page fault?

A page fault occurs when a program tries to access data that is not currently in RAM. The operating system then retrieves the required page from the hard drive and loads it into RAM.



Virtual Memory in Operating System - GeeksforGeeks (4)

GeeksforGeeks

Virtual Memory in Operating System - GeeksforGeeks (5)

Improve

Previous Article

Segmentation in Operating System

Next Article

Page Replacement Algorithms in Operating Systems

Please Login to comment...

Virtual Memory in Operating System - GeeksforGeeks (2024)

FAQs

What is virtual memory in an operating system? ›

Virtual memory is a common technique used in a computer's operating system (OS). Virtual memory uses both hardware and software to enable a computer to compensate for physical memory shortages, temporarily transferring data from random access memory (RAM) to disk storage.

What is virtual memory in operating system geeksforgeeks? ›

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of the main memory.

What is the difference between RAM and virtual memory? ›

Virtual memory and physical memory serve distinct roles in a computer system. Physical memory (RAM) is the actual hardware that provides temporary storage for active data, while virtual memory is a reserved space on the storage device (usually a hard drive or SSD) used when physical memory is exhausted.

What is the difference between cache and virtual memory? ›

1. Cache Memory is the high speed of computer memory that reduces the access time of files or documents from the main memory. Virtual Memory is a logical unit of computer memory that increases the capacity of main memory by storing or executing programs of larger size than the main memory in the computer system. 2.

What is the main disadvantage of virtual memory? ›

Disadvantages of Virtual Memory

Thrashing is a phenomenon where a computer with little RAM always alternates between virtual and physical memory. Performance lags may result from the computer becoming slower as a result. A computer's available memory is usually decreased if it has little secondary storage.

Where is virtual memory stored? ›

Virtual memory is an area of a computer system's secondary memory storage space, such as an HDD or SSD, that acts as if it were a part of the system's RAM or primary memory. Ideally, the data needed to run applications is stored in RAM, where the CPU can quickly access it.

How to use virtual memory? ›

Click Start > Settings > Control Panel. Double-click the System icon. In the System Properties dialog box, click the Advanced tab and click Performance Options. In the Performance Options dialog, under Virtual memory, click Change.

What is the main memory in OS? ›

Main memory is the place where programs and information are kept when the processor is effectively utilizing them. Main memory is associated with the processor, so moving instructions and information into and out of the processor is extremely fast. Main memory is also known as RAM (Random Access Memory).

Can we replace RAM with virtual memory? ›

Increasing virtual memory can be a temporary fix for managing multitasking when physical RAM is low, but it's not as effective as upgrading actual RAM. Physical RAM operates at much higher speeds, offering faster data access and improved multitasking without the performance drawbacks of virtual memory.

Why is virtual memory needed? ›

Virtual memory enables data that is in RAM and not currently being used to be transferred to the hard disk. This frees up room in RAM for other programs and data. When the data on the hard disk is needed again, any other unused data is transferred to the hard disk before the original data is transferred back to RAM.

What is the best size for virtual memory? ›

Microsoft recommends that you set virtual memory to be no less than 1.5 times and no more than 3 times the amount of RAM on your computer. For power PC owners (like most UE/UC users), you likely have at least 2GB of RAM so your virtual memory can be set up to 6,144 MB (6 GB).

What is a virtual memory example? ›

For example, suppose a program requires 5 GB of memory, but the computer system has only 4 GB of RAM installed. In this case, the virtual memory system will allocate additional memory needed by creating a larger virtual address space and mapping it to both the available RAM and the computer's secondary storage device.

What are the advantages of virtual memory? ›

Benefits of virtual storage

Allowing computers to run programs that are larger than their physical memory. Running multiple programs at once at a slower speed. Increasing the efficiency of how your computer uses its physical memory.

Is virtual memory temporary or permanent? ›

Virtual memory uses both hardware and software to enable a computer to compensate for physical memory shortages, temporarily transferring data from random access memory (RAM) to disk storage.

How much virtual memory should I set for 16GB RAM? ›

However, with advancements in technology and more efficient operating systems, this rule is not as rigid as it once was. For a system with 16GB RAM, setting the virtual memory between 24GB to 32GB is a good starting point, but adjustments might be needed based on specific use cases.

How much virtual memory should I set for 8GB RAM? ›

As a rule of thumb, the paging file should be a minimum of 1.5 times the size of your installed RAM, and a maximum of 3 times your RAM size. For example, if you have 8 GB RAM, your minumum would be 1024 x 8 x 1.5 = 12,288 MB, and your maximum would be 1024 x 8 x 3 = 24,576 MB.

Should I increase virtual memory? ›

Note: Microsoft recommends that virtual memory be set at no less than 1.5 times and no more than 3 times the amount of RAM on the computer. For power PC owners (most UE/UC users), there is likely at least 2 GB of RAM, so the virtual memory can be set up to 6,144 MB (6 GB).

Top Articles
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 5838

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.