Virtual memory is a feature of an operating system that enables a computer to be able to compensate shortages of physical memory by transferring pages of data from random access memory to disk storage.
This process is done temporarily and is designed to work as a combination of RAM and space on the hard disk.
This means that when RAM runs low, virtual memory can move data from it to a space called a paging file. This process allows for RAM to be freed up so that a computer can complete the task.
Occasionally a user might be shown a message that says the virtual memory is running low, this means that either more RAM needs to be added, or the size of the paging file needs to be increased.
How virtual memory works
Virtual memory uses both computer hardware and software to work. When an application is in use, data from that program is stored in a physical address using RAM. More specifically, virtual memory will map that address to RAM using a memory management unit (MMU). The OS will make and manage memory mappings by using page tables and other data structures. The MMU, which acts as an address translation hardware, will automatically translate the addresses.
If at any point later the RAM space is needed for something more urgent, the data can be swapped out of RAM and into virtual memory. The computer’s memory manager is in charge of keeping track of the shifts between physical and virtual memory. If that data is needed again, a context switch can be used to resume execution again.
While copying virtual memory into physical memory, the OS divides memory into pagefiles or swap files with a fixed number of addresses. Each page is stored on a disk, and when the page is needed, the OS copies it from the disk to main memory and translates the virtual addresses into real addresses.
However, the process of swapping virtual memory to physical is rather slow. This means that using virtual memory generally causes a noticeable reduction in performance. Because of swapping, computers with more RAM are seen to have better performance.
Types of virtual memory
A computer’s MMU handles memory operations, including managing virtual memory. In most computers, the MMU hardware is integrated into the CPU. There are two ways in which virtual memory is handled: paged and segmented.
Paging divides memory into sections or paging files, usually approximately 4 KB in size. When a computer uses up its RAM, pages not in use are transferred to the section of the hard drive designated for virtual memory using a swap file. A swap file is a space set aside on the hard drive as the virtual memory extensions of the computer’s RAM. When the swap file is needed, it’s sent back to RAM using a process called page swapping. This system ensures that the computer’s OS and applications don’t run out of real memory.
The paging process includes the use of page tables, which translate the virtual addresses that the OS and applications use into the physical addresses that the MMU uses. Entries in the page table indicate whether the page is in real memory. If the OS or a program doesn’t find what it needs in RAM, then the MMU responds to the missing memory reference with a page fault exception to get the OS to move the page back to memory when it’s needed. Once the page is in RAM, its virtual address appears in the page table.
Segmentation is also used to manage virtual memory. This approach divides virtual memory into segments of different lengths. Segments not in use in memory can be moved to virtual memory space on the hard drive. Segmented information or processes are tracked in a segment table, which shows if a segment is present in memory, whether it’s been modified and what its physical address is. In addition, file systems in segmentation are only made up of a list of segments mapped into a process’s potential address space.
Segmentation and paging differ as a memory model in terms of how memory is divided; however, it can also be combined. Some virtual memory systems combine segmentation and paging. In this case, memory gets divided into frames or pages. The segments take up multiple pages, and the virtual address includes both the segment number and the page number.
Benefits Of Virtual Memory
The primary benefits of virtual memory include freeing applications from having to manage a shared memory space, ability to share memory used by libraries between processes, increased security due to memory isolation, and being able to conceptually use more memory than might be physically available, using the technique of paging or segmentation.
Virtual memory vs. physical memory
When talking about the differences between virtual and physical memory, the biggest distinction is normally seen to be in speed. RAM is considerably faster than virtual memory. RAM, however, tends to be more expensive than virtual memory.
When a computer requires storage, RAM is the first used. Virtual memory is used when the RAM is filled, because it’s slower.
Users can actively add RAM to a computer by buying and installing more RAM chips if they are experiencing slowdowns due to memory swaps happening too often. The amount of RAM depends on what’s installed on a computer. Virtual memory, on the other hand, is limited by the size of the computer’s hard drive. Virtual memory settings can often be controlled through the operating system.