Why is RAM called virtual memory?

Linux supports virtual memory, that is, using a disk as an extension of RAM so that the effective size of usable memory grows correspondingly. The kernel will write the contents of a currently unused block of memory to the hard disk so that the memory can be used for another purpose. When the original contents are needed again, they are read back into memory. This is all made completely transparent to the user; programs running under Linux only see the larger amount of memory available and don't notice that parts of them reside on the disk from time to time. Of course, reading and writing the hard disk is slower (on the order of a thousand times slower) than using real memory, so the programs don't run as fast. The part of the hard disk that is used as virtual memory is called the swap space.

Linux can use either a normal file in the filesystem or a separate partition for swap space. A swap partition is faster, but it is easier to change the size of a swap file (there's no need to repartition the whole hard disk, and possibly install everything from scratch). When you know how much swap space you need, you should go for a swap partition, but if you are uncertain, you can use a swap file first, use the system for a while so that you can get a feel for how much swap you need, and then make a swap partition when you're confident about its size.

You should also know that Linux allows one to use several swap partitions and/or swap files at the same time. This means that if you only occasionally need an unusual amount of swap space, you can set up an extra swap file at such times, instead of keeping the whole amount allocated all the time.

A note on operating system terminology: computer science usually distinguishes between swapping (writing the whole process out to swap space) and paging (writing only fixed size parts, usually a few kilobytes, at a time). Paging is usually more efficient, and that's what Linux does, but traditional Linux terminology talks about swapping anyway.

Virtual memory is an area of a computer system’s secondary memory storage space (such as a hard disk or solid state drive) which acts as if it were a part of the system’s RAM or primary memory.

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.

Why is RAM called virtual memory?

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.

Why is RAM called virtual memory?

Paul Rubens

Paul Rubens is a technology journalist based in England and is an eSecurity Planet and Datamation contributor.

Why it is called virtual memory?

A computer can address more memory than the amount physically installed on the system. This extra memory is actually called virtual memory and it is a section of a hard disk that's set up to emulate the computer's RAM. The main visible advantage of this scheme is that programs can be larger than physical memory.

Why is RAM better than virtual memory?

Virtual memory is used when the RAM is filled. Virtual memory is slower than physical memory, so it can decrease the performance of applications.

What is the difference between memory and virtual memory?

The main difference between physical and virtual memory is that the physical memory refers to the actual RAM of the system attached to the motherboard, but the virtual memory is a memory management technique that allows the users to execute programs larger than the actual physical memory.