How to set swap priority in Linux

How to set swap priority in Linux? Can I use 2 swap partition at the same time?

You can set the priority of swap in Linux by swapon.

For example, to set /dev/sdc1‘s priority to 0:

# swapoff /dev/sdc1; swapon -p 0 /dev/sdc1

You can also put one entry into the /etc/fstab to make it take effect each time Linux reboots:

/dev/sdc1 swap swap pri=0 0 0

You can set 2 swap partions to have the same priority, pages are allocated on a round-robin basis between them.

Priority

Each swap area has a priority, either high or low. The default
priority is low. Within the low-priority areas, newer areas are even
lower priority than older areas. All priorities set with swapflags are
high-priority, higher than default. They may have any non-negative
value chosen by the caller. Higher numbers mean higher priority.

Swap pages are allocated from areas in priority order, highest
priority first. For areas with different priorities, a higher-priority
area is exhausted before using a lower-priority area. If two or more
areas have the same priority, and it is the highest priority
available, pages are allocated on a round-robin basis between them.

As of Linux 1.3.6, the kernel usually follows these rules, but there
are exceptions.

From swapon manual.

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 *