zram, formerly called compcache, is a Linux kernel module for creating a compressed block device in RAM, i.e. a RAM disk with on-the-fly disk compression. The block device created with zram can then be used for swap or as general-purpose RAM disk.
Important
Parts of this guide assumes that you are using the BTRFS filesystem and GRUB.
This details how to setup Zram to be used as an alternative to swap.
-
First and foremost, ensure that swap is currently disabled:
inxi -j
Sample output:
Swap: Alert: No swap data was found.
If swap is enabled, disable it first.
-
Install the
zram-generator
package usingyay
. -
Prepare a
zram-generator
config:Create the config file:
sudo nano /etc/systemd/zram-generator.conf
Sample content of the config:
[zram0] zram-size = ram / 2 compression-algorithm = zstd swap-priority=60
[!TIP]
This sets thezram-size
to half of your total system memory. To set it to full, set the value toram
.[!TIP]
Theswap-priority
variable is optional and can be set from-1
(lowest priority) to32767
(highest priority). This variable defaults to100
if not set. -
Reload and start the relevant services:
-
Reload the systemd manager configuration:
sudo systemctl daemon-reload
-
Start the Zram service we had configured (i.e.
/dev/zram0
).
-
-
Disable Zswap for maximum efficiency with using Zram:
-
Update the GRUB file:
sudo nano /etc/default/grub
-
Locate the
GRUB_CMDLINE_LINUX_DEFAULT
variable in the file.Sample content:
GRUB_CMDLINE_LINUX_DEFAULT='nowatchdog nvme_load=YES loglevel=3'
-
Add the
zswap.enabled=0
parameter to the end of the value of theGRUB_CMDLINE_LINUX_DEFAULT
variable.Sample content:
GRUB_CMDLINE_LINUX_DEFAULT='nowatchdog nvme_load=YES loglevel=3 zswap.enabled=0'
-
Save the file.
-
-
Regenerate a new GRUB config so our changes to GRUB apply in the next boot.
-
Restart the system.
-
Verify our changes:
-
Verify Zram has been enabled:
inxi -j sudo dmesg | grep zram
Sample output:
Swap: ID-1: swap-1 type: zram size: 7.66 GiB used: 2.44 GiB (31.9%) dev: /dev/zram0
[ 3.471553] zram: Added device: zram0 [ 3.753380] systemd[1]: Created slice Slice /system/systemd-zram-setup. [ 3.760467] systemd[1]: Expecting device /dev/zram0... [ 4.857465] zram0: detected capacity change from 0 to 16072704 [ 5.057472] Adding 8036348k swap on /dev/zram0. Priority:60 extents:1 across:8036348k SSDsc
-
Verify Zswap has been disabled:
sudo dmesg | grep zswap
Sample output:
[ 0.000000] Command line: BOOT_IMAGE=/@/boot/vmlinuz-linux-lts root=UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg rw rootflags=subvol=@ nowatchdog nvme_load=YES loglevel=3 zswap.enabled=0 [ 0.046053] Kernel command line: BOOT_IMAGE=/@/boot/vmlinuz-linux-lts root=UUID=bzxd1bo8-9nnb-ddet-qykk-qjdgr9yytybg rw rootflags=subvol=@ nowatchdog nvme_load=YES loglevel=3 zswap.enabled=0
-