More swap with same amount of RAM

Swapping with zRAM

Lead Image © vectora, 123RF.com

Lead Image © vectora, 123RF.com

Author(s):

ZRAM is a faster RAM disk than other swap sources, and it’s easier on SSD drives.

ZRAM is the latest version of a RAM disk: a virtual filesystem created using RAM. The idea is as old as personal computers, and in the early days, when there was a limit of 640k RAM, extra memory was often made into a RAM disk to improve performance. Originally called compcache, zRAM is a kernel module that creates a compressed device in RAM, usually for use as a swap device. This use is so efficient that, in the past few years, with little attention, zRAM has been enabled during installation in many distributions.

Swap, if you need to be reminded, is device memory that is used when all available RAM is used. On most machines, swap is allocated to a specially formatted partition, listed in /etc/fstab along with regular partitions, although it can also be a single file made by the root user with the command mkswap /swapfile. Multiple swap sources are possible, so if a swap partition proves too small, a swap file can be added in much less time than resizing the swap partition would take. You can choose which swap source to use first by adjusting priorities (see below).

Any sort of swap memory can keep your computer running when all the RAM is allocated, although these days, when home computers often have 8GB or more of RAM, you may only need it if you are doing intensive work such as graphic design, or if you regularly hibernate your machine. The only trouble is, device memory is glacially slow compared to RAM: .10 percent of RAM speed if it is on an SSD drive and .01 percent if it is on a mechanical hard drive. For this reason, RAM disks have always been popular on computers, although their use was impractical until about two decades ago, when the higher amounts of RAM became the norm. Today, zRAM's added compression means that more swap is available with the same amount of RAM.

Whatever the swap sources you are using, one of the standard debates in Linux is how much swap memory you needed. The standard answer is something like this: twice the RAM when 2-4GB of RAM are available, maybe 2GB with more than 4GB on a home machine, and 5GB or more on a server. The answer also depends on your computing. If your computing consists mostly of texting and web-browsing you may never actually use any swap, so allocating too large an amount of swap space of any kind will only waste your resources. Similarly, zRAM is most efficient on data files and less efficient on multimedia files, which are already compressed.

Whatever swap space you allocate, using zRAM before any other swap source significantly improves performance. Not only is a RAM disk like zRAM faster, but it reduces the wear and tear on SSD drives (which can reduce the useful life of cheaper SSDs, although top-end SSDs today are likely to outlive the rest of a computer). These days, many distributions such as Arch, Debian, Fedora, and Ubuntu set up zRAM automatically alongside swap partitions, although you may still want to edit the settings. Should zRAM not be installed, you can easily set it up.

Installing zRAM

If zRAM is not already set up on an installation, it may be in the distribution's repository. If not, you can install it from scratch from the development page. If your installation uses systemd, open a text editor as root and create the files:

  1. /etc/modules-load.d/zram.conf. Add the line zram to the file.
  2. /etc/modprobe.d/zram.conf. Add the line options zram num_devices=1
  3. /etc/udev/rules.d/99-zram.rules. Add the line KERNEL=="zram0", ATTR{disksize}="SIZE",TAG+="systemd"

Note that, because zRAM is controlled by systemd, no entry in /etc/fstab is needed.

If your installation uses a SysV-based init system rather than systemd, the Debian wiki gives a script that can be added to /etc/init.d/zram:

# Author: Antonio Galea <antonio.galea@gmail.com>
#
# Thanks to Przemysław Tomczyk for suggesting swapoff parallelization
# Distributed under the GPL version 3 or later, see terms at
# https://gnu.org/licenses/gpl-3.0.txt

### BEGIN INIT INFO
# Provides: zram
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: S
# Default-Stop: 0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description: Use compressed RAM as in-memory swap
### END INIT INFO

FRACTION=75
MEMORY=$(perl -ne '/^MemTotal:\s+(\d+)/ && print $1*1024' < /proc/meminfo)
CPUS=$(nproc)
SIZE=$((MEMORY * FRACTION / 100 / CPUS))

