Enable zram
Introduction
From the Archwiki
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 a general-purpose RAM disk. The two most common uses for zram are for the storage of temporary files (/tmp) and as a swap device. Initially, zram had only the former function, hence the original name "compcache" ("compressed cache").
More Information
Initially the created zram block device does not reserve or use any RAM. Only as files need or want to be swapped out, they will be compressed and moved into the zram block device. The zram block device will then dynamically grow or shrink as required.
For example, consider an example system with 32 GiB RAM where zram is configured with 64 GiB capacity. Assuming that zstd employs a 1:4 compression ratio, the actual compressed zram block size on physical RAM when fully occupied will be around 16 GiB. Therefore:
RAM and zram completely occupied: 16 GiB RAM + 64 GiB zram (~16 GiB in RAM) Normal usage without swapping: 32 GiB RAM + 0 GiB zram Normal usage with light swapping: 30 GiB RAM + 8 GiB zram (~2 GiB in RAM) Without any zram configuration: 32 GiB RAM
Therefore, zram always offers the advantage of being able to store more content in RAM. Note:
If the related zswap kernel feature remains enabled, it will prevent zram from being used effectively. This is because zswap functions as a swap cache in front of zram, intercepting and compressing evicted memory pages before they can reach zram. Despite the output of zramctl(8), most of zswap is unused in this circumstance. As discussed above, when configuring zram, the size of the zram device controls the maximum uncompressed amount of data it can store, NOT the maximum compressed size. You can configure the zram's size to be equal to or even greater than your system's physical RAM capacity, as long as the compressed size on physical RAM will not exceed your system's physical RAM capacity. Hibernating to swap on zram is not supported, even when zram is configured with a backing device on permanent storage. logind will protect against trying to hibernate to a swap space on zram.
zram-generator provides a
systemd-zram-setup@.service
unit to automatically initialize zram devices without users needing to enable/start the template or its instances.
ℹ️ Note that zram is not compatible with hibernation unless combined with a swap device. ℹ️
If you are here just to have a quick install method you may skip to this section.
Install zram-generator
Execute:
sudo pacman -Syu zram-generator
Configure zram-generator
Create file /etc/systemd/zram-generator.conf
with contents:
[zram0]
zram-size = ram * 2
compression-algorithm = zstd
swap-priority = 100
fs-type = swap
Instructions on how to create the file
Use either of the following methods.
Create/Edit main configuration file /etc/systemd/zram-generator.conf
by hand
To create or edit the /etc/systemd/zram-generator.conf
file you will need elevated privileges.
Execute:
sudoedit /etc/systemd/zram-generator.conf
If you have not set SUDO_EDITOR
then you can define the variable with your favorite editor.
Execute:
SUDO_EDITOR=micro sudoedit /etc/systemd/zram-generator.conf
OR
Run this one-line command
Execute:
printf '[zram0]\nzram-size = ram * 2\ncompression-algorithm = zstd\nswap-priority = 100\nfs-type = swap\n' | sudo tee /etc/systemd/zram-generator.conf
zram-generator
should now create a zram swap device double the size of available physical memory.
Extra Notes
-
Notice the arithmetic in
zram-generator.conf
forzram-size
. You can likewise use/2
to create zram half the size of physical memory. Or*1
for equal the amount of physical memory. -
Due to compression multiples of RAM should be safe even higher than
*2
or 2 times the size of physical RAM.*2
may be considered a conservative maximum.
Disable zswap
zswap
is a similar tool that compresses swap cache. It is incompatible with zram
.
Add the option zswap.enabled=0
to your kernel parameters.
Instructions on how to add the boot option
Use either of the following methods.
Edit main configuration file /etc/default/grub
To edit the CMDLINE
lines of /etc/default/grub
you will need elevated privileges.
Execute:
sudoedit /etc/default/grub
If you have not set SUDO_EDITOR
then you can define the variable with your favorite editor.
Execute:
SUDO_EDITOR=/usr/bin/micro sudoedit /etc/default/grub
Assuming no other options then it would appear as:
GRUB_CMDLINE_LINUX="zswap.enabled=0"
Save and close the file.
OR
Create drop-in configuration file /etc/default/grub.d/disable-zswap.cfg
Execute:
sudo mkdir -p /etc/default/grub.d && printf 'GRUB_CMDLINE_LINUX+=" zswap.enabled=0"' | sudo tee /etc/default/grub.d/disable-zswap.cfg
Execute:
sudo update-grub
OR:
sudo grub-mkconfig -o /boot/grub/grub.cfg
Start zram Devices (Optional)
Devices should be automatically enabled on reboot.
Instructions on how to manually start the services
Alternatively, to start them right away check for configuration changes:
systemctl daemon-reload
Then start your configured systemd-zram-setup@zramN.service
instance(s).
systemctl start systemd-zram-setup@zram0
Reboot & Check
After installing, configuring zram-generator
and disabling zswap
:
REBOOT
After reboot you may check the status of swap.
Instructions on how to view zram/swap usage
Execute:
swapon
AND/OR
Excute:
zramctl
Quick Install Instructions
If you are absolutely sure you want zram, want to disable zswap, use grub, and that I know whats best, then you can just enter these commands.
Click to show
Simply execute the following:
sudo pacman -Syu zram-generator
printf '[zram0]\nzram-size = ram * 2\ncompression-algorithm = zstd\nswap-priority = 100\nfs-type = swap\n' | sudo tee /etc/systemd/zram-generator.conf
sudo mkdir -p /etc/default/grub.d && printf 'GRUB_CMDLINE_LINUX+=" zswap.enabled=0"' | sudo tee /etc/default/grub.d/disable-zswap.cfg
sudo grub-mkconfig -o /boot/grub/grub.cfg
After a reboot you should have zram up to 2x, double, your physical memory.
More Information
Kernel Documentation
https://docs.kernel.org/admin-guide/blockdev/zram.html
Online Manual for zram-generator
https://man.archlinux.org/man/extra/zram-generator/zram-generator.8
Online Manual for zram-generator.conf
https://man.archlinux.org/man/zram-generator.conf.5
Online Manual for zramctl
https://man.archlinux.org/man/core/util-linux/zramctl.8
ArchWiki zram Page
https://wiki.archlinux.org/title/Zram