Setting vm.swappiness=1 by default

I have 32GB of RAM which barely get full even when running LLMs

Still I noticed that my system uses swap, for no reason? Afaik this means it is written to the SSD, or is ZRAM detected as swap somehow?

I ran

run0
vm.swappiness=1
swapoff -a && swapon -a

and it seems to be fixed. Not sure if swap is only used after a reboot, KDE systemmonitor and htop show no usage and no capacity.

It makes no sense to use swap when there is enough RAM, it is bad for disk lifetime. Someone enlighten me why this is not set by default?

By default it should only be zram, I believe, so no worry about disk writes.

You can see using swapon --show.

swapon --show for me shows:

Filename				Type		Size		Used		Priority
/dev/zram1                              partition	4194300		0		100
/swap/swapfile                          file		16777212	0		-2

The higher priority for zram shows that it will be used first.

1 Like

for me it shows this after running above commands

NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G   0B  100

same on a freshly installed system

You don’t need to worry about disk writes as you only have only one swap device, zram, entirely in your RAM.

funny, so has the swappiness value any effect?

It seems that zram confuses traditional system monitors. zram is fine

It should. The lower the value the less prone is the kernel to swap. You could even set the value to 0 but this doesn’t mean that the kernel will stop swapping. It will only avoid it as much as possible until it is under such a memory shortage so it will swap to prevent OOM condition.

You could set up htop to show zram usage if you want.

1 Like

Do you mount your partitions with noatime option.
If atime is on the meta data about file accesses it written all the time.
I have it turned off on all my mounts, ssd and hdd.

Oh damn. I wonder how problematic that is though. Swap the size of multiple GB is absolutely bad the the disk

Each time you run ls or cp etc the block with the inode for that file is updated with the time of access. Given the access time is not that useful unless your backup/archive tools need it you might as well disable.
I did not calculate the amount of data written. I noticed because I had a system doing little useful disk IO but lots of writes.

1 Like

That indicates you are only using 4G for zram and have an additional 16G swapfile used.

I would disable and remove that swapfile, as well as enabling the default size of zram (8G)
Zram is configured by the settings in /usr/lib/systemd/zram-generator.conf
The default of that file is

# This config file enables a /dev/zram0 device with the default settings:
# — size — same as available RAM or 8GB, whichever is less
# — compression — most likely lzo-rle
#
# To disable, uninstall zram-generator-defaults or create empty
# /etc/systemd/zram-generator.conf file.
[zram0]
zram-size = min(ram, 8192)

If you really really need the swapfile as well as zram then make no changes, but I have a system with 32G RAM and the default zram swap and have never needed to use additional swap.
The ‘free’ command shows the swap usage as well.

$ free
               total        used        free      shared  buff/cache   available
Mem:        32772968     4596488    18304488       81032    10423116    28176480
Swap:        8388604           0     8388604


 $ swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition   8G   0B  100
2 Likes

That’s right. I think I modified the amount of zram some time ago and left it at that.
The swapfile was also created because i wanted to enable hibernation. Something that I haven’t done yet so for the moment it just sits there.

I will though restore zram to 8G as per your advice. Thanks!

You stated in the OP that you have 32G RAM. That swapfile is useless for hibernation since it is only 16G.

I have not done hibernation for several years, but I suspect that the combination of zram and swapfile would present an issue with hibernation. Probably would need to have only the swapfile for that purpose.

I’m not the OP :slightly_smiling_face:
I just responded to them in post #3.

Wish I had 32G on this laptop but it has 16G and is non-upgradable. So a 16G swapfile should fit the bill were i to enable hibernation.

I remember having read somewhere that you may need to disable zram before hibernation by using a systemd service. I need to look into it.

Indeed check on the hibernate requirements.

I would hope the kernel is smart enough to know zram is not suitable for hibinate data.

1 Like

See the fedora magazine article Update on hibernation in Fedora Workstation - Fedora Magazine

1 Like

Thank you for the link! I will have a look.