1. Introduction

An important aspect of memory management in any Linux system is swap space. In general, swap space keeps the system operating smoothly by acting as a safety net in the event that actual RAM runs out. To maintain the Linux system’s optimal performance, we might occasionally need to enable or disable the system’s swap space.

So, in this tutorial, we’ll look into the steps involved in safely disabling the swap space and reclaiming the space back in the Linux systems.

Without any further ado, let’s get into the nitty-gritty of it.

2. Check the Current Swap Usage

To begin, it’s essential to check the system’s current swap space configuration. We can achieve this in many 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
/swapfile_extend_1GB file      1024M   0B   -3
/dev/sdb             partition 1024M   0B   -4

This command displays the active swap devices and their usage. So, we see that swapon displays the existing swap space of 4.9 GB, including its usage.

On the other hand, we can also use the free command, which provides a concise and human-readable summary of the system’s memory usage. It displays information about both physical and swap memory usage:

$ free -gh
               total        used        free      shared  buff/cache   available
Mem:           4.7Gi       620Mi       3.4Gi        13Mi       696Mi       3.9Gi
Swap:          4.9Gi          0B       4.9Gi

Furthermore, the -g flag calculates the size in GB, while the -h flag displays the memory sizes in human-readable format. This command is valuable for quickly assessing the available, used, and total memory on a system, helping users monitor and manage their system’s resources efficiently.

3. Disabling the Swap Space

In this section, we’ll go over the four simple actions in this part to turn off the swap and give the operating system back control of the storage.

The swapoff command disables all the swap devices immediately. The time taken by the command is directly proportional to the amount of data in swap memory. Subsequently, it has to be swapped from the swap memory to the physical memory during the turn-off. Also, this disabling the temporary swap device persists across the system reboots:

$ sudo swapoff --all

The next important step is to comment on the swap entries in the file. In other words, we need to ensure the swap file is removed even after the reboot. At this instant, we comment out all the swap entries in 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 comment the following lines at the end of the file:

/swapfile                                 none            swap    sw              0       0
/swapfile_extend_1GB                      none            swap    sw              0       0
/dev/sdb                                  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 commented swap info:

$ 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

Next, let’s rebuild the GRUB configuration file to implement the changes. Here, we’ll use the update-grub command to make a fresh GRUB configuration file. The boot loader cannot work as expected without this file. Also, it makes sure the system is aware of the modifications and can display them in the boot menu:

$ sudo update-grub
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
...
... output truncated ...
... 
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done

Now, the update-initramfs command will update the initramfs, a temporary file system loaded into RAM during the Linux kernel’s boot process. When executed with the -u flag, it rebuilds the initramfs to include changes in the system’s swap configuration. This command is crucial after making swap modifications that impact the boot process, ensuring that the initramfs is synchronized with the current state of the system, allowing for a smooth and successful boot:

$ sudo update-initramfs -u
update-initramfs: Generating /boot/initrd.img-6.2.0-33-generic

4. Reclaim the Space

Now, it’s the time to reclaim the space previously allocated to swap. We can either delete the swap file swap partition or resize it, depending on our system configuration. Use the command line tools like fdisk or parted for this purpose.

Lastly, reboot the system to apply the changes and ensure that swap is permanently disabled:

$ sudo reboot

After the system restarts, use the free -gh command to confirm that swap is no longer active:

$ free -gh
               total        used        free      shared  buff/cache   available
Mem:           4.7Gi       567Mi       3.2Gi        13Mi       994Mi       3.9Gi
Swap:             0B          0B          0B

Alternatively, we also use the swapon –show which didn’t return any swap values:

$ swapon --show

On another note, we can also see the /proc/swaps file, which is a virtual file in Linux that provides information about active swap devices and their usage and offers insights into the system’s virtual memory configuration:

$ cat /proc/swaps
Filename                                Type            Size            Used            Priority

Since we deleted the swap configuration from the system, the file in our instance is empty.

5. Conclusion

Disabling swap permanently can be a valid choice for users seeking to optimize system performance or preserve disk longevity. However, it’s crucial to assess our system’s specific needs and ensure that there are no critical processes relying on swap before making this change.

By following the above steps and exercising caution, users can safely turn off swap, reclaim the allocated space, and tailor their system to meet their performance requirements.