As recent computers include more and more memory, we could wonder if swap is still required on Linux systems. In short, the swap partition is like a backup for your RAM: do you still need it? Why and when it is used? I’ll answer all your questions in this article.
On Linux systems, the swap space is used when the system needs more memory, but the RAM is already full. In this case, the kernel will move some memory pages to a swap partition, located on the computer hard drive.
Using the hard drive will be slower, but it’s still better than waiting for some applications to free up memory. Keep reading if you want to understand this process better, why it’s still used on powerful computers, and the commands you can use to tweak the swap usage.
The goal of swap on Linux systems
Let’s start by the basics, what is the swap partition on Linux and why do you need it.
What is swap on Linux?
On Linux systems, swap usually is a dedicated partition, created to assist the physical memory. When the RAM is full, the system will move content from inactive applications to the swap partition to free up some space in the physical memory.
On modern systems, we have applications asking for resources all the time: your web browser each time you open a new page, the core system, the antivirus that runs in the background, etc. The kernel will use a complex algorithm to decide who can take up more space in the memory, and how much.
If your computer has limited memory, applications that are considered less critical will not have access to more memory, but if the swapping is enabled, it will be better than nothing.
What’s the role of a swap partition?
As a general rule, the swap partition is a backup memory for the system. Applications may use it to store temporary files when needed. The swap is on the hard drive, so it’ll be slower than the physical memory, but it’s better than nothing.
In some cases, the swap partition may also help put your computer in sleep or hibernation mode, and recover everything as it was before when you restart it. That’s because the memory is cleared when the computer is powered off.
How does Linux use the swap partition
Now, that you understand the basics and the goal of a swap partition on Linux systems, let’s learn how it works in practice. Do you really need a swap partition? If so, what size? I’ll answer these questions in this part.
When is swap used on Linux systems?
The way Linux systems rely on the swap partition is not as trivial as you might think, it’s not something like “IF memory usage > 90% THEN swap”.
I won’t give you the complete algorithm here. But here is a general idea:
- The kernel monitors how much pressure there is on the physical memory, meaning how many requests have been received for more space recently.
- If there is a lot of demand, the kernel will decide to swap.
- The swapping process means moving some memory pages to the hard drive, to free up some RAM.
Obviously, the amount of RAM on the computer will also play a role. If you have 128 GB ram and are using only 4 GB, even if the pressure is high, there is no need to swap, for now, it will only slow the system.
In the last part of this article, I will explain how to adjust how much your kernel relies on the swap space, tweaking this may slightly improve the overall performance of your system.
How much swap do you need?
As a general rule, the swap partition size depends on the amount of physical memory. Red Hat and CentOS recommend setting it to twice the RAM size if less than 2 GB of RAM, and adding 2 GB if it’s more than 2 GB.
You can find their recommendation here. But I think this recommendation is a bit dated. As we now have systems with a large amount of memory, it doesn’t seem a good idea to waste so much space for the swap partition if the system may never use it.
Ubuntu has different recommendations (link here), that seem more up-to-date with the current hardware:
- With less than 1 GB of RAM, you should have at least the same amount of swap.
- For more than 1 GB (almost any computer), you should have a minimum of the square of the amount of RAM and a maximum of twice the amount of RAM.
Here are a few examples:
|RAM size||Minimum Swap||Maximum Swap|
|512 MB||512 MB||1024 MB|
|1 GB||1 GB||2 GB|
|2 GB||1 GB||4 GB|
|4 GB||2 GB||8 GB|
|8 GB||3 GB||16 GB|
|16 GB||4 GB||32 GB|
|32 GB||6 GB||64 GB|
Then use your common sense to make a decision. It will also depend on the disk you have. If you have 32 GB of RAM and an SSD with only 100 GB available, you won’t use 60% of it for the swap partition. In this scenario, something around 6 to 10 GB seems more appropriate.
Remember that if you want to use the hibernation mode on your computer, the minimum swap listed in this table probably won’t be sufficient. So try to give more space to your swap partition, especially if you have large SATA disks. On a 4 TB disk, you can easily save 64 GB for your swap partition, just in case.
Is swap still needed with a lot of memory?
Overall, even on systems with a lot of physical memory (RAM), setting up a swap partition is still recommended. Ubuntu is even creating one by default during the system installation.
The swap usage was more important years ago when physical memory was limited on computers. Nowadays, it’s not as useful as it was, but it’s still important.
Also, we now often have SSD (fast hard drives) on most recent computers, meaning the swap usage is not as slow as it used to be. For better performances, a good practice is to have a large amount of RAM, an SSD that will host the system and the swap partition, and then adjust the swap configuration to rely more or less on it.
I will explain how to configure this in the next part.
Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.
Linux commands to manage the swap usage
Before wrapping up this guide, I want to give you a few commands and tips you can use to monitor and configure the swap usage on your system.
Check the current swap usage
The command to check the current swap usage on a Linux system is:
You’ll get a table with two lines, one for the physical memory, and the other for the swap:
On each line, there is the total amount available, how much is used, and free. So, in my case, I have something like 6 GB of RAM and 1.5 GB of swap available, but it’s not currently used.
In general, I never use this command. I prefer to use htop, which is like the task monitor in a terminal.
And obviously, if you have a desktop interface, don’t bother with the command lines, just open the task monitor and check the memory graphs. Whatever the distribution you use, they should include the swap usage if it’s enabled on your system.
Adjust the swap usage on Linux
The swappiness is a configuration on Linux systems, ranging from 0 to 100, telling the kernel how much it should rely on the swap partition. The default value is 60. A lower value will reduce the swap usage, while a higher value will cause the kernel to use it more often.
To find the current swappiness configuration on your system, you can use the command:
If you haven’t touched it, it should be 60.
You can try another value temporarily with this command:
sudo sysctl vm.swappiness=30But to get something permanent, you need to edit this configuration file:
sudo nano /etc/sysctl.conf
Add this line at the end (or edit the value if you already have it) and reboot your system:
If you notice that your system is regularly slow and that each time you find that the swap partition is used, decreasing this value might help with the overall performance of your computer.
Enable or disable the swap on Linux
Tweaking the swappiness is the first thing you can do to tell your system to use the swap more or less. But you can also disable it completely.
Setting a swappiness to 0 will disable it, but there are other commands you can use:
- Disable swap usage:
sudo swapoff -a
- Enable swap usage:
sudo swapon -a
As I told you in the previous part, this is not necessarily a good idea, but it makes sense in some cases. I often use these commands for debug purposes, to better understand what is happening on the computer.
Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.