read 4 min
1 / 25
Jan 2017

What is ZSWAP

Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. Instead of moving memory pages to a swap device when they are to be swapped out, zswap performs their compression and then stores them into a memory pool dynamically allocated inside system's RAM.

You can read more about it in this Beginner friendly introduction.

ZSWAP has been merged into the 3.11 mainline linux kernel, but is not turned on by default.

Benefits
ZSWAP will make very efficient use of swap. It will minimizes Disk I/O by both reducing the number of writes and reads required (data is compressed and held in RAM) and by reducing the bandwidth of these I/O operations as the data is in a compressed form. Thus you will not face sudden slowdowns when your system runs out of memory and tries to read/write the swap drive.

For whom it wont work

  • Who does not have a SWAP partition
  • People with less than 1ghz CPU speed
  • People who uses ZRAM

For whom this tweak will work

  • People who have a dedicated SWAP partition / SWAP file
  • People with low system memory

Steps to enable ZSWAP

  1. Check if your OS Kernel comes with ZSWAP by running the command below.
    If the response is CONFIG_ZSWAP=y, you are OK to proceed.

cat /boot/config-`uname -r` | grep -i zswap

  1. Open grub as admin

gksu pluma /etc/default/grub

  1. Change the line GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" to

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.compressor=lz4"

  1. Save, close Pluma. Update Grub by running

sudo update-grub

  1. Enable lz4 (for very fast compression/decompression)

sudo su
echo lz4 >> /etc/initramfs-tools/modules
echo lz4_compress >> /etc/initramfs-tools/modules
update-initramfs -u

  1. Restart your PC.

  2. Check if ZSWAP is enabled by running (Mentioned by @1Q7FE6zp)

cat /sys/module/zswap/parameters/enabled

If ZSWAP is enabled, you should see Y in return.

To check if LZ4 compressor is being used run

dmesg | grep -i zswap

Check the last line of the response. If the result says

[ 0.715381] zswap: loaded using pool lz4/zbud

Then voila ! You've successfully enabled ZSWAP.

read 4 min

I’ve used zswap before. It’s nice for what it is, but I hardly ever use my swap space.

There was only one time on the system I am typing this from right now where swap was used. It was when my system load average was super-high, and my CPU was being hammered when I ran our of RAM and my PC was taking a massive poop all over itself.

@tiox, I envy you. My current machine has only 4 gigs or ram. And I need to keep an instance of postgresql, rails puma server, chromium with developer tools, atom / vs code / sublime text, 2nd browser with api documentations open most of the time. That’s when my system starts writing on the swap file, and simple alt-tab takes 2-3 seconds. After adding zswap, my problem has mostly disappeared.

But, I get your point, I should’ve mentioned ‘People with low system memory’ on the post.

Well, I don’t do much with my Linux system beyond keeping it around for a “Plan B” and also using it when I don’t want to touch anyone’s Windows system.

So yeah, general-purpose things. Nothing that would tax my system’s memory too heavily. But for users with low system memory, or even or users with sufficient system memory every little bit of performance counts. ZSwap probably works better with more memory because there is more swap for it to write to whenever it’s necessary.

Doesn’t take a genius to know more is better. But also, as a cost-effective solution for users with little memory to begin with (until they upgrade and max board capacity), it’ll work. Also probably nice for embedded system manufacturers because they can (in theory) put ZSwap into the thing and with a little more space taken up on embedded storage they can stretch the memory further.

Not to mention, SFF embedded systems are a boon these days with M.2 and U.2 being easy means to add interchangeable storage, and systems only getting smaller all the time.

2 months later

here is my result (a bit different )

[ 3.324142] zswap: loaded using pool lz4/zbud

I don't know if its activated. BTW thanks

2 months later

[quote=“SSalekin, post:8, topic:11302, full:true”]Yes it’s activated. You’re welcomed.[/quote]Actually, not necessarily. That line just says it has been loaded. But, basically, zswap is always loaded. Whether or not it is actually enabled is a different story.

Terminal:

cat /sys/module/zswap/parameters/enabled

Y = Enabled
N = Disabled

For example, in my case:

dmesg | grep -i zswap

Output:

[ 2.846273] zswap: loaded using pool lzo/zbud

But:

