# Zswap

Related articles

- Kernel parameters
- Mkinitcpio

"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." - Wikipedia

zswap is a compressed RAM cache for swap devices. Pages which would otherwise be swapped out to disk will instead be compressed and maintained in RAM until RAM is exhausted, after which the least recently used (LRU) pages will be sent to disk. This is in contrast to zram, which is a swap device in RAM and does not require a backing swap device. The long and the short of it is that you need to set up a Swap device in order to use zswap.

## Enabling zswap

To enable zswap at runtime, execute the following command:

    # echo 1 > /sys/module/zswap/parameters/enabled

To enable zswap permanently, add this to your kernel boot parameters Kernel parameters#Configuration:

    zswap.enabled=1

Tip: You can use the systemd-swap package to enable and configure zswap.

## Customizing zswap

By design, zswap has relatively few customizable parameters.

### Customize the maximum allowed size

zswap does not use a preallocated pool of memory to hold compressed and swapped data. If you want to put a maximum bound on the percentage of memory that zswap can use, add this to your kernel boot parameters:

      zswap.max_pool_percent=25

This can also be set in `sysfs`.

### Changing the compression algorithm

zswaps compression algorithm can also be set as a kernel boot parameter:

      zswap.compressor=lzo #deflate #lz4

it can also be set at runtime by writing it to /sys/module/zswap/parameters/compressor, e.g:

      echo lz4 > /sys/module/zswap/parameters/compressor

#### Enable LZ4 compression (faster than lzo and deflate, less compression)

1. Add `lz4 lz4_compress` to the Mkinitcpio#MODULES array.
2. Rerun `mkinitcpio`.
3. Add `zswap.compressor=lz4` to your kernel parameters.
4. Reboot
5. Check dmesg :

       $ dmesg | grep 'zswap.* compressor'
       [    0.307223] zswap: using lz4 compressor

## See also

- Zswap documentation.
- zswap: How to determine whether it is compressing swap pages?.
- IBM Developer Works Article (with benchmarks).
- Ask Ubuntu: zram vs. zswap vs. zcache.
- Archlinux forum thread.

Category:

- Kernel