Set priority for Swap devices in Ubuntu

Published: October 16, 2022

Set priority for Swap devices in Ubuntu

When RAM starts to fill up, inactive pages there are moved to Swap space. Swap space could be there in form of a partition or file. But, what if there are multiple Swap partitions or files on different physical disks. If a particular disk is slower than the other then, we may want to prioritize the faster one. That doesn't mean we are ignoring the slower disk, its just that we want our system to function efficiently and prefer the faster device.

So, the Kernel would prefer the Swap device according to the priority we have assigned. The default priority values are set to low, which means newer Swap device would always have lower priority assigned than the existing ones. From here, we can deduce that higher values by a user show higher priority.

Use the following command to view priority -

cat /proc/swaps

Note: Following operations require superuser privileges. In case you don't have one then, we advise you to contact your System Administrator for assistance.

Set priority for Swap devices in Ubuntu

A user should set a Priority value greater than equal to zero but less than equal to 32767 for a Swap device. If we don't assign a priority value then, system would on its own assign a priority value -2 and lower.

To set a priority value, we need to edit the /etc/fstab file. Use a text-editor like nano -

sudo nano /etc/fstab

And, add pri= parameter in fourth field as shown here.

/swap1G.img     swap     swap     pri=5,defaults     0     0

Priority value should >=0 but <=32767. The higher the value the higher is the priority. Save and exit. Now, turn off and then turn on all Swap devices to see the changes.

sudo swapoff -a
sudo swapon -a

Lastly, we can view the changes through -

swapon --show

In conclusion, if there are multiple Swap devices available then it is better to set a priority to ensure that faster devices are preferred.