What is Linux Virtual Memory

admin

Ram Sticks In Motherboard.jpg

What is Linux Virtual Memory

Virtual memory is one of the foundations of modern operating systems, but usually, you don’t really think about it unless you run into a problem. Linux distributions require you to set up virtual memory space (swap partition) during installation, but most beginners don’t know how useful this is.

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

Recommendation:  WordPress Content Protection Plugin UnGrabber  

What is Linux Virtual Memory

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

In Linux, this is done at the kernel and hardware level. The CPU has a piece of hardware called a memory management unit (MMU) that translates physical memory addresses into virtual memory addresses. These addresses have nothing to do with where they are physically located on the machine. These address spaces are called “pages” and they may be in RAM or on your hard drive or SSD. The operating system sees these addresses as one large pool of memory, called the “address space”.

Virtual memory takes advantage of the fact that, in theory, not all of the memory that is in use is in use all the time. Programs in memory are broken up into pages, and parts that the kernel deems unnecessary are “swapped out,” or moved to the hard drive. When they are needed, they can be “swapped in,” or brought back to RAM.

The space on the drive used for virtual memory is called “backing store” or “swap space.” In the Windows world, it’s usually implemented as a file, called a “swap file.” This can also be done in Linux, but it’s more common to use a dedicated disk partition.

Swap files on Linux are usually reserved for minimal or embedded systems, the latter of which usually run without virtual memory at all because embedded operating systems have to be small.

A consequence of virtual memory is that large programs can be run by using more memory than the physical RAM in the machine has, similar to how a credit card lets you make large purchases with 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 overuse it.

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

The main disadvantage of virtual memory historically is that hard drives are slower than RAM. If a machine doesn’t have enough RAM, the system can endlessly swap pages, a process known as “thrashing.” On modern PCs with more RAM and faster SSDs replacing mechanical hard drives, this is less of a problem, but it’s still something to be aware of.

Linux swap partition

Linux swap partition

As mentioned earlier, the common way to set up virtual memory on Linux is to use a dedicated disk partition. The installation utility will check your hardware and propose a partitioning scheme that includes a swap partition.

You can also add a swap partition after installation. If you want to add a new partition to an existing drive, you must use a non-destructive partitioning tool such as GParted. Make sure you have selected “Linux swap” as the file system for the partition.

Back up important data before repartitioning the drive.

After creating the partition, format the partition using the mkswap command.

sudo mkswap /dev/sdX

Now you have to edit your /etc/fstab as root to add your swap partition. Adding this line to the file will set up a swap partition that is mounted at boot time:

/dev/sdX none swap defaults 0 0

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

sudo swapon /dev/sdX

Using Swap Files on Linux

It is 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 the drive or editing /etc/fstab. One way 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 use the dd command correctly, as errors in the input and output files can lead to data loss. For this reason, fallocate is the preferred method.

The /dev/zero  device is a special device that outputs “0”. What this dd command does is create a blank 2 GB block file with 1024 KB blocks suitable for use as swap space.

You can then use the mkswapswap and swaponswap commands to use a swap file just like you would a swap partition:

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

You may wonder when you should use a swap file or a dedicated partition for your swap space. The choice is simple: In most cases, you should use a partition. This is the best choice for a Linux desktop or server. The partitioning scheme suggested by the installer is usually suitable for a single-user Linux desktop.

If you are running Linux on a virtual machine, a small embedded system, or if you simply don’t want to repartition an existing Linux system, you may want to use a swap file.

How Much Swap Space?

For many years, the standard recommendation for how much swap space you need was twice the physical RAM. With even the cheapest PCs coming with large drives and lots of memory, this rule can be called 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 have your system set up this way.

Still, twice the physical RAM is a good starting point and insurance policy if you make higher demands on memory. You can make changes to your system as needed. If your system is using all of its RAM, your computer may experience performance issues when using virtual memory.

A computer can constantly swap in and out, a process called thrashing, making it appear unresponsive. If you also have a mechanical hard drive, you can often hear it being constantly accessed.

This isn’t as much of an issue these days, since the amount of RAM on even the cheapest PCs is more than adequate, and SSDs are much faster than older hard drives. It’s still something you should be aware of.

The easiest way to fix this is to add more RAM to your computer. If that’s not feasible, you can try adjusting the “swappiness” of the Linux kernel.

The swappiness number determines how much the kernel devotes to virtual memory. It ranges from 0 to 100. Setting it to 0 means Linux won’t swap at all, and at 100 it will swap all the time. The default for most systems is 60.


To temporarily change swappiness, use the sysctl command:

sudo sysctl vm.swappiness=20

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

Linux Virtual Memory Keeps Your System Running Smoothly

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

Many of this advice also applies to other systems, including Windows, even if the method of setting up virtual memory is different.

Recommendation: How to Convert a Webpage to PDF in Google Chrome


Leave a Comment