You are not logged in.

#1 2023-05-30 00:48:00

daniel1988
Member
Registered: 2009-06-28
Posts: 34

SSD - swap file or swap partition

Although my RAM is large, I don't like rebooting and I do it only when I have to. So my swap starts to be used after a while. I hope it is mostly cold pages (like apps with memory leaks, or browser tabs that I keep open but don't access in a while). Is there actually a way to inspect what is in swap?

My main question is: is it better to have a swap partition or a swap file in the file system? SSDs are good nowadays, but there is still a limited number of writes per cell. If the swap is a partition, it means that the same SSD cells are going to be used over and over.  On the other side, if swap is a file, there is a small overhead of the file system. If only cold pages are offloaded there, the overhead is negligible. In order to have the swap to use different physical cells as much as possible, should the swap file be removed and recreated on every boot?

Offline

#2 2023-05-30 05:23:24

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

Re: SSD - swap file or swap partition

I prefer swap as a dedicated partition/LV/block device over swap file. But it's a matter of opinion...

To the SSD it does not make any difference either way. Swap file is a bit of a hack but if it works it's really just the same as a partition. In the end the SSD has to take care of wear leveling by itself. There is no point to emulate this in software...

The only advantage of swap file is that it's easy to (re-)create on demand. Partitions are a bit static. But you can do dynamic partitions with LVM. As long as your VG isn't 100% full, you can create/remove/resize LVs whenever, without having to move any other LVs around.

Another option might be to try zram/swap instead of a physical swap device. This can be useful in some situation but it depends a lot on use case and on how well swapped data can be compressed.

Offline

#3 2023-05-30 06:35:01

seth
Member
Registered: 2012-09-03
Posts: 51,012

Re: SSD - swap file or swap partition

Not "instead", https://wiki.archlinux.org/title/Zswap "works in conjunction with a swap device" and "all officially supported kernels have zswap enabled by default"
It's very much a cache for your actual swap.

https://wiki.archlinux.org/title/Zram should™ not be used along zswap and you're either limited to using it as the only swap device (drastically limiting the available swap) or are likely to run into a situation where it first gets filled up with rarely accessed data and then you're constantly running the hot swap on the disk (and more so because zram is effecively wasting RAM)
So you really. REALLY. should monitor its behavior against your default usecases.

Online

#4 2023-05-30 07:00:22

icar
Member
From: Catalunya
Registered: 2020-07-31
Posts: 449

Re: SSD - swap file or swap partition

One advantage I find is that with a swap file it's easier to set up full disk encryption. My swap file resides on /, and that is an ext4 partition in a luks volume. This means that when I hibernate my laptop, my dumped memory is encrypted alongside all my system files.

Last edited by icar (2023-05-30 07:01:06)

Offline

#5 2023-05-31 21:04:36

walkingstickfan
Member
From: USA
Registered: 2021-05-10
Posts: 104

Re: SSD - swap file or swap partition

The last time I reinstalled distros on both of my PCs, I opted to try using a 4gb swap partition for all of my Linux installs; Debian Stable, Debian Unstable, and Arch Linux. I found that the disk space taken up by the swap partition wasn't worth the amount of use by swap. I hardly ever noticed swap being used under either of my Debian installs, and when swap was used under Arch Linux, which wasn't very often, it was only around 300mb. I've since disabled swap and when I can find the time, I'll absorb the swap partition into my Arch Linux partitions since Arch uses more disk space than either of my Debian installs. I'll never use swap again.


Arch Linux with Openbox & Tint2

Offline

#6 2023-05-31 21:13:36

seth
Member
Registered: 2012-09-03
Posts: 51,012

Online

#7 2023-06-01 15:41:55

rep_movsd
Member
Registered: 2013-08-24
Posts: 133

Re: SSD - swap file or swap partition

icar wrote:

One advantage I find is that with a swap file it's easier to set up full disk encryption. My swap file resides on /, and that is an ext4 partition in a luks volume. This means that when I hibernate my laptop, my dumped memory is encrypted alongside all my system files.

This is incorrect

When you hibernate, its not using the ext4 filesystem code to write to the file
The kernel knows at what offset on the disk your swap file is and writes to that

By the time the hibernation code starts writing to disk, everything else in the kernel is stopped including FS drivers.
Only something that can do raw disk IO is alive

Offline

#8 2023-06-01 19:53:54

seth
Member
Registered: 2012-09-03
Posts: 51,012

Re: SSD - swap file or swap partition

That's not what icar said.
His point is that

with a swap file it's easier to set up full disk encryption.

And objectively the swapfile paragraph in https://wiki.archlinux.org/title/Dm-cry … encryption is much shorter than the swap partition one.

Also see the blue note at https://wiki.archlinux.org/title/Dm-cry … _swap_file

Online

#9 2023-06-01 22:12:07

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

Re: SSD - swap file or swap partition

daniel1988 wrote:

If the swap is a partition, it means that the same SSD cells are going to be used over and over.

SSDs have FTL. It knows nothing about partitions. Any block from any partition may be mapped to any physical block on chip.

Offline

Board footer

Powered by FluxBB