5

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.

1

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. ).

1

Some of the questions/suggestions left by the other answer can be answered by reading the documentation below.

I don't believe discard is recommended on filesystems. It is recommended to run fstrim periodically instead. You can find a cron job for this.

Kernel developers are not particularly happy about the performance of discard operations, even recently. It varies too much between devices. And you're supposed to do the periodic discards anyway, because devices are allowed to drop discard operations if they are too busy.

As the other answer says, your filesystem is so much bigger than your swap space, so unless you can prove otherwise it is best to not add the option discard. And discard=once is clever, but it is still likely to slow down boot (if the swap space had been used). So I do not see it as worth enabling.

man swapon

-d, --discard[=policy]

Enable swap discards, if the swap backing device supports the discard or trim operation. This may improve performance on some Solid State Devices, but often it does not. The option allows one to select between two available swap discard policies: --discard=once to perform a single-time discard operation for the whole swap area at swapon; or --discard=pages to asynchronously discard freed swap pages before they are available for reuse. If no policy is selected, the default behavior is to enable both discard types. The /etc/fstab mount options discard, discard=once, or discard=pages may also be used to enable discard flags.

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.