You are not logged in.

#1 2023-03-24 19:35:51

jsaxx
Member
Registered: 2022-11-07
Posts: 16

Trim on swap

I was wondering if any kind of attention is needed in trimming the swap partition of my drive.

I am currently running fstrim.timer to execute trim commandweekly. On my main drive it gets executed on root and home partition but non on swap partition (this should be happening because fstrim gets executed on mounted partitions only).

I can see that swapon provides a discard option, and there is the possibility to set discard in /etc/fstab. On the other hand I read that using discard is not suggested in most cases.

Should I use discard on the swap partition, is there some other method to trim that, or what exactly is the best practice in this case?

Offline

#2 2023-03-24 21:14:29

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,794
Website

Re: Trim on swap

Perhaps use a swap file instead and that will get trimmed with the root trim?


https://ugjka.net
paru > yay | webcord > discord
pacman -S spotify-launcher
mount /dev/disk/by-...

Offline

#3 2023-03-25 21:40:03

jsaxx
Member
Registered: 2022-11-07
Posts: 16

Re: Trim on swap

I guess that would solve the trim situation, but I wanted to avoid swap file.

I was just wondering what the best practice for a swap partition would be.

Thanks though!

Offline

#4 2023-03-25 23:13:19

frostschutz
Member
Registered: 2013-11-15
Posts: 1,409

Re: Trim on swap

swap files don't help here. they have to be allocated so they won't be discarded by fstrim either.

swap supports several discard modes (none, once, pages, both). you have to choose one of them. there is no other method other than obscure ones.

obscure way to discard swapfile: swapoff, delete file, fstrim, create new file, swapon.

obscure way to discard swap partition: swapoff, blkdiscard swap partition, mkswap, swapon.

that's if you absolutely had to do it manually (like fstrim) without using the builtin discard support. however it would be prone to errors so just go with the discard options.

if you intend to trim the swap partition out of privacy concerns, it would be better to use encryption instead.

Offline

#5 2023-03-26 04:46:18

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 237

Re: Trim on swap

jsaxx wrote:

On the other hand I read that using discard is not suggested in most cases.

Where did you read this? I have

LABEL=SWAP	swap	swap	noauto,discard	0 0

in fstab and curious in what cases it may be harmful.

Offline

#6 2023-03-26 06:36:29

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: Trim on swap

See the notes in
https://wiki.archlinux.org/title/Solid_state_drive#TRIM
https://wiki.archlinux.org/title/Solid_ … nuous_TRIM

Ftr

https://man.archlinux.org/man/core/util-linux/swapon.8.en wrote:

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

Offline

#7 2023-03-27 20:27:56

jsaxx
Member
Registered: 2022-11-07
Posts: 16

Re: Trim on swap

dimich wrote:
jsaxx wrote:

On the other hand I read that using discard is not suggested in most cases.

Where did you read this? I have

LABEL=SWAP	swap	swap	noauto,discard	0 0

in fstab and curious in what cases it may be harmful.

The idea I have against discard is mostly taken from Arch wiki as mentioned by Seth: https://wiki.archlinux.org/title/Solid_state_drive#TRIM

Note: If you want to use TRIM, use either periodic TRIM or continuous TRIM. Continuous TRIM is not the most preferred way to issue TRIM commands among the Linux community. For example, Ubuntu enables periodic TRIM by default [2], Debian does not recommend using continuous TRIM and Red Hat recommends using periodic TRIM over using continuous TRIM if feasible [3].

***

seth wrote:

See the notes in
https://wiki.archlinux.org/title/Solid_state_drive#TRIM
https://wiki.archlinux.org/title/Solid_ … nuous_TRIM

Ftr

https://man.archlinux.org/man/core/util-linux/swapon.8.en wrote:

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

I am concerned about trimming swap mostly because of disk performance and longevity. Should I be then using the discard option? Or is the benefit to performance and longevity negligible?

Offline

#8 2023-03-28 06:50:03

seth
Member
Registered: 2012-09-03
Posts: 49,992

Re: Trim on swap

Make sure your SSD principally supports trimming, just try.
If you start to get random memory errors, try defining a (different) method.
If you still get  random memory errors, disable trimming.
If the memory errors go away, you cannot use trimming (regardless of the pot. benefits)
Otherwise it's not likely to make your concerned issues worse. Whether it improves them depends on access patterns and the swap size and probably the model. You can (synthetically) test the performance immediately. Longevity benefits will require to use the disk for a long time w/ and w/o trimming and compare the sector wear stats of the periods.

Offline

#9 2023-03-28 14:44:22

jsaxx
Member
Registered: 2022-11-07
Posts: 16

Re: Trim on swap

Thanks seth. The SSD itself does support trimming, and is being trimmed on root and boot partitions weekly.

I will probably give a shot on creating swapon with discard and try out the different methods to see how it goes.

Last edited by jsaxx (2023-03-28 14:48:59)

Offline

Board footer

Powered by FluxBB