Zswap is a kernel feature that provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to disk are instead compressed and stored into a memory pool in RAM. Once the pool is full or the RAM is exhausted, the least recently used (LRU) page is decompressed and written to disk, as if it had not been intercepted. After the page has been decompressed into the swap cache, the compressed version in the pool can be freed.
In the stable
CONFIG_ZSWAP_DEFAULT_ON flag in the stable kernel config.
To disable zswap at runtime, execute the following command:
# echo 0 > /sys/module/zswap/parameters/enabled
To disable zswap permanently, add
zswap.enabled=0 to your kernel parameters.
/etc/systemd/swap.conf. In this case you must start/enable
Zswap has several customizable parameters. The live settings can be displayed using:
$ grep -R . /sys/module/zswap/parameters
/sys/module/zswap/parameters/same_filled_pages_enabled:Y /sys/module/zswap/parameters/enabled:Y /sys/module/zswap/parameters/max_pool_percent:20 /sys/module/zswap/parameters/compressor:lz4 /sys/module/zswap/parameters/zpool:z3fold /sys/module/zswap/parameters/accept_threshold_percent:90
See the zswap documentation for the description of the different parameters.
The boot time load message showing the initial configuration can be retrieved with:
$ dmesg | grep zswap:
[ 0.317569] zswap: loaded using pool lz4/z3fold
Each setting can be changed at runtime via the sysfs interface. For example, to change the
# echo lz4 > /sys/module/zswap/parameters/compressor
Using kernel boot parameters
To persist the parameter change, the corresponding option, for example
zswap.compressor=lz4, must be added to the kernel boot parameter. Therefore to set permanently all the above settings, the following kernel parameters must be added:
zswap.enabled=1 zswap.compressor=lz4 zswap.max_pool_percent=20 zswap.zpool=z3fold
When changing the compression algorithm via boot parameter, one needs to ensure the corresponding compression module is loaded early during boot (refer to #Compression algorithm).
For the ones using the systemd-swap script, it modifies the sysfs parameters at a later stage of the boot process based on its configuration stored in
Maximum pool size
The memory pool is not preallocated, it is allowed to grow up to a certain limit in percentage of the total memory available, by default up to 20% of the total RAM. Once this threshold is reached, pages are evicted from the pool into the swap device.
The maximum compressed pool size is controlled with the parameter
Compressed memory pool allocator
The zpool parameter controls the management of the compressed memory pool.
With the zbud data allocator, 2 compressed objects are stored into 1 page which limits the compression ratio to 2 or less.
The superior z3fold allocator allows up to 3 compressed objects by page. The compression ratio with z3fold typically averages 2.7 while it is 1.7 for zbud.
A zpool of type z3fold is created by default. Use the kernel parameter
zswap.zpool to select another method at boot time. The data allocator can also be changed at a later stage via the sysfs interface.
For page compression, zswap uses compressor modules provided by the kernel's cryptographic API. It uses by default the lz4 compression algorithm but this can be changed with
zswap.compressor at boot time. Other options include deflate, lz4hc, lzo, lzo-rle, 842 and zstd.
There is no issue changing the compression at runtime using sysfs or via systemd-swap but zswap starts in this case with lz4 and switches at a later stage to the defined algorithm. To start zswap with another algorithm straight away, this must be set via the kernel boot parameters and the corresponding module must be loaded early by the kernel. This can be achieved by following these steps:
- Add the modules related to the chosen compressor to the mkinitcpio#MODULES array.
- Regenerate the ramdisk environments after modifying mkinitcpio configuration: see mkinitcpio#Image creation and activation.
zswap.compressorto your chosen algorithm in the kernel parameters.
On next boot, see #Current parameters to check if zswap now uses the requested compressor.