Mounting swap on zram with discard

Hi, guys! Zram supports discard with really useful semantics, immediately returning the pages backing freed blocks to the kernel. How about changing the /etc/init.d/zram script and adding the -d option to the swapon command? (I've actually been doing this on my builds for a long time.) Thanks!

3 Likes

Oh. That's a good point! Thanks for the advice. I've investigating and zram seems to return the pages as needed but the discard option will definitely help to perform the action explicit.

I'm using a custom version of the script because it did not match my quality criteria, so in turn of your advice, maybe you can find something useful in here:


Ignore the comments about 1/4 of the ram. I forgot to update it.

1 Like

Juicy! You size your zram swap device based on the selected compression algorithm's expected compression ratio, that would also be quite nice to have. :smiley: I do the calculation by hand and just specify the calculated size in /etc/config/system.
What kernel version are you running, in order to have zstd (which I'm really looking forward to)? And what's your use case for zswap?
All in all, wonderful stuff! Thanks!

Well, I'm using a custom kernel for my "Comunity Build". Kernel 4.19 includes zstd but need to be enabled. zstd is (theorically) upto 5 times faster than deflate. Still slower than lz4 and lz4hc.
In order for the script to excels you'll need to use your custom kernel with zstd and lz4hc, and to manually change the algorith im /etc/config/system because LuCI does not recognize it. Also, leave empty the size and let it guess a "best case" value based on the expected compression ratio.
This is the relevant kernel seed:

The use case for zswap is that it's able to provide decent savings and when using lz4 at almost no delay/CPU cost. While evicting older pages to zram which requieres more time but does a better job at compressing. The relevant explanation is found here:


So, in theory your kernel can get pages in and out in the fastest way possible from the zswap pool as needed, and it will evict pages that are not used in log time periods to zram, allowing the kernel to place more "rarely used" pages in zswap.

And the relevant script that enables zswap is here:

You should note that they are scripts in /bin, that's because I updated all of them to be procd services, as shown here:


Why I would do such a crazy thing? Well... I begun my days as developer in XDA Developer (always in my heart) about 7 or 8 years ago. So we cared a lot about memory costraints and optimizations for what Android and ARM was, in general, in that moment. So I have some crazy techniques under my sleeve.

1 Like