I don't like zram on SSD

I don’t know if this is the right category to post this. I’d like to get suggestions from other users, but I’m also giving feedback about the way Fedora is built, that could hopefully reach the development team.

I used to have a laptop with 4 GB of RAM, swap, and an HDD. When I ran out of RAM, the whole system would start to freeze (not only the app that’s consuming a lot of RAM). This is probably the case where zram could help.

I now have a laptop with 8 GB of RAM, and an SDD. I used to have Ubuntu and it worked great RAM and performance wise.

But now I’ve installed Fedora and it uses swap-on-zram instead of swap on disk, which means my available memory is limited by just a bit larger than the amount of RAM I have.

I think the decision to use the early OOM killer is good, since the machine doesn’t slow down, but when I run out of memory, due to it, my Brave (Chromium-based browser) are getting killed, which is quite annoying, especially when working under time pressure to meet a deadline.

On my laptop, my user experience has worsened due to this.

Furthermore, I won’t be able to hibernate my machine this way.

I’m asking 3 things:

  • What was the rationale for choosing zram, especially on a setup like this?
  • I think that, for SSD users, regular swap should be used by default, and a clear option to use swap should be available.
  • What’s a suggested way to disable zram and revert back to regular swap?

Disabling zram on swap is described in the change proposal here:

But I’m not quite sure I understand the scenario you’re describing. From what I’ve seen, zram helps significantly in tight memory situations, as it’s typical to get 2× or 3× compression.

1 Like

Disabling zram on swap is described in the change proposal here:

Thanks for the instructions to disable it!

But I’m not quite sure I understand the scenario you’re describing. From what I’ve seen, zram helps significantly in tight memory situations, as it’s typical to get 2× or 3× compression.

Yeah, you might have more RAM but for example I use multiple Electron apps, GNOME and a Chromium-based browser, which eat up the RAM. The moment you run out of RAM your browser tabs keep getting killed. I could screenshot it the next thing it happens, as it’s quite annoying, and in the worst case scenario you’re filling out a long complex form and your tab gets abruptly killed (didn’t happen to me, but it’s plausible).

Instead of just disabling zram, a nice middle ground could be to use zram, but add a swapfile or partition when, even with compression, you run out of memory. Would this be possible to do? If so, how can I configure this?

Hello @gabrc52,
Welcome to the discussion area. Of course you can set up a swap partition, and if it was a new install with BTRFS as the default filesystem, it is relatively easy to do. I believe it may be useful to install the blivet-gui for your partition modifications as it provides more capabilities than the Disks utility. I currently run F33 Silverblue with zram and a swap partition since I have only 8GB, which is somewhat low these days compared to the newer gear that is available.
The way zram is setup, it will use the swap on disk (if it exists) when required due to low ram. Also, you may want to tweak your zram a bit, I think there are ways to tune it’s behavior to suit your needs.

3 Likes
1 Like

Thanks, I already tried this, but it didn’t apply on boot. While it’s what comes up on search results, it seems to be for Fedora 14.

after running swapon /swapfile, I get the following output:
swapon: /swapfile: swapon failed: Invalid argument

After a reboot, running swapon doesn’t show the swapfile.

I’m also getting this on my dmesg:

[12104.188290] BTRFS warning (device dm-0): swapfile must not be copy-on-write, so I’ve ran chattr +C /swapfile following https://wiki.archlinux.org/index.php/Btrfs#Copy-on-Write_(CoW)

But after running swapon /swapfile, it outputs the same thing.

1 Like

Try this way:
https://wiki.archlinux.org/index.php/btrfs#Swap_file

Thanks, I tried it but I’m still getting “read swap header failed”. I’ve tried mkswap before and after these instructions, if it’s after, I get the following output:

mkswap: error: swap area needs to be at least 40 KiB

1 Like

Thanks, making a swap partition with Disks or blivet gui works, although I’d prefer if it was encrypted. I’ll use this in the meantime.

sudo btrfs subvolume create /swap
sudo touch /swap/swapfile
sudo truncate -s 0 /swap/swapfile
sudo chattr +C /swap/swapfile
sudo chmod go= /swap/swapfile
sudo btrfs property set /swap/swapfile compression none
sudo dd if=/dev/zero of=/swap/swapfile bs=1M count=1000
sudo mkswap /swap/swapfile
sudo swapon /swap/swapfile
sudo swapon -s
sudo tee -a /etc/fstab << EOF > /dev/null
/swap/swapfile none swap defaults 0 0
EOF

Works for me, tested in a Fedora 33 VM.

3 Likes

This seems like a good candidate for Fedora Quick Docs. @vgaetera — would you be interested in contributing a quick guide there?

3 Likes

In this way you have created a subvolume on another subvolume, called “nested”, the problem is that if you create a snapshot of the root lsubolume and then restore the snapshot, you will not find the contents of the “swap” subvolume.
The best way to create a swapfile is the “flat” way, that is to create the subvolume on the toplevel.
As explained by the Btrfs maintainer: https://www.spinics.net/lists/linux-btrfs/msg103213.html
Also explained by Chris Murphy: https://www.spinics.net/lists/linux-btrfs/msg103190.html
These are the steps I take to create the swapfile on Btrfs, no problem with snapshot and restore:

  • sudo mount -t btrfs /dev/sdYN /mnt
  • sudo btrfs subvolume create /mnt/swap
  • sudo chattr +C /mnt/swap
  • sudo truncate -s 0 /mnt/swap/swapfile
  • sudo fallocate -l 2000M /mnt/swap/swapfile
  • sudo chmod 600 /mnt/swap/swapfile
  • sudo mkswap /mnt/swap/swapfile
  • sudo umount /mnt
    I edit /etc/fstab to mount the subvolume and point the swapfile to the subvolume directory:
    UUID=yourUUID /swap btrfs noatime,subvol=swap
    /swap/swapfile none swap defaults 0 0
2 Likes

I only use Btrfs on a testing VM and I’m afraid that my understanding is superficial.
Thus it might be wiser if someone more experienced verifies and clarifies these instructions before their publication.