I have two large swapfiles, each about 500 GB in size, and a program that is using about 900 GB of the total 1 TB of swap space. The memory-hungry program will take a few days to finish. However, I just realized that the swapfile priorities are incorrect. swapon says:

NAME                TYPE        SIZE   USED PRIO
/dev/sda            partition 465,8G 465,8G   -1
/home/alex/swapfile file      502,9G 384,5G   -2

/home/alex/swapfile is on an M.2 disk capable of 3.5 GB/s, whereas /dev/sda is a SATA disk capable of only 540 MB/s. So, I want /home/alex/swapfile to have priority -1 and /dev/sda to have priority -2, but I really don't want to interrupt the long-running process. Is there any way to change the swapfile priorities without first disabling the swapfile with swapoff?


I don't think there is a way to do that with a live existing swapfile -- however, you can create another swapfile with an even higher priority of 0 or 1 that's on the fast disk (assuming you have space on /home/alex still).

Create it with dd if=/dev/zero of=swapfile2 bs=1M count=$SIZE_IN_MEGS. (Your 500 gig swapfile is huge btw.)

You could then swapoff the first /home/alex/swapfile, but there's no point as it's on the same disk as the new /home/alex/swapfile2 - it'll only be used to swap in (and not out) until swapfile2 (and /dev/sda) are full. You could swapoff /dev/sda as well but you'll suffer the speed penalty of it til it's all read back in - and if you are low on ram, swapped back out to swapfile2.

This might be useful to you if you continue to operate with low ram and can do this at some point you don't need performance for the time it takes to swap back in and out, then to return later with the swapped ram in swapfile2, which will operate much faster from then on.

  • Even better to use fallocate instead of dd – golimar Dec 11 '19 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.