Setting Swap Priority in Linux

Using Multiple Swap Spaces with Priority

Linux can manage multiple swap partitions or files simultaneously, each with an assigned priority. The kernel allocates swap pages from highest to lowest priority, with round-robin allocation among equally-prioritized areas.

Setting Swap Priority with swapon

Use the swapon command to activate a swap partition and set its priority:

swapon -p 10 /dev/sdc1

The -p flag sets the priority (0-32767, higher number = higher priority). You can also deactivate and reactivate with a new priority:

swapoff /dev/sdc1
swapon -p 10 /dev/sdc1

Persistent Configuration via /etc/fstab

To make swap priority persist across reboots, add entries to /etc/fstab:

/dev/sdc1 swap swap pri=10 0 0
/dev/sdc2 swap swap pri=5  0 0

Reload swap configuration:

swapoff -a
swapon -a

Priority Behavior

Priority assignment rules:

  • Higher numeric values have higher priority
  • Default priority is -2 if not specified
  • Newly enabled swaps default lower than existing ones
  • Swap areas with different priorities: higher priority exhausts first before kernel touches lower priority
  • Swap areas with equal priority: kernel allocates pages round-robin between them

Practical example: If you have an SSD and a mechanical drive, set the SSD to priority 10 and the mechanical drive to priority 5. The kernel will fill the SSD first, only using the mechanical drive when the SSD is full.

Viewing Current Swap Configuration

Check active swap areas and their priorities:

swapon --show

Output example:

NAME      TYPE      SIZE USED PRIO
/dev/sdc1 partition 4.0G   0B   10
/dev/sdc2 partition 2.0G   0B    5

Or use the older but still functional method:

cat /proc/swaps

Dynamic Priority Adjustment Without Downtime

You cannot change swap priority on a live partition directly. To adjust priorities safely:

  1. Create the new swap configuration in /etc/fstab
  2. Activate with swapon -a
  3. Let the system gradually move to the new configuration
  4. Deactivate old swap with swapoff /dev/old_partition

Alternatively, for a controlled migration:

swapon -p 15 /dev/new_swap  # New higher priority
# Traffic naturally shifts to higher priority
swapoff /dev/old_swap       # Disable old swap after migration

Multiple Swaps on Same Device

You can create multiple swap partitions on the same physical device:

# Create two 2GB swap partitions on /dev/sdc
sudo fdisk /dev/sdc
# Create /dev/sdc1 and /dev/sdc2, type 82 (Linux swap)

sudo mkswap /dev/sdc1
sudo mkswap /dev/sdc2

swapon -p 10 /dev/sdc1
swapon -p 8  /dev/sdc2

This can be useful for load distribution across the same device, though priority ordering is typically more important than parallelism within a single drive.

Swap Files vs Partitions

Modern systems often use swap files instead of partitions for flexibility:

# Create 4GB swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon -p 10 /swapfile

Add to /etc/fstab:

/swapfile none swap sw,pri=10 0 0

Swap files have negligible performance difference from partitions on modern filesystems and offer easier resizing.

Similar Posts

3 Comments

  1. You don’t mention how many swap partitions you have, but I’ll assume with a 32GB swap that you have only one, so having a swap priority of -2 is fine. If you have more than one swap defined (Why oh why would you have more than 32GB), then that would mean the partitions would be used in an order defined by the priority. When I have more then one swap partition, I *usually* set then to have the same priority to they write in a round-robin fashion. If the drive are the same speed, then it speeds up reading and writing to the swap.

Leave a Reply

Your email address will not be published. Required fields are marked *