[Raspberry Pi 5 2 GB] Zswap vs Zram?

I’m not even going to pretend I’m not completely lost here, so I’ll jump straight to it:

On a 2 GB Raspberry Pi that I want to optimize performance/maximize available RAM/swap on, which is better: zram or zswap?

I was going to go with zram after seeing a Gary Explains video, but along the way I ran into zswap, and read just enough about both of them to confuse myself quite effectively.

Boot disk: USB 3-connected enterprise SATA SSD.

Use Case 1: Grafana/Grafana Loki Server

Use Case 2: Raspberry Pi OS/Xubuntu box for basic web browsing.

I’m not sure if those use cases matter; I suspect the answer is more about the hardware itself.

I’d appreciate any advice on which of zram or zswap is the better option to use here, or at least a real, practical explanation of the difference so that I can make the decision in a more informed state. :stuck_out_tongue:

Thanks!

Are you even close to running out of memory?

The difference is zram is a generic compressed tmpfs-like store that you can use as swap[1], zswap is integrated in the swapping mechanism to go to a region of memory first before putting the compressed zswap contents on disk[2]. Stuff in zram gets decompressed before going to any other swap device (which is going to change soon[3]).

1: zram: Compressed RAM-based block devices — The Linux Kernel documentation
2: zswap — The Linux Kernel documentation
3: Modernizing swapping: virtual swap spaces [LWN.net] , this comment: Writing back compressed pages [LWN.net]

Thanks for the article recs. I’ll check those out. :slight_smile:

I’ve got a 2 GB Pi, and the base Ubuntu Server, with SSH enabled and some useful autoconfigured services running (SMART monitoring, the fstrim timer). I’ve not customized anything or actually set up any workloads yet.

So, with a pretty much stock system, I’ve got this:

root@statler:~# free -h
               total        used        free      shared  buff/cache   available
Mem:           1.9Gi       334Mi       1.2Gi       3.4Mi       473Mi       1.6Gi
Swap:             0B          0B          0B

Honestly, 334 MiB used/1.6 GiB available isn’t terrible for what I’m hoping will be a dedicated homelab log centralization/metric server–in particular given what I’ve read about Grafana Loki using relatively little RAM in a basic configuration–but I’m very conscious of the need to be very conscious of my RAM usage in this situation.

(I’m going to rip out snapd as soon as I figure out how to do that. With no snaps installed, on a fresh system, it’s idling at 36 MiB … which actually matters here.)

Still, there’s a good chance I can avoid swap, but we’ll see.

(Honestly, it’s good exercise in any case. My usual servers (two Proxmox nodes) both have more RAM than I actually need–they were spec’d and built before the Rampocalypse, and I’m old fashioned and bought as much as I could afford.)

So, I figure now is the time to learn about zram and zswap, before I actually need them.

More pressing: I have a second Raspberry Pi 2GB I want to set up to basically act as a web browsing kiosk with Xubuntu (installed on top of Ubuntu Server 25.10), and 2 GB will be a bigger constraint there. I suspect swapping will be unavoidable, so I want to set myself up for the least bad experience. (I know swap isn’t magic, and 2 GB isn’t much, but. Ram shortage.)

Which of zram or zswap would you use in those scenarios? When I’m reading the material, it might help to keep an actual practical use case in mind from someone more familiar with the technologies. Just from what you wrote, unless there’s some reason not to use it, zswap sounds like the better idea, as it’s actually part of the native swap system.

  • Yes, I need to learn what free vs available means, let alone buff/cache and the others.

An important thing to note about zswap is that it requires swap to work. From your free -h output, it looks like you will need to manually configure swap in addition to enabling zswap if you choose to use zswap.

If you wanted, you could benchmark your system with no swap vs swap vs swap+zswap vs zram vs swap+zram (and take it further by configuring how much memory zswap and zram can use, and which compression algorithm they use).

…But the easiest thing would be to just not do all of that and see how the system works, then installing the systemd-zram-generator package if you feel like you need to. systemd-zram-generator automatically creates a zram swap with either half the ram or 4GB (whichever is smaller).


I would recommend reading the arch wiki’s articles for zram and zswap to learn more. Here is the manpage for systemd-zram-generator’s config file zram-generator.conf.

Here is a short article that should give you enough information about free and available memory: https://www.linuxatemyram.com/

Tip: Don’t use both zswap and zram at the same time. They basically do the same thing so it would end up being a waste of CPU cycles having them both enabled.

@Tidal4861 I got distracted hard by real life, unfortunately, and I’m just now getting back to this. I really appreciate your message and the sanity check–and the recommended reading.

I’ve decided to push on with my overclocked (2.8 GHz CPU/1.1 GHz GPU) 2 GiB Pi for now and see how far I get without any sort of swap at all, but I’ll definitely go with the systemd-zram-generator if it comes to that. Honestly, I need more easy-mode buttons in my life. :slight_smile:

Also, I have some VMs on my Proxmox cluster that have default Ye Olde Swap on them. It might be useful to try the systemd-zram-swap generator on them, as well.