Especially when your system includes SSD or SDcard drives and/or have 8GB RAM or more, using zswap or zram is a great idea.
A usual Linux installation includes the presence of a swap partition, or at the best a swap file, that are usually automatically created from the installers. The reason for using swap is to support low system memory situations, which was always a problem, due to the high cost of RAM and the inability of non-modern systems to utilise the extra RAM memory, if it was present. Nowadays, RAM has become more in excess, as the well known mainstream OS demands a lot of it, just to work in idle..
Brief description of zram and zswap
Summary of their implementations:
- ZRAM is a compressed RAM based swap device
- ZSWAP is a compressed Cache if you already have a swap.
- ZRAM: Makes a swap device in the RAM. Pages sent here are compressed as they are stored. It has a higher priority than other swap devices: pages that are swapped out are preferentially sent to the zram device till it is full, only then are any other swap devices used.
- Benefits: Independent of other (physical) swap devices. It can be used when there is no swap partition to expand the available memory.
- Disadvantages: If other swap devices (HDD/SSD) are present they are not used optimally. As the zram device is an independent swap device, once it is full, any new pages that need to be swapped out are sent to next swap device directly, hence:
- There is a real chance of LRU (least recently used) inversion: It will be the most recently swapped data that goes to the slow disk, while inactive pages that were swapped out long ago will remain in the fast ZRAM
- The data sent to and read from the disk will consume a lot of bandwidth as it is uncompressed.
- ZSWAP: The
frontswapsystem hooks attempts to swap out pages and uses zswap as write-back-cache for a HDD/SSD swap device: An attempt is made to compress the page and if it contains poorly compressible data it is directly written to the disk. If the data is compressed, it is stored in the pool of zswap memory. If pages are swapped out of memory when the total compressed pages in RAM exceeds a certain size, the Least Recently Used (LRU) compressed page is written to the disk as it is unlikely to be required soon.
- Benefits: Very efficient use RAM and disk based swap. Minimizes Disk I/O by both reducing the number of writes and reads required (data is compressed and held in RAM) and by reducing the bandwidth of these I/O operations as the data is in a compressed form.
- Limitations: It is an enhancement of disk based swap systems and hence depends on a swap partition on the hard disk.
Linux kernel has included zram and zswap modules/features, since several years. Not using them, is like having a beautifull super-fast car in your garage and never ride it!
Because of the rare usage of my laptop, as I haven't a lot mobilization needs for a long time, I had not given the proper care to its system. I had only installed an old SSD, after upgrading my desktop with a new one, leaving the proper setup to a later time, which never came..
Because of the unexpected need of a mobile system, the laptop was used extensively for the last two months and only recently I remembered, I had to configure SSD properly. Looking for the best options (again) I stumbled upon the z-terms, which then made be find an older greek article about it. And then I did it. I removed swap partition (from the.. SSD, which was a crime..) and enabled zram. Now my system runs fine, with no swap partition or file nad... I have a "new" 4GB partition to experiment my new idea about a rescue/restore partition on Grub-based systems.
zswapresult in a higher (not crazy high, though) CPU usage. It is a user/distro decision if they use it or not by default, depending on their individual needs and priorities.
This is just an easy and quick guide about a specific way of using zram/zswap with
systemd-swapofficial Arch package. You may want to use
zramswapfrom AUR, or the "Arch way", editing files by hand and enable/disable manually, or even create a service unit, or a udev rule. Always check Archwiki for more options.
- Check your existing swap (example)
swapon --show NAME TYPE SIZE USED PRIO /dev/sda3 partition 4,09G 1,2M 100
sudo pacman -Syu systemd-swap
- Create a file with these contents:
zswap_enabled=0 zram_enabled=1 swapfc_enabled=1
Save the file as (the file name can be anything, as long it's
This file contains your custom overrides to the package defaults at
/etc/systemd/swap.conf. You can add more customizations in the new file, after reading the help comments in the main file.
- Enable and start
sudo systemctl enable --now systemd-swap
- Disable swap
and disable/remove the swap line in your
# UUID=3170c524-1317-493c-8afe-d16c502e8452 swap swap defaults,noatime 0 0
Now you may re-format the swap partition to ext4 or other FS type, in order to use the extra disk space. You can use your favorite partition manager's GUI, like GParted. You need to unmount the partition first, so you don't need to reboot.
After you finish, reboot and check if zram swapping is working
swapon --show NAME TYPE SIZE USED PRIO /dev/zram0 partition 1,9G 1,2M 32767
You may use systemd-swap utility to do several things about normal swap, using zswap instead of zram, etc. Also you may not use it, and use a udev rule or kernel parameters. It was just the easiest way for me to do it!
More info on Archwiki. For Greek language there are two nice articles here and here, that I used them as a guide and to help me understand, which is hard...