cat /sys/module/zswap/parameters/enabled

Output:

N

And no, I do not need it enabled. Just demonstrating that it can be loaded without being enabled. :wink:

I’m glad you’ve pointed that out. My English is to blame here. When I said Yes it's activated, I meant LZ4, not ZSWAP. But I see it’s misleading. I will update my post mentioning it. Thank you so much :grinning:

Thank you :slight_smile:

I still can't see the edit option myself. So, can you please replace step 7 of the post with these lines please? Thank you.

=========================================================================
7. Check if ZSWAP is enabled by running (Mentioned by @1Q7FE6zp)

cat /sys/module/zswap/parameters/enabled

If ZSWAP is enabled, you should see Y in return.

To check if LZ4 compressor is being used run

dmesg | grep -i zswap

Check the last line of the response. If the result says

[ 0.715381] zswap: loaded using pool lz4/zbud

Then voila ! You've successfully enabled ZSWAP.

=========================================================================

My answer was somewhat wrong and misleading,

[ 3.324142] zswap: loaded using pool lz4/zbud

This line means that zswap is using LZ4 successfully, but doesn't make sure zswap itself is activated. (pointed out by @1Q7FE6zp)

Please see this to check if zswap is running.

Again, I apologise for the misleading answer.

At one time, all tips were supposed to be a Wiki so I made it that way for editing. How about now? I ran into an unfortunate formatting of number lists with the attempt.

2 years later

I executed the commands in the post above, but after reboot and checking the zram enable (commend=cat /sys/module/zswap/parameters/enabled), it gave me (output=N)

I executed the following commands and got the outputs as below:

  1. cat /boot/config-uname -r | grep -i zswap
    output =CONFIG_ZSWAP=y

  2. gksu pluma /etc/default/grub <<====
    gave me an empty file. I did not find GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

  3. add GRUB_CMDLINE_LINUX_DEFAULT=“quiet splash zswap.enabled=1 zswap.compressor=lz4”
    this works OK

  4. sudo update-grub
    output =command 'update-grub' not found

  5. Enable lz4 (for very fast compression/decompression)
    this works OK

  6. sudo reboot

Note: I'm using ubuntu-mate 18.04 in raspberry pi3 B+

4 months later

Hi. Could you add to the thread "how to disable or uninstall ZSwap", please? I've installed it and I feel no difference. I could say that, maybe, the system behave is worse than before... Just turning down the part:

is enough?

Well, and updating GRUB:

Update Grub by running

sudo update-grub

And restarting, yeah?


But thank you for the theme. Is good to know that this exists.

You would basically reverse the steps you did to install it. Turing it off would be as simple as undoing the changes you made in GRUB and removing the lines you appended onto /etc/initramfs-tools/modules. Once you reboot, then uninstall ZSwap and it's done.

Yeeeeh! You're right! I forgot to do the thing about the modules. Thank you. Now is ok.

About the performance, yeah I never noticed a difference either, and it in fact made my system perform worse. I am sure in the correct scenario the opposite would be true, but in my particular use case this isn't doing.

I suspect, because I can't be certain, that this solution was doing "something". I think it was achieving a more relaxed system, but I'm not sure. Now, I have changed strategy :smile: I have installed LxQt instead Mate and modified swappines to 25, instead 60.

So far, the system is a bit more fast, but I'm just starting. I have to try more things, more times. We will see...

2 years later
5 months later

Yes, look into file /usr/bin/init-zram-swapping (you can search it with listing the package files using dpkg -L zram-config) , this is the script that creates the zram swap devices. In this file there's this line:

mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

The part that you can tweak is / 2 . So by default it takes total amount of memory and divides it by 2 and takes that as the total reserved for Zram .

So if you want to make Zram use all the memory just remove that part.
There's this answer on StackExchange that one should set this to 150% of total memory , so * 3 / 2 and then also set vm.swappiness=200 in /etc/sysctl.conf :

Though my experience is that the default value suits me well.
I've also set my disk swap to the same priority 5 that Zram has it set by default, so in /etc/fstab I have pri=5 in the disk/ssd swap device arguments . That way the resource consumption is a bit more balanced between CPU load due to Zram and SSD throughput due to disk swap.


Powered by Discourse