Virtual memory is a memory management technique where secondary memory can be used as if it were a part of the main memory. Virtual memory is a very common technique used in the operating systems (OS) of computers.
Virtual memory uses hardware and software to allow a computer to compensate for physical memory shortages, by temporarily transferring data from random access memory (RAM) to disk storage. In essence, virtual memory allows a computer to treat secondary memory as though it were the main memory.
Today, most PCs come with up to around 4 GB of RAM. However, sometimes this isn't enough to run all the programs a user might want to use at once. This is where virtual memory comes in. Virtual memory can be used to swap data that has not been used recently -- and move it over to a storage device like a hard drive or solid-state drive (SDD). This will free up more space on the RAM.
Virtual memory is important for improving system performance, multitasking, using large programs and flexibility. However, users shouldn't rely on virtual memory too much, because using virtual data is considerably slower than the use of RAM. If the OS has to swap data between virtual memory and RAM too often, it can make the computer feel very slow -- this is called thrashing.
Virtual memory was developed at a time when physical memory -- also referenced as RAM -- was expensive. Computers have a finite amount of RAM, so memory can run out, especially when multiple programs run at the same time. A system using virtual memory uses a section of the hard drive to emulate RAM. With virtual memory, a system can load larger programs or multiple programs running at the same time, allowing each one to operate as if it has infinite memory and without having to purchase more RAM.
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.
How to manage virtual memory
Operating systems have default settings that determine the amount of hard drive space to allocate for virtual memory. That setting will work for most applications and processes, but there may be times when it's necessary to manually reset the amount of hard drive space allocated to virtual memory, such as with applications that depend on fast response times or when the computer has multiple HDDs.
When manually resetting virtual memory, the minimum and maximum amount of hard drive space to be used for virtual memory must be specified. Allocating too little HDD space for virtual memory can result in a computer running out of RAM. If a system continually needs more virtual memory space, it may be wise to consider adding RAM. Common operating systems may generally recommend users not increasing virtual memory beyond 1.5 times the amount of RAM.
Managing virtual memory may be a different experience on different types of operating systems, however. And IT professionals should understand the basics when it comes to managing physical memory, virtual memory and virtual addresses. For example, here are some tips on managing virtual memory on the Windows 10 operating system.
Benefits of using virtual memory
Benefits of virtual memory include:
- its ability to handle twice as many addresses as main memory;
- frees applications from managing shared memoryand saves users from having to add memory modules when RAM space runs out;
- increased security because of memory isolation;
- multiple larger applications can be run simultaneously;
- allocating memory is relatively cheap;
- doesn't need external fragmentation;
- effective CPU use;
- data can be moved automatically; and
- pages in the original process can be shared during a fork system call.
In addition, in a virtualized computing environment, administrators can use virtual memory management techniques to allocate additional memory to a virtual machine (VM) that has run out of resources. Such virtualization management tactics can improve VM performance and management flexibility.
- The use of virtual memory has its tradeoffs, particularly with speed. It's generally better to have as much physical memory as possible, so programs work directly from RAM or physical memory.
- The use of virtual memory slows a computer because data must be mapped between virtual and physical memory, which requires extra hardware support for address translations.
- The size of virtual storage is limited by the amount of secondary storage, as well as the addressing scheme with the computer system.
- Thrashing can happen if the amount of RAM is too small, which will make the computer perform slower.
- It may take time to switch between applications using virtual memory.
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.
Before virtual memory was developed, computers had RAM and secondary memory. Early computers used magnetic core memory for main memory and magnetic drums for their secondary memory. Computer memory was expensive and usually in short supply back in the 1940s and 1950s. As computer programs grew in size and complexity, developers had to worry that their programs would use up all of a computer's main memory and run out of memory.
In those early days, programmers used a process called overlaying to run programs that were larger than available memory. Parts of a program that weren't continually in use were set up as overlays that, when needed, would overwrite the existing overlay in memory. It required extensive programming to make overlaying work, and that was a key impetus for the development of automated virtual memory.
German physicist Fritz-Rudolf Güntsch has been credited with developing the concept of virtual memory in 1956 -- though this point is contested. Güntsch did, however, end up describing a form of cache memory.
The first apparent real instance of a virtual memory system came from the University of Manchester, in their attempt to develop a one-level storage system for the Atlas computer. The system used paging to map virtual addresses to a programmer on to the primary memory. Atlas was developed in 1959 and later commissioned in 1962.
In 1961, the first commercial computer with virtual memory was released by the Burroughs Corporation. This version of virtual memory used segmentation as opposed to paging.
In 1969, IBM researchers demonstrated that virtual memory overlay systems worked better than the earlier manual systems. Up until this point, there was still a debate over this. Mainframes and minicomputers in the 1970s generally used virtual memory. Virtual memory technology was not included in early personal computers because developers thought running out of memory would not be a problem in those machines. That assumption proved incorrect. Intel introduced virtual memory in the protected mode of the 80286 processor in 1982, and paging support when the 80386 came out in 1985.