How to compress disk swap

I am looking for a way to compress swap on disk. I am not looking for wider discussion of alternative solutions. See discussion at the end.

I have tried…

Using compressed zfs zvol for swap is NOT WORKING. The setup of it works, swapon works, swapping does happen somewhat, so i guess one could argue it’s technically a working solution, but exactly how “working” is your working solution, if it’s slower than floppy disk access, and causes your system to completely freeze forever 10 times out of 10? Tried it several times — soon as system enters memory pressure conditions, everything just freezes. I tried to use it indirectly as well, using losetup, and even tried using zfs zvol as a backing device for zram. No difference, always same results — incredibly slow write/read rates, and system inevitably dies under pressure.

BTRFS. Only supports uncompressed swapfiles. Apparently, only supports uncompressed loop images as well, because i tried dd-ing an empty file, formatting it with regular ext2, compressing it, mounting as a loop device, and creating a swapfile inside of it. Didn’t work, even when i mounted btrfs with forced compression enabled — compsize showed the ext2 image compression ratio of exactly 1.00 .

Zswap — it’s just a buffer between ram and regular disk swap. The regular disk swap keeps on being the regular disk swap, zswap uncompresses pages before writing them on there.

Zram — has a backing device option since it’s inception as compcache, and one would think, is a perfect candidate to have had compressed disk swap for years. No such luck. While you can do writeback of compressed in-ram pages to disk at will, the pages get decompressed before they’re written. Unlike zswap, doesn’t write same- and zero-filled pages though, which both saves io, slightly improves throughput, and warrants the use of loop-mounted sparse files as backing_dev. So far, this is the best option I found for swap optimization on low-end devices, despite it still lacking disk compression.


Any ideas what else I can try? Maybe there’s some compressed block device layer, that I don’t know of, that can compress anything written to it, no filesystem required? Maybe there’s some compressed overlay I could make use of? Not done in FUSE though, as FUSE itself is a subject to swapping, unless you know a way to prevent it from being swapped out.

Since i don’t see this being explored much, you’re welcome to suggest any madness you like. Please, let’s throw stuff at the wall and see what sticks.

For experts — if any of you have read, or even written, any part of linux sourse code that relates to this problem, please describe in as much detail as possible, why do you think this hasn’t been implemented yet, and how do you think it could be implemented, if you have any idea. And obviously, please do implement that if you can, that’ll be awesome.


Discussion

Before you mark it as a duplicate — I’m aware there have been a few questions like that around stackexchange, but none i saw had a working answer, and few had any further feedback. So I’ll attempt to describe details, sort of aggregate everything, here, in hopes that someone smarter than me can figure this out. I’m not a programmer, just a user and a script kiddie, so that should be a pretty low bar to jump over.

just buy more ram, it’s cheap

get an ssd

swap is bad

compression is slow anyway, why bother

If all you have to say, are any of the above quotes — go away. Because the argument is optimization. However cheap RAM is these days, it’s not free. Swap is always needed, the fact that it’s good for the system to have it, has been established for years now. And compression is nothing, even “heavy” algorithms perform stupidly fast on any processors made in the last decade. And lastly, sure, compression might actually become a bottleneck if you’re using an ssd, but not everyone prioritizes speed over disk space usage, and hdd drives, which DO benefit grossly from disk compression, are still too popular and plentiful to dismiss.