The question appears if we want to use a swap partition on an SSD drive. I think, it would be better (for the SSD) if the deallocated swap space could be trimmed, because trimming the not used blocks enables the underlying SSD electronics to tune it wear leveling.

Google seems silent on the matter.

Alternatively, a regular blkdiscard command (and re-creating the swap space with mkswap) would be a feasible workaround, although it would be sub-optimal.

  • Why do you believe that "it would be obviously better (for the SSD) if the deallocated swap space would be trimmed"? Quotation needed. If you have a heart-felt conviction that issuing TRIM commands to the swap file or partition is a Good and Holy Thing then you can always add discard to the options of the swap partition or file in /etc/fstab. – AlexP Dec 26 '17 at 15:04
  • @AlexP It is not a religious convinction, it is a rational knowledge, thus I am convincable with facts. To my best knowledge, trimming is useful because it enables the underlying SSD control electronic to optimize wear leveling of the drive. I inserted this into the post. Note: the drive has no way to know, which blocks are allocated and which are free. It can see only block read and write operations coming from your motherboard, and no more. It doesn't know the fs. The discard option what you suggest in the fstab, has only any effect if the filesystem (this time, linux swap) supports it. – peterh - Reinstate Monica Dec 26 '17 at 15:10
  • 2
    Yes. man swapon, /discard – frostschutz Dec 26 '17 at 15:10
  • @frostschutz Oops! Sorry, it seems I checked it everywhere, except where it had been the most obvious :-) I made a self-answered post, for similar google-first mistakes, but I am considering to delete this question et al. Thank you very much! – peterh - Reinstate Monica Dec 26 '17 at 15:13
  • The thing with swap is that the swap frames are reused, so the underlying firmware will see a write operation overwriting a previously written frame; this is enough for it to decide that the previous data is no longer useful. So if the swap area is lightly used TRIM doesn't matter, and if it is heavily used TRIM doesn't do anything which frame reuse won't do automatically. This is very different from some file systems, where the kernel tries hard to scatter the files all over the available space. – AlexP Dec 26 '17 at 15:18

Yes, as it is visible the manual of the swapon command, it has a discard option. It would be meaningless if the linux swap wouldn't support it. Of course the underlying device should support trimming.

Your Answer

