Virtual memory is one of those things that underpin modern OSes but usually, you don't really think about it unless you have a problem. Linux distros ask you to set up your virtual memory space (swap partitions) during installation, but most beginners aren't aware of how useful that is.

Here's everything you need to know about virtual memory on Linux.

What Is Virtual Memory?

Virtual memory is a way of representing your memory that's abstracted from the physical memory on your machine. It makes use of both your RAM and your storage space, whether that's on a traditional hard drive or an SSD.

In Linux, this is done at the kernel and hardware levels. The CPU has a piece of hardware called a Memory Management Unit (MMU) that translates physical memory addresses into virtual ones. These addresses are independent of where they physically reside on the machine. These address spaces are known as "pages" and they could be in RAM or on your hard drive or SSD. The OS sees these addresses as one big pool of memory, known as an "address space."

Virtual memory takes advantage of the fact that not all of the memory that's being used in theory is being used all of the time. Programs in memory are broken down into pages and the parts that the kernel deems as unnecessary are "swapped out," or moved to the hard drive. When they're needed, they can be "swapped in," or brought back into RAM.

The space used for virtual memory on a drive is known as "backing store," or "swap space." In the Windows world, it's usually implemented as a file, known as a "swap file." It's also possible to do this in Linux, but it's much more common to use a dedicated disk partition.

Swap files on Linux are usually reserved for minimal or embedded systems, and it's common for the latter to run without virtual memory completely because embedded OSes have to be small.

The upshot of virtual memory is that it's possible to run large programs by using more memory than the physical RAM in your machine, similar to how a credit card lets you make large purchases for more money than you have in your bank account. Like a credit card, virtual memory is useful when you need it but you don't want to overextend its use.

Virtual memory also allows developers to create applications without having to know how a computer's memory is organized.

The main downside historically with virtual memory has been that hard drives have been slower than RAM. If a machine doesn't have enough RAM, the system can keep swapping pages in and out endlessly, a process known as "thrashing." This is less of an issue on modern PCs with more RAM and faster SSDs displacing mechanical hard drives, but it's still something to be aware of.

Linux Swap Partitions

Debian installer partition scheme with swap partition

As mentioned previously, the usual method for setting up virtual memory on Linux is to use a dedicated disk partition. The installation utility will examine your hardware and propose a partitioning scheme that includes a swap partition.

You can also add swap partitions after the installation. If you want to add a new partition to an existing drive, you'll have to use a nondestructive partitioning tool like GParted. Make sure you've selected "Linux swap" as the filesystem for your partition.

Back up important data before repartitioning your drive.

After you've created your partition, use the mkswap command to format your partition.

        sudo mkswap /dev/sdX
    

Now you'll have to edit your /etc/fstab as root to add your swap partition. This line, added to the file, will set up a swap partition to be mounted at boot time:

        /dev/sdX none swap defaults 0 0
    

Now use the swapon command to activate your new swap space, where sdX is the name of your swap partition:

        sudo swapon /dev/sdX
    

Using Swap Files on Linux

It's easy to set up a swap file in Linux using the command line. You might want to do this if you don't want to bother repartitioning your drive or editing /etc/fstab. One method is to use fallocate:

For example, to create a 2GB swap file:

        sudo fallocate -l 2G /path/to/swapfile
    

As an alternative, you can use the dd command to create the swap file.

        sudo dd if=/dev/zero of=/path/to/swapfile bs=1024 count=2048
    

Make sure you're using the dd command correctly, as getting the infile and outfile wrong can cause data loss. fallocate is the preferred method for this reason.

The /dev/zero device is a special device that outputs "0." What this dd command does is create a blank two-gigabyte block file using 1024-kilobyte blocks suitable for use as swap space.

You can then use the mkswap and swapon commands with a swap file as you would with a swap partition:

        sudo mkswap /path/to/swapfile
sudo swapon /path/to/swapfile

You might wonder when you should use a swap file or a dedicated partition for your swap space. The choice is easy: in most circumstances, you should use a partition. This is the best choice for a Linux desktop or a server. The partition scheme suggested by the installer is usually adequate on a single-user Linux desktop.

You might want to use a swap file if you're running Linux in a virtual machine, on a small embedded system, or you just don't want to repartition your existing Linux system.

How Much Swap Space?

For many years, the standard advice for how much swap space is needed is twice the physical RAM. With the large drives and amounts of memory that even the cheapest PCs come with, this rule may come into question.

On many systems, if you check top or htop, you might even notice that your swap space isn't even being used at all if you set up your system this way.

htop showing virtual memory in use

Still, twice the physical memory is a good starting point and an insurance policy if you make greater demands on the memory. You can make changes to your system as necessary. If your system is using all of your RAM, the computer may run into performance problems when using virtual memory.

The computer can keep swapping in and out, a process known as thrashing, making it appear unresponsive. If you still have a mechanical hard drive, you can often hear it constantly accessing.

This is much less of an issue these days as the amount of RAM on even the cheapest PCs is more than adequate, plus the speed of SSDs is much faster than the old hard drives. It's still something you should be aware of.

The easiest way to fix this is to just add more RAM to your machine. If that's not possible, you might try to adjust the "swappiness" of the Linux kernel.

The swappiness number determines how much the kernel dips into virtual memory. It ranges from 0 to 100. Setting it to 0 means that Linux won't swap at all, while at 100, it'll swap at every opportunity. The default on most systems is 60.

To change the swappiness temporarily, use the sysctl command:

        sudo sysctl vm.swappiness=20
    

The "20" in that command will be the swappiness number until you reboot. To change it permanently, edit the /etc/sysctl.conf file as root and place the line, "vm.swappiness=[swappiness number]", where "[swappiness number]" is the swappiness number you want. This will be a stopgap until you can install more RAM.

Virtual Memory Keeps Your Linux System Running Smoothly

Virtual memory is a component of modern OSes, including Linux, that keeps your computer running smoothly. You can use a swap file, but the usual method is a dedicated partition. You don't have to think about it as much, but Linux swap partitions and swap files are easy to set up and troubleshoot.

A lot of this advice is also applicable to other systems, including Windows, even if the methods for setting up virtual memory are different.