System administrators and engineers love Linux for its stability and performance, but occasionally Linux too experiences performance hiccups.
The “read-error on swap-device” is a relatively common failure on Linux that can cause your system to crash or be non-responsive rendering it unusable. This guide will show you how to fix the read-error on swap-device failure on Ubuntu Linux.
Why Use a Swap File?
A swap file can be a physical storage medium such as a USB drive, a file on a hard drive, or a dedicated partition on a storage medium.
Swap files play an important role because they act as a supplementary medium to the physical RAM on your PC. When you are running memory-intensive processes and your RAM runs out of storage, Linux will use the swap file to run the other applications or store variable data.
Starting with Ubuntu Linux 18.04, the swap area is by default a swap file, prior to that the swap area used to be a dedicated swap partition.
Common Causes of the Read-Error on Swap-Device Failure
Some of the most common causes of failures on swap devices or files include the following:
Very low RAM on your PC: When you have very low memory left on your system, then most applications will forcibly store variable data in a swap file. Unfortunately reading data from a swap file is much slower than reading from RAM. Low swap device storage: Problems will occur if you have a very small swap file with a lot of data stored as variable data, which in turn will lead to low performance of the system.
Looking at the causes mentioned above, we can say that increasing the size of RAM or the swap file can help in fixing the read-error on swap-device issue in Ubuntu.
Viewing Swap File Size
To fix the read-error on the swap device failure, you have to make sure that you have enough storage space on your swap file. Ideally, the size of your swap file should slightly match the size of your RAM.
Run the following command to check the size of your swap file on Ubuntu Linux. In addition, it also lists the RAM space.
As you can see from the output above, this particular PC has a swap file storage of 2GB.
Alternatively, you can also the GUI interface to check the swap file and memory in use. Press the Super + A keyboard keys and search for System Monitor. The graph in the middle shows your memory and swap file usage.
Knowledge of swap file and RAM usage is important for making informed decisions while managing your RAM on Linux.
Creating a Swap File
Before you create or increase the size of your swap file, make sure that you disable the /swapfile using the command below.
Once the swap file has been disabled, you are ready to create a new swap file. For example, to create a swap file of 4G, run the following command.
For security purposes, you should assign your swap file with only read-write permissions on the root user, using the command below.
You can specify that the /swapfile is a swap area using the mkswap utility as below.
Finally, you can enable or start your swap file by running the following command.
Monitoring Your Memory Usage on Linux
Now that you have a swap file with sufficient memory in place, your Linux system will use it accordingly. You can monitor the swap file and RAM usage using the tools defined in this guide. Another option for checking swap file and RAM usage is to use the free -m command.
Low system memory is the primary reason why programs become unresponsive on a computer. Knowing how to kill such programs can be a lifesaver in such situations.