Ideally, the data needed to run applications is stored in RAM, where they can be accessed quickly by the CPU. But when large applications are being run, or when many applications are running at once, the system’s RAM may become full.
To get around this problem, some data stored in RAM that is not actively being used can be temporarily moved to virtual memory (which is physically located on a hard drive or other storage device). This frees up space in RAM, which can then be used to accommodate data which the system needs to access imminently.
By swapping data between RAM and virtual memory when it is not needed and back from virtual memory to RAM when it is needed, a system can continue to work smoothly with far less physical RAM than it would otherwise require.
Virtual memory enables a system to run larger applications or run more applications at the same time without running out of RAM. Specifically, the system can operate as if its total RAM resources were equal to the amount of physical RAM, plus the amount of virtual RAM.
Why is There a Need for Virtual Memory?
Virtual memory was developed when physical RAM was very expensive, and RAM is still more expensive per Gigabyte than storage media such as hard disks and solid state drives. For that reason it is much less costly to use a combination of physical RAM and virtual memory than to equip a computer system with more RAM.
Since using virtual memory (or increasing virtual memory) has no extra financial cost (because it uses existing storage space) it offers a way for a computer to use more memory than is physically available on the system.
Another key driver for the use of virtual memory is that all computer systems have a limit (dictated by hardware and software) on the amount of physical RAM that can be installed. Using virtual memory allows the system to continue to operate beyond those physical RAM limits.
Virtual Memory vs. Physical Memory
Since RAM is more expensive than virtual memory, it would seem – all things being equal – that computers should be equipped with as little RAM and as much virtual memory as possible.
But in fact the characteristics of virtual memory are different than those of physical memory. The key difference between virtual memory and physical memory is that RAM is very much faster than virtual memory.
So a system with 2 GB of physical RAM and 2 GB of virtual memory will not offer the same performance as a similar system with 4 GB of physical RAM. To understand why, it is necessary to understand how virtual memory works.
How Does Virtual Memory Work?
When an application (including the operating system) is running, it stores the location of program threads and other data at a virtual address, while the data is actually stored at a physical address in RAM. If later that RAM space is needed more urgently by another process, then the data may be swapped out of RAM and into virtual memory.
The responsibility for keeping track of all this data as it is swapped between physical and virtual memory falls to the computer’s memory manager. The memory manager maintains a table which maps virtual addresses used by the operating system and applications to the physical addresses that data is actually stored in. When data is swapped between RAM and virtual memory, the table is updated so that a given virtual address always points to the correct physical location.
A computer can only run threads and manipulate data that is stored in RAM rather than virtual memory. And it takes a non-negligible amount of time to swap data that is needed into RAM. Consequently, it follows that using virtual memory involves a performance hit.
Put another way, a system with 4 GB RAM will generally offer higher performance than a system with 2 GB RAM and 2 GB virtual memory because of the performance hit caused by swapping, and for that reason it is said that virtual memory is slower than RAM.
One potential problem with virtual memory is that if the amount of RAM present is too small compared to the amount of virtual memory then a system can end up spending a large proportion of its CPU resources swapping data back and forth. Meanwhile, performance of useful work grinds to a near halt – a process known as thrashing.
To prevent thrashing it is usually necessary to reduce the number of applications being run simultaneously, or simply to increase the amount of RAM in the system.
Operating systems, such as most versions of Windows, generally recommend that users do not increase virtual memory beyond 1.5 times the amount of physical RAM present. So a system with 4 GB RAM should have virtual memory of no more than 6 GB.
To minimize the performance hit caused by swapping between physical and virtual memory, it is best use the fastest storage device connected to the system to host the virtual memory, and to locate the virtual memory storage area on its own partition.
Virtual memory can act in concert with a computer’s main memory to enable faster, more fluid operations.
How to Increase Virtual Memory in a System
Most operating systems allow users to increase virtual memory from a configuration page.
- In Windows, users can also allow the system to manage the amount of virtual memory provided dynamically.
- Similarly, in the Mac OS, users can use the preferences panel to allot virtual memory.
Types of virtual memory: Paging and Segmentation
Virtual memory can be managed in a number of different ways by a system’s operating system, and the two most common approaches are paging and segmentation.
Virtual Memory Paging
In a system which uses paging, RAM is divided into a number of blocks – usually 4k in size – called pages. Processes are then allocated just enough pages to meet their memory requirements. That means that there will always be a small amount of memory wasted, except in the unusual case where a process requires exactly a whole number of pages.
During the normal course of operations, pages (i.e. memory blocks of 4K in size) are swapped between RAM and a page file, which represents the virtual memory.
Virtual Memory Segmentation
Segmentation is an alternative approach to memory management, where instead of pages of a fixed size, processes are allocated segments of differing length to exactly meet their requirements. That means that unlike in a paged system, no memory is wasted in a segment.
Segmentation also allows applications to be split up into logically independent address spaces, which can make them easier to share, and more secure.
But a problem with segmentation is that because each segment is a different length, it can lead to memory “fragmentation.” This means that as segments are allocated and de-allocated, small chunks of memory can be left scattered around which are too small to be useful.
As these small chunks build up, fewer and fewer segments of useful size can be allocated. And if the OS does start using these small segments then there are a huge number to keep track of, and each process will need to use many different segments, which is inefficient and can reduce performance.
Advantages and Disadvantages of Virtual Memory
Even though RAM is now relatively inexpensive compared to its cost when virtual memory was first developed, it is still extremely useful and it is still employed in many, perhaps most, computer systems. The key problem with virtual memory relates to performance.
Advantages of Virtual Memory
- Allows more applications to be run at the same time.
- Allows larger applications to run in systems that do not have enough physical RAM alone to run them.
- Provides a way to increase memory which is less costly than buying more RAM.
- Provides a way to increase memory in a system which has the maximum amount of RAM that its hardware and operating system can support.
Disadvantages of Virtual Memory
- Does not offer the same performance as RAM.
- Can negatively affect the overall performance of a system.
- Takes up storage space which could otherwise be used for long term data storage.