← All posts

Ubuntu. Installing and Configuring zRAM

Diagram showing zRAM swap stored in RAM

Today, I’ll leave a short note here about zRAM. This module lets you get rid of slow disk-based SWAP. On an SSD, for example, using SWAP is undesirable because it generates a large number of disk writes. However, zRAM does not store SWAP on the disk itself: it stores it in RAM and compresses the data by up to a factor of three. That’s the benefit!

Here’s what Wikipedia says about this module:

zRam is a Linux kernel module (formerly known as compcache). It improves performance by preventing pages from being swapped to disk, using a compressed block device in RAM until it becomes necessary to use a swap file on the hard disk. Accessing RAM is faster than accessing a hard disk, so zRam allows Linux to perform more swapping operations, especially on older computers with little RAM. Although RAM is now relatively inexpensive, zRam still offers benefits on laptops, low-powered computers, in virtualization, and in embedded systems that use flash memory, which has a limited number of write cycles.

I strongly recommend using zRAM instead of disk-based SWAP. Its benefits are especially noticeable on low-powered netbooks or virtual machines such as those on DigitalOcean or Amazon EC2.

Installing zram-config

Try this first:

sudo apt-get install -y zram-config

If it doesn’t complain, you already have SWAP in the form of zRAM. If it does complain, you’ll need to install “linux-image-generic” and reboot, then try installing “zram-config” again:

sudo apt-get install -y linux-image-generic
reboot
...
sudo apt-get install -y zram-config

Newer versions of the package create a service file for systemd, so you’ll need to start the service manually: systemctl start zram-config After a reboot, everything will start automatically.

zram-config will count the CPU cores and set up the same number of devices. You can check like this:

sudo swapon -s

Manual Installation

If you don’t like the size of the SWAP partitions created by zram-config, you can create the ones you need yourself:

/sbin/modprobe zram num_devices=1
echo 1073741824 > /sys/block/zram0/disksize
/sbin/mkswap /dev/zram0
/sbin/swapon /dev/zram0 -p 10

Where:

  • num_devices - the number of swap partitions; you should usually set this to the number of CPU cores;
  • 1040646144 - the size of the swap partition in bytes;
  • /dev/zram0 - the path to the device; there can be many such devices, depending on what you specified in num_devices;
  • swapon ... -p 10 - the priority; the higher it is, the sooner the specified partition will be used.

After that, check your swap partitions:

sudo swapon -s

That’s really all there is to it. Breathe new life into your low-powered Linux computers!

Comments are closed. Read the original discussion in Russian →