Simplistically, when a memory location is read or written to, hardware within the computer translates the memory address generated by the software (the virtual memory address) into a, usually distinct, real memory address (the physical memory address) within the computer's memory. This is accomplished by preserving the low order bits of the binary representation of the input address while treating the high order bits as a key to one or more address translation tables. For this reason a range of consecutive addresses in the virtual address space whose size is a power of two will be translated in a corresponding range of consecutive physical addresses. The memory referenced by such a range is called a page. The page size is typically in the range of 512 to 8192 bytes (with 4K being very common), though page sizes as large as 32KB have been known. Using the same or a related mechanism, contiguous regions of virtual memory larger than a page are often mappable to contiguous physical memory for purposes other than virtualization, such as setting access and cache control bits.
The translation is implemented by an MMU. This may be either a module of the CPU or an auxiliary, closely coupled chip. The MMU may have the ability to monitor page references according to the type of reference (for read, write or execution) and the privilege mode of the CPU at the time the reference was generated. In addition, the MMU may detect that a reference is to a page that is marked as unavailable. The MMU responds to such conditions by raising an exception with the CPU which will be trapped by system software. This makes it possible for an operating system to carry out swapping. This is the behaviour of copying one page of memory to disk in order to restore to memory another page copied to disk earlier. This can be realized in a swap partition, a dedicated section of disk in order to hold paged memory, or in a swap file.
The most fundamental advantage of virtual memory is that it allows a computer to multiplex its CPU and memory between multiple programs without the need to perform expensive copying of the programs' memory images. It also allows the operating system to protect its own code from corruption by an erroneous application program and to protect application programs from each other and (to some extent) from themselves. If the combination of virtual memory system and operating system supports swapping, then the computer may be able to run simultaneously programs whose total size exceeds the available physical memory. This is possible because most programs have a small subset (active set) of pages that they reference over significant periods their execution. If too many programs are run at once, then copies to and from disk will become excessively frequent and overall system performance will become unacceptably slow. This is often called thrashing (since the disk is being excessively overworked - thrashed) or paging storm and corresponds to access to the swap medium being three orders of magnitude or more slower than access to main memory.
Note that virtual memory is not a requirement for precompilation of software, even if the software is to be executed on a multiprogramming system. Precompiled software may loaded by the operating system which has the opportunity to carry out address relocation at load time. This suffers by comparison with virtual memory in that a copy of program relocated at load time cannot run at a distinct address once it has started execution.
It is possible to avoid the overhead of address relocation using a process called rebasing, which uses metadata in the executable image header to guarantee to the run-time loader that the image will only run within a certain virtual address space. This technique is used on the system libraries on Win32 platforms, for example.
In embedded systems, swapping is typically not supported—virtual memory is primarily a convenience to cope with software errors.
See also: CPU design, computer, memory management