I’m no expert, and i have no real set convictions about swap, but when i read [1], i decided to always have some swap. I believe the author knows what they talk about, and they seem to debunk some myths that are common place. So far, using swap has not had any negative effects on my systems.
One point why I prefer zswap over zram is that zswap handles uncompressable pages by simply writing them to disk.
When an uncompressable page is pushed to a zram-backed swap device it will still use the same amount of memory as before, so no memory is actually freed but additional CPU cycles wasted.
As I understand zswap, it needs the backing of another swap device, be it a swapfile or swap partition.
Zram, generally, doesn’t need the backing of another another swap device but, almost similar thing as with zswap, if a writeback-device is configure, as was mentiond above, the incompressible pages are written to disk under memory pressure.
Back when 2 or 4 gig ram was the norm, I read an article that purported to show that the Linux kernel could struggle when managing memory with no swap enabled. It didn’t have to be a big swap, it just had to be there. Now, that was a long time ago so things might have changed since then but old habits die hard.
I have 32Gb RAM and an 8gGb swap partition to which I recently added 8Gb of zram. It often gets used.
You do need on disk swap at least the size of your RAM if you want to hibernate.
>>> free -h
total used free shared buff/cache available
Mem: 31Gi 11Gi 1.5Gi 472Mi 20Gi 19Gi
Swap: 15Gi 151Mi 15Gi
After a few days uptime esp. with gaming there are always a few hundred MB or GB in swap, so the system clearly utilizes it, even if available memory rarely goes below 50%.
I have a collection of 128 GB SSDs with no real purpose, they get new 512 GB friends soonish. SSD don’t wear out here - maybe I’m just lucky. So, I don’t see the point of having non-compressible swap in memory, it goes on the disk.
In the past i’ve read a bit about zram as Swap Device and zswap and there seem to be quite a few pros and cons for both ways.
I’ve mostly settled with zswap as it seems to be more “straight forward” to setup and worked fine for all the situations i encountered in the last years.
Most people probably use zram-generator, and that has a writeback-device option. So it’s a popular notion that some writeback happens. But no, that only specifies a device but triggers no writeback requests. The user has to set up some timers or heuristic to trigger writebacks themself.
That was new to me and interesting. I feel I have to look at it more in detail. It’s odd though that there is no mention of this on ArchWiki. They mention only configuration in zram-generator.conf or on the flight by (given example): # echo /dev/*sdX* > /sys/block/zram0/backing_dev
They surely do. Without the second one the first one is useless!
This is really interesting! Thank you so much for bringing it up and bringing some light into this.
I was almost on the verge of heading to kernel docs to see what the say about zram and writeback device. You pushed me right into it
I find the subject of swap fascinating as there are so much opinions, misconceptions, “myths” and misinformation about it. This thread is indeed an enlightening one.
Why should that not be a good combination. Fedora for example uses zram per default. But with zram you can not hibernate your latop. You need a swap file/partition for hibernation. Therefore you can find instruction for fedora how to use a swap file/partion in combination with zram so that hibernation is working.
Anyways, you should always have some sort of swap. You can find lots of got read on this topic elsewhere. I do not repeat the pros here.
I am using only zram and I am very happy with it. I installed the package zram-generator and my configuration is:
I have 64 GB of RAM and add 16 GB of zram swap. I have done extensive memory stress test with stress-ng where it consumes all the free RAM plus some swap and it never fails. And swapping with zram is very fast. Much faster than swap file/partition on SSD or nvme.
I am using zram-generator and the relevant part of the zram-generator.conf file is
[zram0]
# This section describes the settings for /dev/zram0.
#
# The maximum amount of memory (in MiB). If the machine has more RAM
# than this, zram device will not be created.
#
# "host-memory-limit = none" may be used to disable this limit. This
# is also the default.
#host-memory-limit = 9048
# The size of the zram device, as a function of MemTotal, both in MB.
# For example, if the machine has 1 GiB, and zram-size=ram/4,
# then the zram device will have 256 MiB.
# Fractions in the range 0.1–0.5 are recommended.
#
# The default is "min(ram / 2, 4096)".
#zram-size = min(ram / 10, 2048)
zram-size=4096
# The compression algorithm to use for the zram device,
# or leave unspecified to keep the kernel default.
compression-algorithm = lzo-rle
Everything below this is commented out.
The file is in /etc/systemd/zram-generator.conf.d/zram-generator.conf.
If i should open a new thread, please tell me so. I
should be doing it anyways since I want to set up a writeback-device that actually get used and I would need some guidance for it.