What is virtual memory?
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 common technique used in a computer's
operating system (OS) as it is effective at optimizing memory usage.
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. Mapping chunks of memory to disk files enables a computer to treat secondary memory
as though it were main memory.
Today, most PCs come with at least 8 gigabytes (GB) of RAM. But, sometimes, that is not enough
to run several programs at the same time. Virtual memory frees up RAM by swapping data that
has not been used recently over to a storage device, such as a hard drive or solid-state drive (SSD).
Virtual memory is important for improving system performance, multitasking and using large
programs. However, users should not overly rely on virtual memory, since it is considerably slower
than RAM. If the OS must swap data between virtual memory and RAM too often, the computer
begins to slow down, resulting in a condition called thrashing.
Virtual memory was developed at a time when physical RAM-based memory was expensive.
Computers have a finite amount of RAM, so memory eventually runs out when multiple programs
run simultaneously. A system using virtual memory uses a section of a hard drive or SSD to
emulate RAM. With virtual memory, a system can load larger or multiple programs running at the
same time, enabling each one to operate as if it has more disk allocation space, without having to
purchase more RAM.
How virtual memory works
Virtual memory uses both hardware and software to operate. When an application is in use, data
from that program is stored in a physical address using RAM. A memory management unit (MMU)
maps the address to RAM and automatically translates addresses. The MMU can, for example,
map a logical address space to a corresponding physical memory address.
If, at any point, the RAM space is needed for something more urgent, data can be swapped out of
RAM and into virtual memory. The computer's memory manager oversees keeping track of the
shifts between physical and virtual memory. If that data is needed again, the computer's MMU
uses a context switch to resume execution.
While copying virtual memory into physical memory, the OS divides memory with a fixed number
of addresses into either pagefiles or swap files. 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 using
virtual memory generally causes a noticeable reduction in performance. Because of swapping,
computers with more RAM are considered to have better performance.
What are the benefits of using virtual memory?
The advantages to using virtual memory include the following:
• It can handle twice as many addresses as main memory.
• It enables more applications to be used at once.
• It frees applications from managing shared memory and saves users from having to add
memory modules when RAM space runs out.
• It can increase speed when only a segment of a program is needed for execution.
• It increases security because of memory isolation.
• It enables multiple larger applications to run simultaneously.
• Allocating memory is relatively inexpensive.
• It does not need external fragmentation.
• CPU use is effective for managing logical partition workloads.
• Data can be moved automatically.
• Pages in the original process can be shared during a fork system call operation that creates a
copy of itself.
In addition to these benefits, 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.
A virtual memory manager can juggle the processes from multiple applications to make additional
memory space available to them.
What are the limitations of using virtual memory?
Although the use of virtual memory has its benefits, it also comes with some tradeoffs worth
considering, such as the following:
• Applications run slower if they are running from virtual memory.
• Data must be mapped between virtual and physical memory, which requires extra hardware
support for address translations and may also affect performance.
• 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 occur if there is not enough RAM, which makes the computer perform slower.
• It may take time to switch between applications using virtual memory.
• Virtual memory reduces the amount of available drive space.