I have just installed zram, as I have developed a habit of using unreasonable amounts of RAM for certain simulations. I would appreciate some help to tune the parameters associated to swapping. My use-case is the following: most of the time, I do not want to swap (I need full performance for other tasks) as I use only a few Gbs of RAM. When I run the RAM hungry simulation, I would like zram to start compressing say at 80% of total RAM (64Gb) usage.
For the moment, I have only created the file /etc/systemd/zram-generator.conf with the option zram-fraction = 1.0. I assume that the compression algorithm is automatically set to zstd (how to check this?).
Then, for my use-case, it seems to me that I should decrease swappiness (right?), but I am confused on the way to do that.
I see:
write swap-priority = ... in /etc/systemd/zram-generator.conf
write vm.swappiness = ... in /etc/sysctl.conf
write vm.swappiness = ... in /etc/sysctl.d/99-vm-zram-parameters.conf
At the contrary, the archwiki advises to increase a lot the swapiness with zram and it suggests other tunings.
Any tip on what it most relevant? Of course the next step is to experiment.
The zram-fraction option is obsolete now (see man zram-generator.conf). It might still work anyway, but I wouldn’t expect it to always work. The “new” way to set the option you have described would be zram-size=ram.
If you don’t specify a compression algorithm, the kernel’s default will be used. You can check what kind of compression this is set to like this:
sudo cat /sys/block/zram0/comp_algorithm
You can explicitly set it to zstd in zram-generator.conf.
Swappiness does not define a threshold where swapping begins, although this is a common misconception. What swappiness actually controls is how aggressively the kernel will swap memory pages. This article explains the concepts behind swappiness fairly well if you are interested in getting a better understanding of what it is:
I wouldn’t worry too much about when your system is going in and out of swap if you are using zram, unless your task is cpu-bound. Unlike swapping to disk, you are unlikely to even notice when you are swapping to zram.
That’s right, if you are using zram there are some performance benefits to increasing swappiness. The documentation gets pretty dense, but if you are interested that rabbit hole goes pretty deep.
Priority only comes into play if you have multiple swap devices (for example, a zram swap device plus a swap file); it defines a device to use first. You’re probably not using multiple swap devices, so you probably don’t need to bother with changing the priority.
Rather than directly editing the /etc/sysctl.conf file, use a drop-in /etc/sysctl.d/99-vm-zram-parameters.conf to set swappiness. Note that you can use any name for the file you want as long as it ends with the .conf extension, but having the file name refer to zram is probably a good idea for clarity (and using the 99 prefix is to be sure that it loads last, overriding anything that loaded before it). Set the swappiness value with the line vm.swappiness = xx (with xx being the value you desire).