More Swap with Same Amount of RAM
Swapping with zRAM
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:
- /etc/modules-load.d/zram.conf. Add the line zram to the file.
- /etc/modprobe.d/zram.conf. Add the line options zram num_devices=1
- /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.
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.
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
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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.