Zram

zram (previously called compcache) is Article description::a Linux kernel feature and userspace tools for creating compressible RAM-based block devices. It has been included as a module of the mainline Linux since kernel version 3.14. Starting with kernel version 3.15, zram supports multiple compression streams and the ability to change the compression algorithms in use.

Introduction
The zram kernel module enables support for creating compressed block devices in RAM. These block devices can then be used for swap or general purpose RAM disks. The two most popular uses for it are swap to extend the available amount of RAM to processes and.

The RAM used for the block device is dynamically obtained and released up to its predefined uncompressed maximum size. The way it extends the amount of available RAM to a system is by using a portion of the RAM as compressed swap. It can therefore hold more pages of memory in the compressed swap than the amount of actual memory used.

Typically it compresses to a 3:1 ratio. So, 1 GiB of swap uses only 333 MiB of RAM on average. The compression ratio including memory used for disk overhead varies depending on the percent of maximum space used. This may typically vary from 1.5:1 for a 1.5 GiB disk with only 5% space used, to over 3:1 when nearly full. It also is much faster at swapping pages than typical hard disk swap.

My experience with using it, my system is still fully functional, with only slight slow downs at times. This is for a Xfce4 desktop with several apps and emerge running with. The memory and swap spaces were nearly maxed out. Intel Core2 Quad core 2.6Ghz, 4 GiB RAM. I had 4 - 1.5 GiB zram disks for swap, plus 1 GiB partition of hard drive swap as backup.

At one point during linking chromium, I saw the system using just over 5 GiB of zram swap, while using about 1.2 GiB of RAM, about 100 MiB of hard disk swap. The desktop was still responsive.

Caveats/Cons
Prior to kernel 3.15, each zram device contains its own compression buffer, memory pools, and other metadata as well as per-device locks. This can become a serious bottleneck for multi-core machines. To work around this problem, zram is capable of initializing multiple devices. For this reason, the recommended amount of devices for swap is 1 per CPU core for kernels prior to 3.15.

Another caveat for systems with limited memory, non swap use can reduce the amount of available memory to run applications.

When using this with a really fast SSD (e.g. Samsung 840 Pro), avoid setting  in. Depending on the size of the zram partitions and the speed of the RAM, some swap partitions and filesystems might not be ready when the and  services are started.

In such case, if you absolutely have to use parallel, consider removing these services from runlevel and adding them to  instead.

Enabling zram support in kernel
Enable the following options are needed in the kernel's configuration file:

It is recommended that zram be built as a loadable module. This allows you to change number of zram devices without reboot, by deactivating zram devices and re-loading module with new parameters. If you have zram built-in, then you can only change the number of devices at boot time by using the kernel boot parameter: zram.num_devices=#

In order to use the LZ4 compression algorithm, you must also enable it in kernel config:

Using zram-init service
By far the easiest method of utilizing zram disk(s) is by using Martin Väth's zram-init script.

Note, that version 2.7 is fully compatible with kernels < 3.15. If version >= 3.0 is used, the maxs (maximum concurrent streams) and algo (compression algorithm selection) is only functional for kernels >= 3.15.

OpenRC
Edit the file and create/configure the desired zram devices. There are lots of comments/instructions in the file. So proceed with editing and be sure to save it when the appropaite modificaitons have been made.

An example:


 * Specs: Dual core CPU, 2GiB total RAM.
 * Configure 1G of two-stream swap, 512MiB of

Then, add the init script to the desired runlevel, usually, and start the service:

In this case the boot runlevel is preferable to the default runlevel becaues zram is providing temporary storage filesystems at or  which are prerequisites for other services which will start during the default runlevel.

systemd
from the mv overlay provides systemd units with self explained names:



These should be copied to directory in order to be edited, then enabled

For example, to enable the directory (which includes Portage's temporary directory used for compiling packages) in zram:

Note, the file size should be adjusted to a size large enough to hold the working directories for the packages that will be compiling. Consider adjusting the default value from  to at least   (16GiB). See this table for estimates of uncompressed space necessary for successful compilation.

Using OpenRC
For manual creation create two files:  and. OpenRC will run these during the appropriate as part of service run process when booting or changing run levels.

An example:


 * Specs: 4 cpu cores, 4G RAM total
 * Configure 6G zram swap and activate.
 * Estimated maximum ram used 2G @ 3:1 compression ratio.

Using udev
Other possibility is to use existing configuration files - this option works on vanilla Gentoo without need to install additional software, also useful if you are using systemd instead of OpenRC. The first example can be implemented using:

Additionally, the utility is part of  and can be used to configure zram devices. See for examples of usage.

systemd
If using systemd with this method, you must ensure that the zram module is loaded by systemd. The simplest way to achieve that is to include a file in like this one:

Checking that zram is used
Check if zram is mounted as swap:

Check if zram is mounted as directories:

External resources

 * zram in official kernel documentation.