1. Introduction
Swap space is a crucial aspect of any Linux system’s memory management. In a broad sense, swap space provides a safety net when physical RAM is exhausted, allowing the system to continue running smoothly. However, there may be times when we need to increase the swap space to ensure the Linux system’s optimal performance.
So, in this tutorial, we’ll review the basics of swap space and cover various methods to apply it in Linux.
2. What Is Swap Space?
Swap space is a space on the hard drive that the Linux operating system uses solely as virtual memory. So basically, when the system’s physical RAM is utilized, the kernel moves the inactive or less frequently used data from RAM to the swap space. This frees up RAM for more critical processes, ensuring the system remains responsive.
Nevertheless, swap space is undeniably essential for preventing system crashes due to running out of memory, but relying on it too heavily can lead to performance degradation. Ideally, our system should have enough physical RAM to handle the workload efficiently. However, there are situations when the need to increase swap space becomes inevitable.
3. How to Check the Existing Swap Space in Linux
To begin, let’s check our system’s current swap space configuration before proceeding with ways to augment it. We can achieve this in two ways – the swapon command and the free command.
In the first method using swapon, we open a terminal and type:
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2.9G 0B -2
So, we see that swapon displays the existing swap space of 2.9 GB, including its usage.
Alternatively, we can also use the free command to check both physical and swap memory:
$ free -h
total used free shared buff/cache available
Mem: 4.7Gi 796Mi 3.0Gi 25Mi 884Mi 3.7Gi
Swap: 2.9Gi 0B 2.9Gi
Here, the -h flag displays the memory sizes in human-readable format.
4. How to Create a Swap File
Now that we’ve scrutinized the existing swap space, let’s move on to the next step: increasing it. One common way to increase swap space in Linux is by creating a swap file. Chiefly, a swap file is a regular file in the file system that serves as additional virtual memory.
Let’s take a step-by-step look at the method to create and enable it.
4.1. Check Available Disk Space
First, let’s examine the disk space to ensure there’s enough room for the new swap file. We use the df command for this purpose:
$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 479M 1.7M 478M 1% /run
/dev/sda3 98G 13G 80G 15% /
tmpfs 2.4G 0 2.4G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/sda2 512M 6.1M 506M 2% /boot/efi
tmpfs 479M 96K 479M 1% /run/user/128
tmpfs 479M 72K 479M 1% /run/user/1000
Now, let’s create the swap file in the /dev/sda3 filesystem with the “/” mount point.
4.2. Create a File to Use for Swap Space
Next, we create the swap file using the fallocate command. Let’s check out the syntax:
$ sudo fallocate -l {SIZE_IN_GB}G /{MOUNT_PATH}
The SIZE_IN_GB gets replaced with our desired file size in gigabytes. For instance, let’s create a 1 GB swap file:
$ sudo fallocate -l 1G /swapfile_extend_1GB
Then, we secure our swap file by restricting permissions on it so that only root can access it:
$ ls -l /swapfile_extend_1GB
-rw-r--r-- 1 root root 1073741824 Aug 28 17:21 /swapfile_extend_1GB
$ sudo chmod 600 /swapfile_extend_1GB
$ ls -l /swapfile_extend_1GB
-rw------- 1 root root 1073741824 Aug 28 17:21 /swapfile_extend_1GB
4.3. Make It a Swap File Using mkswap
Now that we’ve created the file we’re going to use for swap and set appropriate permissions on it, we format the newly created file as swap:
$ sudo mkswap /swapfile_extend_1GB
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=9d28d839-2f4f-4173-9bba-576539fce7b6
After formatting our file as a swap file, the next step is to enable it. So, let’s activate our new swap file:
$ sudo swapon /swapfile_extend_1GB
4.4. Preserve the Swap File
The next important step is to preserve the newly created swap file. In other words, we need to ensure the swap file is available after a reboot. At this instant, we add an entry to the /etc/fstab file. Then, we open the file in a text editor:
$ sudo vi /etc/fstab
Once we’re in the text editor, we add the following line at the end of the file:
/swapfile_extend_1GB none swap sw 0 0
Then, we save the file and exit the text editor. Let’s use the grep command to confirm the configuration for the newly added swap file:
$ sudo grep swap /etc/fstab
/swapfile none swap sw 0 0
/swapfile_extend_1GB none swap sw 0 0
Consequently, we examine whether the swap file is active using the swapon command:
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2.9G 0B -2
/swapfile_extend_1GB file 1024M 0B -3
Alternatively, we can also use the free command to check the swap file:
$ free -h
total used free shared buff/cache available
Mem: 4.7Gi 867Mi 3.0Gi 25Mi 893Mi 3.6Gi
Swap: 3.9Gi 0B 3.9Gi
Here, we see the new swap file listed. We’ve successfully created a swap file so that the system now has additional space, which can help improve the performance when RAM usage is high.
5. Create a Swap Partition
Alternatively, the other way to increase swap space is to create a dedicated partition in the hard drive, provided we have a separate partition available for this purpose.
Now, let’s learn how to achieve it in a step-by-step manner.
5.1. Check Available Partitions
First, let’s evaluate the available space. For this, we’ll employ the lsblk or fdisk commands to list the partitions. Then, we’ll select the /dev/sdb disk to assign the swap space:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
...
... output truncated ...
...
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 513M 0 part /boot/efi
└─sda3 8:3 0 99.5G 0 part /var/snap/firefox/common/host-hunspell
/
sdb 8:16 0 1G 0 disk
sr0 11:0 1 1024M 0 rom
In this instance, we’ll add the /dev/sdb disk as the swap space.
5.2. Create a Swap Partition
As mentioned earlier, unlike a swap file, the swap partition requires a dedicated space. Now that the swap space is allocated, let’s proceed to make the partition:
$ sudo mkswap /dev/sdb
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=4279f517-2531-419c-9f6b-85b2cb22ff34
Here, we used the mkswap command to format the chosen partition as swap. Further, we enabled the newly created swap partition.
Now, let’s activate the swap partition:
$ sudo swapon /dev/sdb
/dev/sdb
5.3. Preserve the Swap Partition
Likewise, we must now ensure the swap partition is available after reboot. For this, we add an entry to the /etc/fstab file. Let’s open the /etc/fstab file in a text editor:
$ sudo vi /etc/fstab
Once open, we add the following line at the end of the file:
/dev/sdb none swap sw 0 0
Then, we save the file and exit the text editor. Let’s check our work:
$ sudo grep swap /etc/fstab
/swapfile none swap sw 0 0
/swapfile_extend_1GB none swap sw 0 0
/dev/sdb none swap sw 0 0
Subsequently, we investigate whether the swap partition is active. So, we run the swapon command again:
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2.9G 0B -2
/swapfile_extend_1GB file 1024M 0B -3
/dev/sdb partition 1024M 0B -4
Here, we see the new swap partition listed. Alternatively, we could use the free command to check it:
$ free -h
total used free shared buff/cache available
Mem: 4.7Gi 565Mi 3.1Gi 13Mi 1.0Gi 3.9Gi
Swap: 4.9Gi 0B 4.9Gi
As these commands show, we’ve successfully created a swap partition on our system to handle additional memory requirements.
6. Swap File vs. Swap Partition
Now that we’ve explored the two methods for increasing the swap space in Linux, let’s make a comparison so that we can choose the right approach for a system.
First, let’s see some key attributes of the swap file method:
- easy to create and resize
- doesn’t require a dedicated partition
- can be located anywhere in the file system
- well-suited for systems with varying memory needs
In contrast to a swap file, let’s review the key takeaways from the swap partition approach:
- faster due to lower overhead
- requires partitioning during system setup
- provides dedicated space for swapping
- apt for systems with stable memory requirements
Therefore, our choice between a swap file and a swap partition depends on the specific use case, system requirements, and available resources. Let’s make a note to always proceed with caution to avoid any unintended consequences.
7. Conclusion
To summarize, increasing swap space in Linux is a straightforward process that ensures the system runs smoothly, even when physical RAM is exhausted. So, in this article, we first acquainted ourselves with the basics of swap space.
Then, we explored two methods used to create additional swap space. Lastly, we compared the methods. Depending on our needs and system configuration, we can choose between creating a swap file or adding a swap partition.