How to Enable Zswap for Ubuntu 24.04 PC with Limited RAM

Last updated: September 6, 2024 — 4 Comments

This tutorial shows how to enable zswap kernel module to increase usable RAM in your Ubuntu computer or laptop.

As you know, Ubuntu Linux usually includes a swap space (either disk partition or swap file) on the disk. It can be used for hibernation, and as extended memory space. When the system needs more memory resources and the RAM is going full, then the inactive pages in memory are moved to the swap space.

Swap space can be helpful for computers with limited RAM. But, it’s slow! Too much swap in use will slow down the overall system performance.

As workarounds, Linux includes 2 kernel modules: zram and zswap. Instead of moving inactive pages from memory to disk (swap space), they do similar things by compressing the data into dynamically allocated RAM space.

As the data is compressed, it takes less physical memory space which makes your RAM even more usable. While, keeping the system running smoothly because reading from compressed data in RAM is usually faster than reading from swap on disk.

zram and zswap

zram creates a block device in RAM, that acts as swap space (or a general-purpose RAM disk). When system needs to swap out inactive pages, zram compress them into the block device. No swap on disk is required.

zswap is a lightweight compressed RAM cache for swap pages. It attempts to compress the pages, that are needed to swap out, and store them into a memory pool in RAM. Poorly compressible data is directly written to disk (swap space). And, when the memory pool is full or the RAM is exhausted, the least recently used page is decompressed and written to disk (swap). A swap space on disk is required!

Both zram and zswap do NOT use any RAM initially. They dynamically grow or shrink RAM usage as required. The difference is that zram works as swap space with higher priority, so hibernation won’t work normally. While zswap needs a swap space on disk to work along with.

Use zram or zswap is a good choice for computer with small amount of RAM, or for large apps/games that need more memory than physical RAM capacity.

However, if you have a plenty of RAM that rarely uses swap space, or you’re CPU-bound (bottlenecked by the CPU), then you may stay away from them, because they consume more CPU for compressing/decompressing.

Option 1: Enable Zswap in Run-time

If you need swap for hibernation feature, then zswap is the better choice. All current Ubuntu releases have the kernel module loaded by default, all you need to do is just enable the function and do few parameter changes.

1. First, press Ctrl+Alt+T on keyboard to open up a terminal window. Then, run command to check current zswap module parameters:

systool -v -m zswap

Run sudo apt install sysfsutils if command not found. In the terminal output, it should print that “enabled = “N”” meaning the feature is not enabled.

All the module parameters can be modified by the config files under /sys/module/zswap/parameters/ directory. They include:

  • accept_threshold_percent – The percentage threshold at which zswap would start accepting pages again after it became full. Default in 90% in Ubuntu.
  • compressor – Set the compression algorithm. Possible values include zstd, deflate, lzo, 842, lz4 and lz4hc. See the comparision.
  • enabled – enable / disable the function by setting value 0 or 1.
  • exclusive_loads – invalidate zswap entries when pages are loaded. Values include 0 or 1.
  • max_pool_percent – the maximum percentage of physical memory that zswap can take.
  • zpool – control the management of the compressed memory pool. Values include zbud (2:1 or less compression ratio), z3fold (3:1 or less), zsmalloc(more complex method since Kernel 6.3).

2. To enable zswap, simply write “1” to the enabled config file by running command in terminal:

echo 1 | sudo tee /sys/module/zswap/parameters/enabled

And, you may change the zpool value to zsmalloc via command:

echo zsmalloc | sudo tee /sys/module/zswap/parameters/zpool

You can as well run similar commands above to configure more zswap parameters. To verify, simply run the systool command in step 1 again:

3. To see zswap statistics, simply run the command below in terminal:

sudo grep -r . /sys/kernel/debug/zswap

Option 2: Enable zswap on startup

The previous commands enable zswap on the run-time, but it won’t persistent after system reboot.

To make it permanent, you may configure the Grub boot-loader to load the Kernel parameters on startup.

1. First, open terminal (Ctrl+Alt+T) and run command to edit the config file for Grub:

sudo nano /etc/default/grub

Here I use nano command line text editor that works in most desktop environments. You may replace it with gnome-text-editor for 24.04 with default GNOME, gedit for 22.04 and earlier, or other editors depends on your desktop.

2. When file opens, add following kernel parameters as value of “GRUB_CMDLINE_LINUX_DEFAULT”:

zswap.enabled=1 zswap.zpool=zsmalloc

You can add/remove more zswap parameters as you want (e.g., zswap.compressor=lzo), and separate them with blank space. Finally, press Ctrl+S to save and Ctrl+X to exit nano text editor.

3. After saving the changes, run the command below to re-generate Grub boot menu entries:

sudo update-grub

Tips: For those who have Grub-customizer installed, just launch the app and navigate to “General Settings” page for adding the Kernel parameters, and finally click Save to apply changes.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

4 responses to How to Enable Zswap for Ubuntu 24.04 PC with Limited RAM

  1. Thats great and speed up my Sytem a little!
    I have a Laptop with 4 GB Memory (DDR 3) and this Tutorial works well on LMDE 6 (based on Debian12) too.

  2. Hi Ji,
    great article.

    Please check, I think zswap.zool=zsmalloc should be zswap.zpool=zsmalloc

    here:

    2. When file opens, add following kernel parameters as value of “GRUB_CMDLINE_LINUX_DEFAULT”:

    zswap.enabled=1 zswap.zool=zsmalloc

  3. also, people should consider installing the package swapspace, which uses a dynamic swapfile, like windows and macos

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*