A Beginner’s Guide to Managing Swap Space on Linux

On
Linux swap space

Every modern operating system has a robust memory management system to efficiently run the processes on a system with low RAM. Linux too has one which involves the use of swap space. In this tutorial, we will not only understand the use and features of swap space but also how we can manage it—like a pro—on our Linux system. Swap space helps you avoid system crashes and improve performance in a low-memory environment. We'll also see practical examples of creating, resizing, and removing swap space on a Linux system. Let's get started and master swap space management on Linux.

Linux swap space
📷 Credit: DALL·E 3

The same technology is applied on other operating systems as well differing in names and the way it is implemented. For example, in Windows the same feature is maintained on a page file.

Read Also:
How to Create Filesystems in Linux: A Complete Walkthrough

No matter which Linux distribution you are using, the swap space management commands mentioned below apply to all of them. If you are low on disk space, swap management may cause issues.

What Is Swap Space and Its Importance?

Swap space is virtual memory that resides on a storage disk. Whenever Linux system detects the RAM is about to get full, it transfers inactive processes data from RAM to the swap space to free memory for new active processes. This keeps the system active and running despite being low on RAM space.

On a Linux system, two types of swap are available.

  • Swap partition: As the name implies, this is the swap space built on a separate disk partition dedicated to this purpose. No other file activity is done in this space except the swap utilization.
  • Swap file: Similarly, a swap file is created on a general filesystem that is also used for regular files and directories. It can be a bit slower compared to the dedicated swap partition.

One can use either type of swap space on a Linux system. You can test both the types and can compare their performance before sticking with one of the variants.

Swap space and RAM in Linux
📷 Swap space and RAM work in conjunction to keep the system running

Why Swap Space Is Important in Linux?

Here are some of the concrete reasons why having a swap space on Linux is critical for its functioning and performance.

  • Swap space functions like an extension of your system's RAM
  • It helps prevent memory-overflow errors
  • Extremely useful on Linux systems with low RAM
  • If you have a large swap space, hibernation is easy on a Linux system
  • It can be used for buffering when memory usage increases—dynamically

Remember, swap memory is significantly slower than RAM and can never be considered as a full-fledged substitute of the same.

Check Current Swap Usage

Before you start working on creating and modifying your Linux system's swap space, you must check its current usage. There are a few handy ways to do it.

# Display existing swap space details
swapon --show

# The 'free' command displays detailed memory stats including swap space details
free -h

# Monitor swap space usage in real time
top

# Or
htop

Swap space monitoring gives you a clearer picture of its attributes and its current usage.

Creating a Swap File

Let's see how we can create a swap file in a few easy steps. In our example, we are going to create a 4GB swap file. Here's how to do it.

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Just creating this file is not enough. You have to make it permanent to make it available for use upon a reboot. And for that, you have to add the following entry into the /etc/fstab file.

/swapfile none swap sw 0 0

The size of your swap space largely depends on the load on your system memory and the space available on your disk drive.

Adjusting Swappiness

Now what the heck is this 'Swapiness'? Well, in this context, the swapiness value determines how actively the Linux kernel is going to use the swap space.

The swapiness value ranges from 0 to 100, where 0 denotes the least usage of swap and 100 implies aggressive use of swap—whenever possible.

To Check Current Swapiness Value:

cat /proc/sys/vm/swappiness

To Temporarily Change Swappiness:

sudo sysctl vm.swappiness=15

To Make This Change permanent, Edit the /etc/sysctl.conf File:

vm.swappiness = 15

Avoid keeping the swapiness value very high as it will slow down the system due to excssive disk I/O activity. Even if you are using a high-performance SSD, avoid high swapiness values.

Remove Swap File or Partition

If you want to remove the swap partition on your Linux system, it can be done through the following 3-step process.

  1. Disable Swap File:
    sudo swapoff /swapfile
    sudo rm /swapfile
  2. Now remove its entry from the /etc/fstab file.
  3. Remove The Swap Partition:
    sudo swapoff /dev/sdX
    Replace X with the alphabet associated with the swap partition.

After the swap space has been deleted, you can either delete this partition or reuse it as a regular partition for storing files and directories. Both fdisk and gparted utilities can do this job—easily.

Resizing Swap

In case you want to resize the swap space, use the following method to do it seamlessly.

  1. Turn off swap:
    sudo swapoff /swapfile
  2. Resize it:
    sudo dd if=/dev/zero of=/swapfile bs=1M count=6144  # Increased to 6GB
    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
  3. Reactivate:
    sudo swapon /swapfile

If you are doing it on a production server, disconnect it from the business process pipeline before starting the resizing process.

Best Practices for Swap Space Management

Now that we know how to manage swap space on a Linux system, let's see some of the best practices we must follow to correctly and efficiently take care of this important Linux feature.

  • Do not consider swap space as a backup option for your important data. If your Linux server experiences a heavy workload, use proper backup methods to safeguard important business data.
  • Swapiness value should be handled with care. If it's a regular home desktop, keep this value low. But, if it's a busy production server, keep it high.
  • If your high-load and busy server is powered by an SSD, do not keep the swapiness value very high as it will wear out the drive much faster than expected. A good strategy is to keep a reasonable swapiness value with a regular hard disk drive built specifically for servers.
  • If you frequently put your Linux system on hibernation, make sure the size of the swap space is equal to RAM or more.
  • You can also use the vmstat command to assess how frequently your system's Linux kernel uses the swap space. Watch the si (swap in) and so (swap out) values to determine this.

Conclusion

Swap management on Linux is a foundational Linux administration skill. While modern systems with abundant RAM may barely touch swap, it remains a critical fallback mechanism.

Whether you are setting up a VPS, optimizing a development machine, or tweaking server performance, understanding and managing swap space smartly can save you from unexpected memory-related crashes.