Zram vs swap partition vs swapfile

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.

[1]

2 Likes

nice read, thanks for sharing.

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.

1 Like

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.

At least this is how I understood it.

1 Like

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
1 Like

32 GB ram, zswap+swap

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.

1 Like

The user can ask zram to write them to disk, but there’s no automatic system that acts e.g. under memory pressure (as zswap does).

With Zram, won’t the writing of incompressible pages to disk happen automatically if you have configured a writeback-device?

https://wiki.archlinux.org/title/Zram#Enabling_a_backing_device_for_a_zram_block

Here, there is no mention of user intervention for zram writing those incopressible pages to disk if you have a writeback device.

Here is an interesting read about swap.
https://www.baeldung.com/linux/ssd-swap-partitions-cons

3 Likes

Yes, it does.

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.

1 Like

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

1 Like

It’s essentially the first line from the zram-manual, but the wiki misses the second one.

The zram-generator creators even say it was a mistake to expose that option, because they provide nothing triggering it see e.g.

2 Likes

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 :grinning_face_with_smiling_eyes:

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.

1 Like

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:

# cat /etc/systemd/zram-generator.conf                 
[zram0]
compression-algorithm=zstd
zram-size=16384
fs-type = swap

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.

Could you guys clarify why I am seeing two zram devices?

# swapon -s

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

zram0 and zram1.

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.

Don’t have zram installed at the moment, but isn’t the default config /etc/systemd/zram-generator.conf? Do you run two configs at the moment?

Having 32 GB memory I simply use only zram.

My /etc/systemd/zram-generator.conf has

[zram0]
compression-algorithm = zstd lz4 (type=huge)
zram-size = ram
swap-priority = 100
fs-type = swap

free -t -h shows

               total        used        free      shared  buff/cache   available
Mem:            30Gi        16Gi       3,9Gi        96Mi        10Gi        14Gi
Swap:           30Gi        25Gi       4,8Gi
Total:          61Gi        42Gi       8,7Gi

It can read from the drop-in file in /etc/systemd/zram-generator.conf.d as well.

But this is a good question. I may have left behind one in /etc/systemd.
I’ll check. Be right back.

No there wasn’t any there. The only one I have is in: /etc/systemd/zram-generator.conf.d
:thinking:

Why 2 compression algorithms? Do they work in combination somehow?