What is Virtual Memory?
processes. The high-order address bits referenced by each
instruction form the virtual page number (VPN). The PID
and VPN are combined to uniquely map to a physical
address set aside by the kernel as shown in Figure 1.
Low-order address bits represent offsets that directly
index into mapped pages in physical memory. The mapping
of virtual memory pages into physical memory is assigned
arbitrarily by the OS kernel. The kernel runs in real memory
rather than in virtual memory so that it can have direct access
to the computer's physical resources to allocate memory as
individual processes are executed and then terminated.
Despite each process having a 4 GB address space,
virtual memory can work on computers with just
megabytes of memory because the huge virtual address
spaces are sparsely populated. Most processes use only a
few hundred kilobytes to a few megabytes of memory and,
therefore, multiple processes that collectively have the
potential to reference tens of gigabytes can be mapped
into a much smaller quantity of real memory.
If too many processes are running simultaneously, or if
these processes start to consume too much memory, a computer can exhaust its physical memory resources, thereby
requiring some intervention from the kernel to either suspend a process or handle the problem in some other way.
When a process is initiated, or spawned, it is assigned
a PID and given its own virtual memory space. Some initial pages are allocated to hold its instructions and whatever data memory the process needs available when it
begins. During execution, processes may request more
memory from the kernel by calling predefined kernel
memory management routines. The kernel will respond
by allocating a page in physical memory and then returning a pointer to that page's virtual mapping. Likewise, a
process can free a memory region when it no longer
needs it. Under this circumstance, the kernel will remove
the mapping for the particular pages, enabling them to be
reallocated to another process — or the same process — at a
later time. Therefore, the state of memory in a typical multi-tasking OS is quite dynamic and the routines to manage
memory must be implemented in software due to their complexity and variability according to the platform and the
nature of processes running at any given time.
the majority of a process's program memory can be
stored on the hard drive without incurring a performance
penalty. When those instructions are ready to be executed, the OS kernel will have to transfer the data into physical memory. This slows the system down, but makes it
more flexible without requiring huge quantities of DRAM.
Part of the kernel's memory management function is
to decide which virtual pages should be held in DRAM and
which should be swapped out to the disk. Pages that have
not been used for a while can be swapped out to make
room for new pages that are currently needed. If a process
subsequently accesses a page that has been moved to the
disk, that page can be swapped back into DRAM to replace
another page that is not needed at the time. A computer
with 256 MB of DRAM could, for example, have a 512 MB
swap file on its hard drive, enabling processes to share a
combined 768 MB of used virtual memory.
This scheme of expanding virtual memory onto a disk
effectively turns the computer's DRAM into a large cache
for an even larger disk-based memory. A virtual memory
page that is not present in DRAM is effectively a cache
miss with a large penalty because hard disks are much
slower than DRAM. Such misses are called page faults.
The MMU detects that the requested virtual memory
address from a particular PID is not present in DRAM and
causes an exception that must be handled by the OS kernel. It is the kernel's responsibility to swap pages to and
from the disk. For a virtual memory system to function
with reasonable performance, the working set of memory
across all the processes running should be able to fit into
the computer's physical memory. The working set
includes any instructions and data that are accessed within a local time interval. Processes with good locality characteristics will do well in a virtual memory system.
Processes with poor locality may result in thrashing as
many sequential page faults are caused by random
accesses throughout a large virtual memory space.
The virtual to physical address mapping process is
guided by the kernel using a page table, which can take various forms, but must somehow map each PID/VPN combination to either a physical memory page or one located on
the disk drive's swap area. Virtual page mapping is illustrated in Figure 2, assuming 4 KB pages, a 32-bit address
space, and an 8-bit PID. In addition to basic mapping infor-
All mapped virtual memory pages do not have to be
held in physical RAM at the same time. Instead, the total
virtual memory allocation on a computer can spill over
into a secondary storage medium such as a hard drive.
The hard drive will be much slower than DRAM, but not
every memory page in every process is used at the same
time. When a process is first loaded, its entire instruction
image is typically loaded into virtual memory. However, it
will take some time for all of those instructions to reach
their turn in the execution sequence. During this wait time,
FIGURE 2. Virtual Page Mapping