DEV Community

Cover image for SWAP: Guide about swap and swappiness
Ruslan Kh.
Ruslan Kh.

Posted on

SWAP: Guide about swap and swappiness

If you're tired of seeing the same old instructions on setting up SWAP in Linux, especially the ones that insist on using dd, you're not alone. This isn't just another tutorial on enabling SWAP; it's a practical guide that goes beyond the basics, addressing better and more efficient ways to manage your system's memory. We'll explore why fallocate might be a smarter choice and dive into optimizing swappiness for better performance.

What is SWAP?

SWAP is a designated area on your disk that serves as overflow space for your system's RAM. When your system runs out of physical memory (RAM), it moves inactive data from RAM to the SWAP space, freeing up memory for other tasks. This process is known as "swapping."

Why is SWAP important?

  1. Increased Virtual Memory: SWAP allows the system to use more memory than is physically available in RAM.
  2. System Stability: Without SWAP, running out of RAM could lead to system crashes, as the system wouldn't be able to handle new memory allocation requests.
  3. Memory Management Flexibility: SWAP is useful in systems with limited RAM or under heavy load, where temporary expansion of available memory is needed.

Types of SWAP:

  1. SWAP Partition: A dedicated partition on the disk used exclusively for SWAP.
  2. SWAP File: A file within the filesystem that can be used as SWAP space.

Image description

Setting Up SWAP:

1. Creating a SWAP Partition:

  • Use fdisk or parted to create a new partition on your disk, and set the partition type to "Linux swap" (code 82).
  • Apply the changes to update the partition table.
  • Format the partition as SWAP using the command:
mkswap /dev/sdXn
Enter fullscreen mode Exit fullscreen mode
  • Activate the SWAP partition:
swapon /dev/sdXn
Enter fullscreen mode Exit fullscreen mode
  • To make the SWAP permanent after reboot, add an entry to /etc/fstab:
/dev/sdXn swap swap defaults 0 0
Enter fullscreen mode Exit fullscreen mode

2. Creating a SWAP File:

Instead of using dd, you can use fallocate, which is faster and simpler for creating SWAP files.

  • Create a SWAP file of the desired size (e.g., 2 GB):
fallocate -l 2G /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Set the correct permissions for the file:
chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Initialize the file as SWAP:
mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode
  • Enable the SWAP file:
swapon /swapfile
Enter fullscreen mode Exit fullscreen mode
  • To ensure the SWAP file is mounted automatically after a reboot, add an entry to /etc/fstab:
/swapfile swap swap defaults 0 0
Enter fullscreen mode Exit fullscreen mode

Why Use fallocate Instead of dd?

  1. Speedfallocate allocates space instantly without writing zeros across the file, unlike dd.
  2. Simplicity: The command is straightforward and does not require specifying block size or count.

When to Prefer dd?

  • Compatibilityfallocate may not be supported on all filesystems, particularly older or non-standard ones, where dd might be necessary.

  • Physical Block Reservation: If you need to create a file with physically reserved blocks, dd is useful because it fills the file with data, ensuring that disk space is reserved.

  • Performance Testingdd can create files with specific content patterns, useful for performance testing or when a file needs to be filled with a specific data template.

Checking SWAP Status:

To check the current SWAP status, use:

swapon --show
Enter fullscreen mode Exit fullscreen mode

or

free -h
Enter fullscreen mode Exit fullscreen mode

Adding Multiple SWAP Files:**

If your system needs more SWAP space, you don't need to expand an existing SWAP file. Instead, you can add additional SWAP files. Each new SWAP file can be created and activated separately, and the system will manage them automatically. This approach is flexible and allows you to scale your SWAP space as needed without modifying existing files.

Creating Additional SWAP Files:

  • Create a new SWAP file (e.g., 1 GB):
fallocate -l 10G /swapfile_2
Enter fullscreen mode Exit fullscreen mode
  • Set the appropriate permissions:
chmod 600 /swapfile_2
Enter fullscreen mode Exit fullscreen mode
  • Initialize it as SWAP:
mkswap /swapfile_2
Enter fullscreen mode Exit fullscreen mode
  • Enable the new SWAP file:
swapon /swapfile_2
Enter fullscreen mode Exit fullscreen mode

Managing SWAP Priorities:

Each SWAP file can have a different priority, which determines the order in which the system uses them. The priority is set when you activate the SWAP file using the swapon command:

swapon /swapfile_2 -p 10
Enter fullscreen mode Exit fullscreen mode

In this example, /swapfile_2 has a higher priority than other SWAP files with the default priority (0). The system will use the higher-priority SWAP space first.

Example with multiple swap files:

~$ swapon -s 
Filename       Type    Size        Used         Priority
/swapfile_1    file    67108860    51176372     -2
/swapfile_2    file    67108860    12404496     -3
/swapfile_3    file    134217724   186348       -4
/swapfile_4    file    134217724   0            -5
Enter fullscreen mode Exit fullscreen mode

Tuning SWAP Parameters: Understanding swappiness

Swappiness is a kernel parameter that controls how aggressively your Linux system will move data from RAM to SWAP. It is expressed as a value between 0 and 100.

  • Low Swappiness (e.g., 10): Tells the system to avoid swapping unless absolutely necessary, keeping most of the data in RAM. This is useful when you have a large amount of RAM and you want to minimize disk I/O, because accessing data in RAM is much faster than accessing it on disk.

  • High Swappiness (e.g., 60 or higher): Makes the system more eager to use SWAP space, even when RAM is still available. This can be useful in environments where you want to keep more free RAM for caching or if the SWAP space is on a fast SSD, making the performance hit less noticeable.

The default value of swappiness is usually set to 60, which provides a balanced approach. However, depending on your specific use case and system resources, you might want to adjust it.

  • To temporarily change the swappiness value:
sysctl vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode
  • To make the change permanent across reboots, add the following to /etc/sysctl.conf:
vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

Why Adjust Swappiness?

Adjusting swappiness allows you to fine-tune how your system manages its memory, optimizing for performance, reducing disk wear on systems with SSDs, or ensuring better utilization of available RAM. By lowering the swappiness value, you can prioritize keeping active data in RAM, which is useful for workloads that are sensitive to latency. Conversely, increasing swappiness might be beneficial in systems with constrained memory, where offloading to SWAP earlier can help maintain system responsiveness.

Top comments (0)