Should the ‘discard’ option be used for mounting a swap file

filesystemsmountswaptrim

The Arch Wiki says that defaults,discard can be used in the fstab file for a swap partition. However it is not clear about a swap file, nor is the man page.

If the swap file resides in a filesystem which itself is mounted with the discard option (and is obviously in an SSD), should it use discard itself? And what if the parent filesystem is not discard'ing?

Intuition says "no", but I could not find anything about it and thought it was worth discussing.

Best Answer

I would say no.

1: The swap space does not use the same concept of free space as filesystem

2: what matter is that you always keep at least 25% free space on your SSD (this value was given to me by Sandisk representative on the phone), to allow proper work of wear levelling.

=> as long as the disk has spare clusters to work with, and to perform WL, it does not really matter if 5% of your disk is never trimmed, or continusouly re-written: even when YOU rewrite the same logical or physical sectors, WL will use different clusters anyway, when you write large enough blocks.

The question remains unanswered if you are using a whole disk for swapping. A whole disk used for swap may suffer premature aging, if never trimmed.

The other question is: does the swap driver support discard ? ext3/ext4 do.

Maybe, if your swap occupies a significant % of the disk, if you can, you could discard/trim the swap space during shutdown: if you can, after killing all services, do swapoff, and find a way to discard the swapspace (since I am not an expert, and to not leave the question unanswered, I would propose to mkfs.ext3, fstrim, mkswap again - there are probably other better solutions. Check if shutdown is due to UPS. ).