[Guide] Setting up zswap
Posted by Alex_Strgzr@reddit | linux | View on Reddit | 13 comments
This is a guide to setting up zswap on Linux-based operating systems. What is zswap and why should you use it? Zswap is a way of giving your computer additional virtual memory, like increasing your RAM. It is very useful for systems with low RAM (< 8 GB) and quite useful even for systems with more RAM. This guide will show you how to setup zswap with the lz4 compression algorithm, which is very fast. But first, to answer some questions.
Warning for BTRFS users: read the wiki before creating a swap file. It easier to use ZRAM instead.
Does this come with a performance penalty?
There is no such thing as free lunch – compressing and decompressing pages in virtual memory will tax your CPU. However, using compressed memory is faster than swapping to an SSD, and orders of magnitude faster than swapping to a spinning hard disk. It is also better than running out of memory, which results in either your system locking up or the out-of-memory killer killing some important process. As oom killers are not very intelligent, it is wise to avoid this.
Note: there is no performance penalty until you actually start swapping, just so we’re clear.
What about ZRAM?
ZRAM is good too; I have used it. But zswap does not compress pages which are incompressible, instead sending them to your swap file. This is a good thing, as it avoids wasting CPU cycles compressing pages that are not compressible anyway. ZRAM is good for hard disk or SD-backed computers; I feel zswap is more appropriate for SSD-backed devices as swapping to an SSD is not such a big problem.
Step 1
Note: run all the commands below in the terminal, copying them one line at a time and hitting enter. Make sure they are copied correctly.
First check if you have a swap file by running free -h
. If you do have a swap file, continue to the next step. Otherwise run the code below.
sudo su
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
Step 2
Run sudo nano /etc/default/grub
and edit the line GRUB_CMDLINE_LINUX_DEFAULT to read:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=50 zswap.zpool=z3fold"
What does the max pool percent variable mean? This refers to the maximum % of your RAM that will be taken up with compressed storage. It is dynamically allocated, so it doesn’t take up any space until you actually start using it. For most systems, 50% is a good maximum. For really low memory systems, you can try 70%. Anything higher will make the system unusably slow (Google has actually benchmarked this for Chrome OS).
Save your changes (type Ctrl+X and type y and then enter). Now run:
sudo update-grub
Step 3
Run the following:
sudo su
echo lz4 >> /etc/initramfs-tools/modules
echo lz4_compress >> /etc/initramfs-tools/modules
echo z3fold >> /etc/initramfs-tools/modules
update-initramfs -u
You are done! Reboot and run cat /sys/module/zswap/parameters/enabled
. If zswap is working, you should see a Y printed.
Originally posted on my blog, which is full of advice, discussion and helpful warnings: https://angry-penguin.blogspot.com/2022/06/guide-setting-up-zswap.html
spryfigure@reddit
Heads up for all reading this in 2025 or later:
z3fold
is deprecated, usezsmalloc
instead.Also, if you want to copy the commands, use the source view, since reddit mashes some lines together if there aren't two spaces at the end.
Check your work with
sudo grep -R . /sys/module/zswap/parameters
to see that everything works as desired.ThisGuysShowsSkills@reddit
even if this is an old post, I have to say that I have found the holy grail, finally after all these years, I can download more ram
MCPEngu1@reddit
hmm what about using zstd instead of lz4 ?
Alex_Strgzr@reddit (OP)
Just change the relevant parameter name. Zstd is slower than lz4 but usually achieves higher compression ratios. With z3fold you are limited to 3:1 compression (excluding deduplication) so you may want to use zsmalloc. However, then you should be using ZRAM, per the documentation:
https://www.kernel.org/doc/Documentation/vm/zswap.txt https://www.kernel.org/doc/html/latest/vm/zsmalloc.html
MarshalRyan@reddit
So, the ZSWAP documentation uses the same language as the kernel documentation regarding zsmalloc, but does NOT include the page-eviction problem.
https://docs.kernel.org/admin-guide/mm/zswap.html
It looks like this was actually patched a long time ago (2014), so it may be a disconnect in the Kernel documentation.
https://lwn.net/Articles/611713/
https://fa.linux.kernel.narkive.com/VzDet7ql/patch-00-10-implement-zsmalloc-shrinking
I've been using zsmalloc with zstd in zswap for some time, and can confirm it does free up ram / swap over time. But, because you highlighted it, I did look into this more deeply - so thank you! Turns out the method described for shrinking seems like it might be relatively slow for page reclamation, and zsmalloc has some other challenges - like fragmentation - that further seem to impact performance. I also came across some evaluations suggesting that while the maximum possible page compression using zsmalloc is like 6x, more realistic workloads showed closer to 3.2x actual realized compression, regardless of actual compressor used - only slightly better than z3fold's maximum. So, I'm going to try out z3fold for a while and see if there's any overall impact.
uselesslogin@reddit
Not sure if you are still using z3fold but now they are talking about deprecating it:
https://lore.kernel.org/lkml/ba2ec393-f484-4c69-aa45-7e9433a71d5d@linux.dev/
zbud has lower latency, zsmalloc saves more ram, and z3fold has both higher latency and ram usage than zsmalloc.
MarshalRyan@reddit
Great note, thanks! The only advantage to z3fold was that it could release unused memory blocks like zbud. zsmalloc has much better memory usage, but I haven't seen them solve the memory recovery issue.
remnant24@reddit
I haven't followed this guide (yet) but I'm wondering if I already have zswap enabled on my system. On the one hand I have:
On the other:
Any insights?
borekon@reddit
Same here....
FryBoyter@reddit
You probably mean a swap file. But that is not correct either. Since kernel version 5.0, btrfs also supports swap files.
With swap files under btrfs, however, there is something to keep in mind (https://wiki.archlinux.org/title/Btrfs#Swap_file and https://github.com/kdave/btrfs-progs/blob/master/Documentation/ch-swapfile.rst).
MarshalRyan@reddit
Yes, swap files with BTRFS, not swap in general.
Incidentally, having worked with both ZSWAP and ZRAM, I agree with the OP and ZRAM is my preferred choice when dealing with swap files on BTRFS. Although, with ZRAM it's not really a true "swap file" - which is why I prefer it. With ZRAM it's the in-memory drive that's formatted as swap, not the on-disk file. The on-disk file with ZRAM is just an empty loop device. In fact, you can't use either a file or partition that has been formatted as swap as the writeback device - it has to be effectively unformatted. So, with ZRAM you basically create an empty file, attach the loop device to it, and there's no special handling otherwise - holes are no longer a problem, standard BTRFS file system handling is no longer an issue, etc. - ZRAM just uses it as needed.
sati@reddit
Running zswap with btrfs with no issues here. What is the problem with btrfs?
MarshalRyan@reddit
The issue isn't ZSWAP with BTRFS, but that if you're using a SWAP FILE as recommended by OP, then there is special handling in BTRFS to use the swap file. If you're using a standard SWAP PARTITION, then there's no need for a separate swap file in the first place, and it just works.