Since version 3.11 the Linux kernel offers zswap support. If enabled, the kernel will avoid swapping out pages to disk by compressing them in ram. This will reduce disk I/O which can make systems with mechanical disks feel more responsive or lower the wear of flash based disks. The compressed memory pool is dynamically managed by the kernel. Basically it creates an intermediary memory system between the ram and the disk. This intermediate memory system is more cpu intensive than any of the other two. Although the compressed memory is slower than ram, it's still much faster than mechanical or flash based disks. Latency, compression ratio and speed are dependent on the compressor used with zswap.

Contrary to zram, zswap eventually swaps out the compressed pages to disk. This behaviour can be configured by setting vm.swappiness inside /etc/sysctl.conf . One of the advantages of zswap over zram is that zswap requires no additional configuration in order to allow system hibernation.

Enabling lz4hc

The lz4 algorithm is one of the fastest in terms of compression and decompression out there. The compression ratio is not the highest but it's the clear winner when both compression ratio and speed are taken into consideration. This is specially important in this setting where speed is critical. Some benchmarks [1] [2]

lz4 kernel support can be enabled during runtime using the following commands

$ su - $ modprobe lz4hc lz4hc_compress

To enable lz4hc permanently, simply tell dracut to include the lz4hc kernel modules in the initramfs files. Create a file /etc/dracut.conf.d/lz4hc.conf with the following contents:

add_drivers+="lz4hc lz4hc_compress"

Now regenerate all of your initramfs images

$ su - # dracut --regenerate-all --force

Enabling zswap

By default zswap uses lzo as the compression algorithm. If you to use any other compressor during runtime, for example lz4hc, run:

$ su - # echo lz4hc > /sys/module/zswap/parameters/compressor

In a similar way you can configure the maximum size of the compressed memory pool with respect to the total ram available in your the system. In this example 25 means that zswap can use up to 25% of your total ram for storing compressed memory pages.

# echo 25 > /sys/module/zswap/parameters/max_pool_percent

Now if you want to enable zswap at runtime just run this

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

Permanently enabling zswap is very easy. Add the following contents to the kernel arguments in /etc/default/grub

GRUB_CMDLINE_LINUX=" ... zswap.enabled=1 zswap.max_pool_percent=25 zswap.compressor=lz4hc"