Compared to the compressed RAM solution, ZSWAP is a kernel-space solution that allows compressing pages before storing them to RAM.
In order to use lz4 compression, initramfs has to be updated to include a few modules.
On Debian, install initramfs-tools which will create a tree under /etc/initramfs-tools. The file /etc/initramfs-tools/module needs to be edited and the following modules:
lz4 lz4_compress
appended to the file.
Once /etc/initramfs-tools/module is updated, issue:
update-initramfs -k all -u
to update the image.
Finally, issue:
update-grub
Upon reboot, the status of the zswap module can be checked by issuing:
dmesg | grep zswap
and a line similar to:
zswap: loaded using pool lz4/zbud
should indicate that lz4 has been loaded.
zswap is initialized on boot and controlled by kernel parameters. Using GRUB, the file /etc/default/grub has to be edited and the following parameters appended to GRUB_CMDLINE_LINUX_DEFAULT:
zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=50 zswap.zpool=zsmalloc
where:
zswap.enabled enables zswap,zswap.compressor selects the compression to use, in this case, zstd ,zswap.max_pool_percent is the percent of RAM reserved to the compression pool (default 20%).zswap.zpool sets the allocator as a choice between zbud, z3fold and, the newest, zsmallocFinally, issue:
update-grub
on the command line and reboot for the changes to take effect.
Depending on the workload some parameters can be tweaked in order to achieve better performance.
The zswap.compressor parameter picks the compressor to be used and the following table summarizes the differences.
| Compressor | Description |
|---|---|
lzo | A very fast instructable compression algorithm. |
zstd | ZSTD compression algorithm. |
lz4 | The LZ4 compression algorithm. |
lz4hc | High-compression algorithm. |
The algorithms are ordered slightly in terms of compression speed and ratio with lzo being unexpectedly the fastest but least achieving and lz4hc being the slowest but most achieving in terms of compression. Nevertheless, different results can be obtained depending on workload. For example, in some benchmarks the zstd algorithm seems to be the mean-average of all the others that seems to be the best choice given its balance between speed and compression ratio.
The zswap.zpool kernel parameter picks the allocator for ZSWAP and there are some options available that might make a difference:
| Allocator | Compression Ratio (Average) | Compressed Objects per Page |
|---|---|---|
zbud | 1.7 | 2 |
z3fold | 2.7 | 3 |
zsmalloc | 3.6 | 3 |
Note that ZRAM creates a swap device(s) using RAM and instructs the kernel to swap on the created device(s). In case plenty of RAM is available, then the swap mark may never be hit. With ZSWAP, given the zswap.max_pool_percent setting, the kernel will always compress RAM up to some proportion.