A swapfile is a special file created on your computer’s disk, which will hold some of the memory contents. For example, when the main memory is starting to get full, the computer may write some of its memory to the swap space on the disk, which, albeit much slower than memory, will work as extra memory in the machine.
In this tutorial, you will learn:
- How to identify if your system is swapping or not
- The difference between a dedicated swap space partition and a swapfile
- How to create a swapfile in Ubuntu and Linux Mint
- How to configure the new swapfile into /etc/fstab
- Example setting up a swapfile
Software requirements and conventions used
|Category||Requirements, Conventions or Software Version Used|
|Software||Bash command line, Linux based system|
|Other||Any utility which is not included in the Bash shell by default can be installed using |
|Conventions||# - requires linux-commands to be executed with root privileges either directly as a root user or by use of |
$ - requires linux-commands to be executed as a regular non-privileged user
Is your system swapping?
There are advantages and disadvantages to using a swapfile (and subsequently running out of memory).
One of the main advantages is that when your system is consuming more memory than is physically in the system, it will not terminate programs semi-randomly to let other programs continue. This process of terminating programs automatically is called the OOM (Out of Memory handler), and it is present by default on most if not all Linux systems.
One of the disadvantages is that when your system starts swapping it becomes prohibitively slow. This is because on a very regular basis, if not constantly, the system will be trying to swap contents from main memory to disk and vice versa. It is very clear when a machine starts swapping. It will become so slow it will be hard to issue commands at the command line, or your desktop will be, again, prohibitively slow.
Note that your desktop being slow does not necessarily mean that your system is swapping. It may simply (and most often) be that your CPU or disk subsystem or actual disk is not keeping up with the tasks you are loading onto it. A faster disk (i.e., a fast SSD or NVMe) is likely to help the most in such cases. A faster CPU may work too but start with a faster disk or more RAM if you are currently working with 0.5-4GB. Try and have at least 8GB of memory in any reasonable workstation.
So how can you see if a system is swapping easily? It is very simple. First, install htop (
sudo apt install htop), and then start it (simply execute
htop at your command line). If you see the following for Memory and Swap:
I.e. a full Memory (Mem) bar, and a filling-up Swap (Swp) bar, your system is highly likely swapping. You will also tend to see that the CPU threads are maxing out or at least quite busy, though this may not always be true. On a system that is just slow and not swapping, memory may be quite high too (though not full, let’s say up to 80-90%), and most importantly, the Swap bar will be used 15-40% and not 70% (and not increasing over time).
It is usually very clear. Slow response, maxed CPU, maxed Memory, filling, and large Swap. The system is swapping.
A dedicated swap space partition versus a swapfile
When you installed your Ubuntu or Mint system, you may have configured (or the installer may have configured for you) a dedicated swap space partition. Such a partition is dedicated to swapping. Perhaps it may be slightly faster than using a swapfile, which is just a regular file inside your directory tree (and likely stored in the root folder) if it is on the same disk as the operating system.
If, however, the swap partition (or swapfile) was created on a different disk, perhaps even a dedicated one, then the swapping be considerably faster, especially if the drive itself is fast like a fast NVMe or SSD drive.
Personally, I used to be quite dedicated to setting up a sizable dedicated swap space partition whilst installing the operating system. Now I tend to use swapfiles instead. Partitions are harder to handle when you want to increase their size, etc.
Also, a dedicated disk for swap (whether as a partition, which may still be preferred, or as a swapfile) may make sense for a production server. If the server has 128GB of memory and is likely only to use 32GB of it, a dedicated disk for the swap would not make sense, etc. Vice versa, if the server had 16GB of memory and will be running 20 heavily used MySQL servers, we had better expanded the memory or allotted sufficient swap space to provide for a much more likely-to-happen memory management mishap. Still, if the money is available, a memory increase would definitely be recommended in such cases.
If you are interested in learning more about using the command line in general, you can review our Linux Subshells for Beginners with Examples and Useful Bash Command Line Tips and Tricks Examples Part 1 articles.
Setting up a swapfile at the command line
The nice thing about creating swapfiles is that it can be done at any point. Even when the workstation or server is heavily used, a swapfile can be created, brought online, and the workstation (or server) will immediately benefit from it. The next time you run into an OOM (Out of Memory), or close to it, and you have plenty of space available on some fast drive, you can quickly configure a swapfile to take on the memory load.
For this example, we will be creating a 10 Gigabyte swapfile. The way to calculate the number of blocks we need to create, at 1024 bytes per block is:
10 Gb * 1000 = 10000 Megabyte * 1024 = 10240000 blocks of 1024 bytes each.
We will place this swapfile into the
/ (root) directory, and will call it
swapfile001. The following commands will need to be correctly and carefully entered into your console prompt, also ensuring that no commands fail along the way.
If the first command should return a pre-existing
swapfile001 file, you should change the name of the new swapfile to something unique and not yet existing. Use
ls again to verify the newly chosen name does not exist before continuing. If a ‘
ls: cannot access ‘/swapfile001’: No such file or directory’ message is returned for the first command, it means
swapfile001 does not exist yet, and you may continue with the next steps.
ls /swapfile001 sudo dd if=/dev/zero of=/swapfile001 bs=1024 count=10240000 sudo chmod 0600 /swapfile001 sudo mkswap /swapfile001 sudo swapon /swapfile001
When you now open up
htop again, you will notice that the Swap bar is less big and that the overall Swap space has increased.
Configuring the new swapfile into /etc/fstab
Now that we have created our new swapfile let us add it to
/etc/fstab to ensure that when we reboot, the swapfile will be reused and we will maintain the benefit provided by it.
If, on the other hand, you added a swapfile as temporary coverage for a near-OOM memory event which is unlikely to happen again, you can skip this step and instead issue
sudo swapoff /swapfile001 when you want to turn off/deactivate the new swapfile. If you do turn it off and also want to remove the swapfile, you can issue
sudo rm /swapfile001, but please make sure using
htop first that the file is indeed no longer in use. Do not add the file to
/etc/fstab in this case (i.e., if you decided to remove it), as this may cause a reboot failure (issues during startup).
To add the new swapfile to
/etc/fstab instead, you can issue
sudo vi /etc/fstab or
sudo nano /etc/fstab (or any other text editor you prefer) and add the following line to the end of the file:
/swapfile001 swap swap defaults 0 0
And save the file. Alternatively, on Mint, you may like to try the following line instead:
/swapfile001 none swap sw 0 0
Save the file and reboot. After the reboot, the new swapfile should be in use and you can again use
htop to confirm the total size of the swap. Alternatively, you can use
free -h at your command line, which gives information about the size of the swap in the last line of the output.
In this article, we first learned how to identify if your system is swapping or not. We also looked at the difference between a dedicated swap space partition and a swapfile. Then we explored how to create a swapfile on Ubuntu and Linux Mint. We setup an example swapfile and activated it, and we had a look at how to configure the new swapfile into
/etc/fstab! Enjoy swapfiles!