case "$1" in
start)
param=$(modinfo zram | grep num_devices | cut -f2 -d: | tr -d ' ')
modprobe zram $param=$CPUS

for n in $(seq $CPUS)
do
i=$((n - 1))
echo $SIZE > /sys/block/zram$i/disksize
mkswap /dev/zram$i
swapon /dev/zram$i --priority 10
done
;;
stop)
for n in $(seq $CPUS)
do
i=$((n - 1))
swapoff /dev/zram$i && echo "zram: disabled disk $n of $CPUS" &
done

wait
sleep .5
modprobe --remove zram
;;
*)
echo "Usage: $(basename $0) (start | stop)"
exit 1
;;
esac

# End of file

Make the script executable with chmod +x /etc/init.d/zram. Then run the following commands:

apt-get install insservinsserv zram

Managing zRAM

If you search for information about zRAM, you may come across references to zram-config, an obsolete utility developed for Ubuntu. In the unlikely event that you find zram-config to download, it should not be used. Instead, there are several other alternatives.

Running the command modinfo zram with root privileges, you can view basic information about a zRAM device (Figure 1). Then, ready the device to be edited with zramctl --reset (-r). You can configure the zRAM device using:

zramctl [-f | zramdev] [-s SIZE] [-t DEVICE-NUMBER] [-a COMPRESSION-ALGORITHM]

This command searches for the device designated with -f and adjusts its size, and sets the compression algorithm from the choice of lzo, lz4, lz4hc, deflate, 842, or zstd algorithms.

Figure 1: F1_mod-info.png: Basic information about zRAM.

Another useful package is zram-tools, which must be installed separately. It includes several self-explanatorily named scripts for managing zRAM devices: zram start, zram stop, and zramswap status. In addition, zram-tool creates the file /etc/default/zramswap, which can be revised in a text editor (Figure 2). In zramswap, size can be defined as a percentage of the total RAM, or as a specific size, but if both are defined, percentage defines the size.

Figure 2: F2_config-file.png: Installed with zram-tools, /etc/default/zramswap is a convenient configuration file for zram.

Other tools and methodologies are also available in some distributions, including Arch, Fedora, and openSUSE. Other solutions, such as zram-generator are also being developed. The variety of approaches reflects how relatively recently zRAM has been installed by default.

Giving zRAM Priority

However you enable zRAM, you can check that it is working if it is listed when you enter the command cat /proc/ swaps (Figure 3). If other swap sources are available, you can give /dev/zram0 the highest priority, so that it is used first. To change the priority of non-zram partitions or files, run as root:

chrt --rr <priority between 1-99> DEVICE
Figure 3: F3_multi-swaps.png: Run cat /proc/ swaps for a list of all swap devices. Note how the zRAM device is named.

Alternatively, if the zRAM device is large enough – at least 2GB – you may decide not to use other swap devices at all. You can delete swap files and comment out swap partitions in /etc/fstab, or delete a swap partition altogether using a partition manager such as parted and re-assigning its memory to ordinary partitions. However, do not be in a hurry to make changes. Experiment first to find what your swap needs are.

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Memory Compression

    Data compression costs virtually no computing power today. Why not save some space by putting data compression techniques to work on RAM and cache memory?

  • Ask Klaus!

    Klaus Knopper is the creator of Knoppix and co-founder of LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

  • Performance Tweaks

    If you are looking for ways to speed up your Linux, consider this collection of curated performance tweaks.

  • Ask Klaus!

    Klaus Knopper is the creator of Knoppix and co-founder of LinuxTag expo. He currently works as a teacher, programmer, and consultant. If you have a configuration problem, or if you just want to learn more about how Linux works, send your questions to: klaus@linux-magazine.com

  • Unmasking Fake Flash Memory

    When it comes to cheap flash memory, buyers should beware. Fake flash memory often offers only a fraction of the advertised storage capacity. With no visible calibration mark, it isn't easy to discover a counterfeit. Here's a test to weed out fake disks.

comments powered by Disqus

Direct Download

Read full article as PDF:

News