Enable ZSwap to increase performance

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.

5 Likes

For more detailed step by step guide, you can read this Arch Linux Wiki on ZSwap.

1 Like

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.

1 Like

@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.

here is my result (a bit different )

[ 3.324142] zswap: loaded using pool lz4/zbud

I don't know if its activated. BTW thanks

Yes it’s activated.
You’re welcomed.

1 Like

Step 5 is unnecessary (it is unrelated).

Thank you for point it out. I’m updating the post.
:slight_smile:

It seems I can't edit my post. Can the moderators please help?

Hi @SSalekin. I think it hit a time limit I can’t control but was able to edit. Let me know if I goofed anything.:slight_smile:

[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.

Thank you. I can see the edit button now. Also, thanks for updating the post.

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+

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.

1 Like