Virtual memory is one of the things that underlies modern operating systems, but usually you don’t really think about it unless you have a problem. Linux distributions ask you to set up your virtual storage space (swap partitions) during installation, but most beginners are unaware of how useful this 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 is abstracted from the physical memory on your computer. It uses both your RAM and your storage space, whether it’s on a traditional hard drive or an SSD.
On Linux, this happens at the kernel and hardware level. The CPU has hardware called the Memory Management Unit (MMU) that translates physical memory addresses to virtual ones. These addresses are independent of where they are physically located on the machine. These address spaces are called “pages” and can be in RAM or on your hard drive or SSD. The operating system views these addresses as one large pool of memory known as the “address space”.
Virtual memory takes advantage of the fact that not all theoretically used memory is used all the time. Programs in memory are broken into pages and the parts that the kernel deems unnecessary are “paged out” or moved to disk. When needed, they can be “paged out” or brought back into memory.
The space used for virtual memory on a drive is called “backing space” or “swap space.” In the Windows world, it is usually implemented as a file known as a “page file”. This is also possible on 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 entirely without virtual memory because embedded operating systems need to be small.
The result of virtual memory is that it’s possible to run large programs using more memory than the physical RAM in your computer, much like you can use a credit card to 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 don’t want to overuse it.
Virtual memory also allows developers to create applications without having to know how a computer’s memory is organized.
The biggest downside to virtual memory in the past was that hard drives were slower than RAM. When a computer does not have enough RAM, the system can swap 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 replacing mechanical hard drives, but it’s still something to be aware of.
Linux swap partitions
As mentioned, the usual way to set up virtual memory on Linux is to use a dedicated disk partition. The installation utility examines your hardware and suggests a partitioning scheme that includes a swap partition.
You can also add swap partitions after installation. If you want to add a new partition to an existing drive, you need to use a non-destructive partitioning tool like GParted. Make sure you have selected “Linux Swap” as the file system for your partition.
Back up important data before repartitioning your drive.
After creating your partition, use the mkswap command to format your partition.
sudo mkswap /dev/sdX
Now you need to edit yours /etc/fstab as root to add your swap partition. This line added to the file sets up a swap partition to be mounted at boot:
/dev/sdX none swap defaults 0 0
Now use the To deceive Command to activate your new swap space, where sdX is the name of your swap partition:
sudo swapon /dev/sdX
Using paging files on Linux
It’s easy to set up a paging file on Linux from the command line. You might want to do this if you don’t want to go through the hassle of repartitioning or editing your drive /etc/fstab. A method is to be used fall:
For example, to create a 2GB paging file:
sudo fallocate -l 2G /path/to/swapfile
Alternatively, you can use the dd command to create the paging file.
sudo dd if=/dev/zero of=/path/to/swapfile bs=1024 count=2048
Make sure you use the dd command correctly because wrong infile and outfile can lead to data loss. fallocate is the preferred method for this reason.
That /dev/null device is a special device that returns “0”. This dd command creates an empty 2 gigabyte block file with 1024 kilobyte blocks suitable for swap space.
You can then use the mkswap and To deceive Commands with a paging file like a paging partition:
sudo mkswap /path/to/swapfile
sudo swapon /path/to/swapfile
You may be wondering when to use a paging file or a dedicated partition for your swap space. The choice is simple: in most cases, you should use one partition. This is the best choice for a Linux desktop or server. The partition scheme suggested by the installer is usually reasonable on a single-user Linux desktop.
You may want to use a paging file if you’re running Linux in a virtual machine, on a small embedded system, or simply don’t want to repartition your existing Linux system.
How much swap space?
For many years, the standard advice for required swap space has been twice the physical RAM. With the large drives and RAM that come with even the cheapest PCs, this rule can be questioned.
In fact, if you check top or htop on many systems, you might find that if you set up your system this way, your swap space is not used at all.
Still, doubling the physical storage is a good place to start and insurance if you have higher storage requirements. You can make changes to your system if necessary. If your system is using all of your RAM, your computer may experience performance issues when you use virtual memory.
The computer can constantly cycle 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 is more than adequate even on the cheapest PCs and 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 simply add more RAM to your computer. If that is not possible, you can try to adjust the Linux kernel “swapping”.
The swap count determines how deep the kernel dives into virtual memory. It ranges from 0 to 100. If you set it to 0, Linux will not switch at all, while at 100 it will switch at every opportunity. The default value on most systems is 60.
To temporarily change the paging, use the sysctl command:
sudo sysctl vm.swappiness=20
The “20” in this command is the swap count 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 swap number you want. This will be a stopgap measure until you can install more RAM.
Virtual memory keeps your Linux system running smoothly
Virtual memory is a component of modern operating systems, including Linux, that keep your computer running smoothly. You can use a paging file, but the usual method is a dedicated partition. You don’t have to think about it that much, but Linux swap partitions and swap files are easy to set up and troubleshoot.
Much of this advice applies to other systems, including Windows, although the methods of setting up virtual memory differ.