If you spend time around serious Linux users, you’ll likely hear a reference to “swap space.” Or maybe you wondered what the swap partition that your Linux installation program created is. Virtual memory and swap space can be confusing. Here’s what you need to know on Linux.
What is virtual memory?
A more flexible view of memory
A lot of people conflate “swap” with “virtual memory.” Virtual memory is a technique that abstracts your computer’s memory over its physical RAM.
If you’re familiar with the concept of a virtual machine, where you can create an entire other computer in a program like VirtualBox to run different operating systems, virtual memory is a similar concept. Virtual memory is a representation of your computer’s memory that the operating system uses to present a picture of a memory that’s not limited by the RAM sticks in your machine.
There’s a piece of hardware on the CPU that makes this possible: the Memory Management Unit or MMU. The MMU splits the memory into “pages.” The MMU also prevents programs from interfering with each other, which is important in a multitasking, multiuser operating system like Linux.
The page sizes tend to be the same size as storage blocks on hard drives and SSDs, which is the main advantage of virtual memory: creating a large, unified address space that includes both the system RAM and part of the hard drive. This approach allows your system to run more and larger programs even when there might not be enough physical RAM in your machine to do so.
The technique was first developed for mainframes and minicomputers that had to be shared among multiple users. Virtual memory meant that there was less need for physical memory, which allowed customers to save money by buying machines with less physical RAM.
The operating system, with memory pages scattered across the RAM and the disk (I’ll use the term “disk” to refer to main storage, whether on a traditional spinning hard drive or SSD), can “swap” these pages in and out of the main memory. The operating system will tend to try to keep the pages actively being used “swapped in”, or in the physical RAM. That’s because RAM has been traditionally faster than the disk, especially in the days before SSDs were readily available.
This approach allows the programs, and parts of programs, that you’re actually using are active in the fast RAM instead of the slow disk. Disabling virtual memory could paradoxically mean a slower system, since all of the active programs would need to be held in memory. Many programs will create a lot of pages on startup, but the kernel will move them into the background while the high priority pages remain in RAM with virtual memory, according to Linux.com.
If you don’t have enough physical RAM in your system, the OS will perpetually swap pages in and out of RAM, unable to keep up, a problem made even worse by the slow speed of traditional hard drives. This makes the system practically unusable, a problem known as “thrashing.” Thrashing with inadequate RAM was why virtual memory got a bad rap among power users, or at least people who thought they were expert computer users, with some people even avoiding disabling it entirely. This is a bad idea. Having the actively used parts in the RAM with virtual memory is more efficient.
The other main concern with virtual memory with the modern deployment of SSDs is possible premature wearing out of the SSD with lower RAM counts, especially on modern laptops where the RAM is soldered to the motherboard and can’t be upgraded. There’s less evidence that this is a practical concern with normal desktop usage. If you’re doing intensive work or AAA gaming, you’ll want more RAM anyway.
What is swap space?
Parking pages in files or partitions
The area of the storage that holds swapped-out pages is known as the swap space.
Linux has two approaches for implementing swap space. The most common is to dedicate a disk partition. The rule of thumb for swap partitions was twice the physical RAM in the machine. This is still what most Linux installations will propose by default when you partition your disk. The disadvantage of this approach is that if you upgrade the RAM, you’ll have to grow the swap partition as well. Swap partitions are often the first partitions on disks due to how traditional hard drives work. It’s faster to seek toward the inside of a platter, so this is where installation programs will place the swap partition.
The other approach is a swap file, which is as the name suggests, a dedicated file in the Linux filesystem. It’s more fleixible than a dedicated swap partition, since it can be resized easily if the physical RAM changes. This is the approach that Windows uses for virtual memory. It’s much less common than the dedicated partition approach in the Linux world.
Swap partition or swap file?
Tradition vs flexibility
The dedicated partition approach seems to be the most common in Linux. Most people will probably just do what their Linux installation program does. The vast majority of linux installations still prefer swap partitions.
If you upgrade your RAM frequently or just prefer a simpler partition scheme, you might opt for a swap file. It’s also possible to run a system without any swap space at all, but you might find yourself running out of memory altogether. Linux systems without any swap are typically in niche applications such as embedded systems controllers.
How much swap space?
Rethinking the traditional “twice the physical RAM” rule
The traditional rule of thumb for how much swap space you need has been twice the physical memory. This was from an era where machines tended to have much less RAM than they do now. It might still hold because modern OSes, including Linux are much more capable. Most modern machines support hibernation, and that feature uses swap space to store the state of the system. If you make use of this feature, more swap space is better. You should regard the amount of physical RAM as the bare minimum of swap space.
The only way to fix thrashing
System stuttering? You probably need more RAM
If you are suffering from thrashing, you might think to try adjusting the “swappiness” of the system, or how aggressive it will swap pages in and out. This would only be a temporary fix.
You might not like the answer, especially with the price of RAM, but the only reliable way to fix swapping is to add more RAM
Linux’s unsung hero
Swap space is something that most people on modern Linux systems don’t think about until something goes wrong. Virtual memory makes modern desktop usage possible, but it can only go so far. Understanding how it works and its limitations will help you use swap space on Linux more effectively on your next installation.
- Operating System
-
Ubuntu Linux 22.04 LTS
- CPU
-
13th Gen Intel Core i7-1360P
- GPU
-
Intel Iris Xe Graphics
- RAM
-
16GB DDR5
- Storage
-
512GB SSD
- Weight
-
2.71 lbs
The Dell XPS 13 Plus with Linux combines powerful hardware and a great screen in a lightweight, great-looking chassis to create a fantastic Linux laptop.




