Introduction

Swap Space plays a vital role in managing memory on Linux for maintaining performance. When your system runs out of RAM, it can use the swap space to keep runningg applications, but it’s slower a than RAM and depends on the storage device you use on your system.

How It Works?

When a system starts running low on RAM, the Linux operating system uses a swap space to prevent applications from crashing. Instead of immediately terminating processes, the kernel identifies memory pages that are inactive or least recently used and moves them from RAM to a reserved area on disk known as Swap.

This process is called paging. By freeing up RAM in this way, the system can continue running active processes smoothly. If a process later needs data access that was moved from RAM to Swap, it can swap out other inactive pages to make space.

Swap Space can be,

  • Dedicated Partition
  • Swap File
  • Combination of Swap Partitions & Swap Files

btrfs doesn’t support swap space.

However, swap space must not be considered a full replacement for RAM, since swap space are located in storage devices(HDD, SSD) which have slower memory R/W speed. A heavy reliance on swap can lead to noticeable performance degradation, often referring to as thrashing. (where system spends more time swapping than executing tasks.)

Modern Linux systems also uses additional optimizations such as :

  • Swappiness: A kernel parameter that controls how aggressively the system swap space.
  • ZRAM/ZSwap: Compressed in-memory swap techniqures that reduce disk I/O and improve performance.
  • Page Cache Management: The kernel may drop cached data before swapping, depending on memory pressure.

In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. However, modern systems often include hundreds of gigabytes of RAM. As a consequence, recommended swap space is considered a function of system memory workload, not system memory. Checkout this Recommended System Swap Space.

RAM in SystemRecommended Swap SpaceRecommended Swap Space (allowing hibernation)
<= 2GB2x the amount of RAM3 times the amount of RAM
> 2GB - 8GBEqual to the amount of RAM2 times the amount of RAM
> 64GBAt least 4GBHibernation not recommended
> 8GB - 64GBAt least 4GB1.5 times the amount of RAM

Increase Swap Memory (Swap File)

Note: This method is only suitable for non-btrfs type filesystems.

  1. Check the available swap memory:
# Lists active swap devices/files
swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G   0B  100

Note: Many modern Fedora systems use zram (compressed RAM swap) by default instead of disk-based swap.

# Shows overall memory and swap usage
free -h
               total        used        free      shared  buff/cache   available
Mem:            15Gi       6.9Gi       1.9Gi       148Mi       6.8Gi       8.2Gi
Swap:          8.0Gi          0B       8.0Gi
  1. Creating a swap file:
sudo fallocate -l 1G /swapfile

fallocate is fast because it allocates space without writing zeros.

# use dd if fallocate is not supported
sudo dd if=/dev/zero of=/swapfile bs=1024 count=65536
  1. Set the right permission for the created swap file:
sudo chmod 600 /swapfile

This restricts access so only root can read/write the swap (required for security + recommended)

  1. Making swap file:
sudo mkswap /swapfile # prepares the swapfile to be used as swap space
  1. Make Swap Persistent: Without this, the swap will disappear after reboot.
echo '/swapfile          swap            swap    defaults        0 0' | sudo tee -a /etc/fstab
sudo systemctl daemon-reload
# ensures the swap file automatically enabled at boot.
  1. Activating swap file:
sudo swapon /swapfile # activates the swapfile immediately (non-persistent)
  1. Verify the change:
swapon --show
free -h

You should now see /swapfile listed alongside any existing swap.

Checkout this post to know how to create swap partition which is recommended on btrfs file type filesystems.