10
September
2020
16:39

Setting up RAM compression with ZSwap

10 September 2020 16:39

When the RAM runs out, the computer becomes very slow as it accesses the swap file on the hard drive. To solve this problem, the zswap module was invented. By default, it is disabled in the Linux operating system, but you can enable it.

Почdid he need me to use zswap? There are 2 2 GB sticks installed, but the motherboard physically supports only 3 GB - this is a limitation of a motherboard based on the Intel 82945 chipset. On another computer, also running Linux, RAM sticks of 1 GB and 2 GB are installed (3 GB in total), they are usually enough for working on the Internet. But sometimes when opening large PDF files or images, RAM may run out...

Choice: zram or zswap

zram is used for permanent page compression - a swap section is allocated in memory, into which compressed memory pages are unloaded as into a regular page file, but in fast RAM.

zswap does not allocate a partition in memory - only a dynamic volume (pool) in RAM, which includes only those pages that are candidates for getting into the physical swap file. Pages are also compressed before they are put into the pool.

Since the memory size is large enough, I don't need to constantly compress the RAM like zram does. It is required to ensure that there is no swapping to disk when the RAM is close to full.

Solution:

  1. In the GRUB bootloader line, which is responsible for passing kernel parameters, I indicate a call to the ZSwap module:

    sudo nano /etc/default/grub

corrected the GRUB_CMDLINE_LINUX_DEFAULT line to the following:

GRUB_CMDLINE_LINUX_DEFAULT="noresume zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=lz4 zswap.max_pool_percent=35"

GRUB_CMDLINE_LINUX=""

  1. Updated GRUB configuration:

    sudo update-grub

  2. Allowed loading of modules for advanced compression

    sudo nano /etc/initramfs-tools/modules

added the lines:

lz4
lz4_compress
z3fold

and saved the changes.

  1. I started updating the initrd file (which contains iniramfs - the initial file system for loading into RAM at the time the OS boots; the second part of the kernel is the static file vmlinuz, which is not changed by this command and contains the kernel itself):

    sudo update-initramfs -u

  2. Reboot the system

    sudo reboot

  3. Check.

  • There was no RAM before setting up the compression module.

    dmesg | grep zswap

  • After the first startup without the lz4, lz4compress modules, the dmesg command output
    [ 2.690663] zswap: loaded using pool lzo/zbud

  • After the final configuration with the inclusion of the lz4, lz4compress and z3fold module, the output of the dmesg command is as follows:
    [ 2.734842] zswap: loaded using pool lz4/z3fold


Notes:

  1. The difference between lzo and lz4 compression methods is in the compression/decompression speed - lz4 has the highest compression and decompression speed among all.
  2. On systems with a small amount of RAM, instead of zswap.zpool=z3fold, it is better to try zswap.zpool=zsmalloc (compression level up to 7).
  3. The maximum pool size for compressed cache pages is 50%, since according to free -h on a computer with 3 GB of RAM, the cache size is 600 MB and 1.1 GB is free (1.7 GB is approximately 50% of the 3GB RAM). On systems with a small amount of RAM, the zswap.max_pool_percent limit should be reduced to 10-15%, so that it is 200-300 MB. The size of the pool (compressed cache) of pages is dynamic.

    Update dated June 10, 2021: after increasing in the configuration file /etc/default/grub parameter zswap.max_pool_percent from 50 to 70 and executing the command sudo update-grub the size of free RAM (free) increased from 213 to 500 MB, apparently due to compression of RAM. In my opinion, the optimal size max_pool_percent for maximum RAM savings lies in the range from 70 to 90%. With the parameter max_pool_percent at the 90% level, I did not notice any slowdown in the operation of the program or the operating system as a whole. After increasing zswap.max_pool_percent to 90, the size of free RAM when the Mozilla Firefox browser is running is in the range of 908...937 MB. The speed of the FireFox browser has also increased.

Addition from 10/21/2021: with max_pool_percent=90, I observed strong swapping on the State Services website when submitting a completed form for the Russian Federation Census. Reduced max_pool_percent=35 (about 1 GB of RAM), executed sudo update-grub and rebooted the PC. Apparently, an excessive increase in the max_pool_percent parameter, above 50%, is undesirable.

Addition 2023. Output of zswap operation statistics

Displaying current ZSwap settings

grep -R . /sys/module/zswap

ZSwap performance statistics (debugging):

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

If zswap is enabled, but there are no compressed pages in the statistics (/sys/kernel/debug/zswap/stored_pages:0), this means that the operating system has enough RAM. In this case, zswap debugging will produce zeros, because There are no memory pages waiting to be loaded into swap.

Sources:



Related publications