How to change swap priority

Linux kernel 2.4 and later can handle multiple swap spaces up to 32 swap space, If you use multiple swap files and partitions and you want to prioritize which spaces are used first, you can do it by altering the priority no of swap files and partitions to boost the virtual memory. By default the priorities are assigned in the order that the swap spaces are added, where -1 is a higher priority number than -2.

Run the following command to see the swap files or partitions priority.
 
[ root @ leolinux ~ ]#  swapon   -s
 
Filename          Type          Size             Used           Priority
/dev/hda6          partition   4095996      57788            -1
/var/swapfile     file             1048576      0                    -2

 

First you need to deactivate the used swap partition or file in order to change the priority temporary.

[ root @ leolinux ~ ]#  swapoff    /dev/hda6

 

To change the priority -2 to 10 

[ root @ leolinux ~ ]#  swapon   -p   10    /dev/hda6

To check priority 
[ root @ leolinux ~ ]#  swapon   -s
 
Filename          Type          Size             Used           Priority
/dev/hda6          partition   4095996      57788            10   
/var/swapfile     file             1048576      0                    -2

 

To change the priority permanently run the follwoing steps. 

[ root @ leolinux ~ ]#  swapoff    /dev/hda6

 

Edit  /etc/fstab file.

[ root @ leolinux ~ ]#  vim   /etc/fstab 

/dev/hda6     swap      swap     defaults,pri=10     0   0

ESC

:wq!

 
To activate swap partitions or swap files .
[ root @ leolinux ~ ]#  swapon   -a
 
To check priority.
[ root @ leolinux ~ ]#  swapon   -s
 
Filename          Type          Size             Used           Priority
/dev/hda6          partition   4095996      57788            10   
/var/swapfile     file             1048576      0                    -2