Deflated
Deflated
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?
Random access memory (RAM) is used to store data for ongoing operations. RAM has an ultra-short access time, but it is comparatively expensive to buy. Moreover, you cannot extend RAM infinitely, because the board can only take a certain size and only offers a fixed number of slots for it. Additionally, memory modules for older devices are sometimes difficult to obtain.
Instead of adding more RAM to the computer, another approach is to make more data fit in the RAM you already have. Current computers are fast enough that on-the-fly data compression hardly makes a difference to the total execution time. Data compression started as a technology for archiving data on hard disks, but you can also use compression for data in RAM memory.
The three methods, zram, zswap, and zcache, provide three implementations that extend content compression to RAM. These features made it into the main kernel branch [1] in the 3.14 kernel, but haven't received much everyday use. Only Android 4.4 and Knoppix 7 or higher activate zram by default.
Inventory
Before using compression technologies, you should know how much memory your system actually has. You don't have to open the hood and physically check your computer – software tools can reliably detect and display the values. At the command line, dmidecode
[2] does the trick; in a graphical environment you can use HardInfo and lshw-gtk
.
dmidecode
relies on the Desktop Management Interface (DMI) [3] to discover information about the built-in system components like the CPU, the motherboard, and the memory. To retrieve only the RAM information, call the program with the --type memory
parameter. Listing 1 shows the output, which comes from a computer equipped with a single 8GB DDR3 RAM module that runs at 1,600MHz. The overview shows that the maximum capacity on the board is 16GB – another 8GB module would be possible.
Listing 1
Retrieving RAM Information
$ sudo dmidecode --type memory # dmidecode 2.12 SMBIOS 2.7 present. Handle 0x0007, DMI type 16, 23 bytes Physical Memory Array Location: System Board Or Motherboard Use: System Memory Error Correction Type: None Maximum Capacity: 16 GB Error Information Handle: Not Provided Number Of Devices: 1 Handle 0x0008, DMI type 17, 34 bytes Memory Device Array Handle: 0x0007 Error Information Handle: Not Provided Total Width: 64 bits Data Width: 64 bits Size: 8192 MB Form Factor: SODIMM Set: None Locator: ChannelA-DIMM0 Bank Locator: BANK 0 Type: DDR3 Type Detail: Synchronous Speed: 1600 MHz Manufacturer: Samsung Serial Number: 25252105 Asset Tag None Part Number: M471B1G73DB0-YK0 Rank: Unknown Configured Clock Speed: 1600 MHz
Figure 1 shows the output from Hardware Lister (lshw-gtk
). The data comes from a virtual machine running Debian 9, which only has about 2GB of RAM.
Two programs, free
and htop
, help you find out how much RAM the running system is currently using. Figure 2 shows free
in action. The call was made with the -h
parameter (long form --human
), which makes the output easier for people to read. In this case, a total of 2GB of RAM is available, of which the running processes use 356MB.
RAM is a valuable commodity. The more working memory you have, the more overhead providing the matching number of memory pages (memory units provided by the kernel for processes) for the individual applications is involved. Memory pages that have not been used for a long time are stored in swap space (see the "Swap" box), which is usually on (slower) mass storage. This is exactly where the three methods mentioned earlier enter.
Swap
The term "swap" stands for the external storage or outsourcing of the main memory's contents. Swap [6] only works in conjunction with a swap partition or a swap device [7]. As soon as the RAM is full, the system removes the least recently used memory pages. This is based on the assumption that the data will not be needed again so quickly. The data is stored in the swap space without compression. If these data are accessed, it takes much longer, because the system first has to reload the data from the swap partition instead of being able to access the computer's working memory directly. However, if the system works without swap and the memory fills up, the kernel terminates programs at its own discretion to free up RAM again. This usually results in unwanted data loss.
The zram, zswap, and zcache methods help you use available memory cells more effectively. Zram (formerly known as compcache [4]) creates a compressed partition as a block device in RAM that can be used as a virtual swap space or as a RAM disk. Zswap [5] employs a compressed buffer that can be used as a write-back cache for physical swap space. This delays or prevents access to the mass storage, reducing the I/O load. Zcache serves as a back end for a specific virtual RAM type that can be used to buffer filesystem contents or swap data.
In Detail
Zram [8] lets you move compressed data directly in RAM. If no more memory pages are available, the kernel tries to keep the data on this block device. This procedure doubles or triples the corresponding storage capacity.
There is currently no package available for Debian, so you will need to set up such a virtual swap device manually (Listing 2). First load the zram
kernel module (line 1). The num_devices=2
statement ensures that two devices, /dev/zram0
and /dev/zram1
, are created. The first serves as an additional swap partition in the following example, the second simply as a compressed directory in RAM.
Listing 2
A Virtual Swap Device
01 $ sudo modprobe zram num_devices=2 02 $ sudo zramctl --find --size 512M 03 /dev/zram0 04 $ sudo mkswap /dev/zram0 05 Setting up swapspace version 1, size = 512 MiB (536866816 bytes) 06 no label, UUID=009222e1-d138-41aa-8656-64a2c3655004 07 $ sudo swapon /dev/zram0 08 $ sudo swapoff /dev/zram0 09 $ sudo zramctl --reset /dev/zram0
Line 2 shows how to find the next available zram device and assign it a size of 512MB. The zramctl
command from the util-linux package is used here. Use the mkswap
command to create the device as a swap partition (line 4); this assigns a unique identifier in the form of a UUID (line 6). Calling swapon /dev/zram0
finally mounts the device as additional swap space (line 7).
The approach for integrating /dev/zram1
as a compressed directory in memory is similar [9]. First use zramctl
to determine the next device; then create a file system on it. The ext2 type is fine here, since you do not need journaling RAM. Then, assign the new device to a mountpoint. Figure 3 shows the procedure and, in particular, the output from zramctl
. The columns in the output include the device names, the compression algorithm used (lzo), the device's disk space, the data (uncompressed, compressed, and total), the number of compression streams, and the respective mountpoints.
Figure 4 shows the call to swapon -s
. The -s
switch (the short form of --summary
) displays an overview of all currently mounted swap partitions – in this case, /dev/sda3
and /dev/zram0
. The output contains five columns: the name of the file or device, the type, the size, the bytes in use, and the priority. The higher the priority, the more important the partition is when swapping out.
To remove the /dev/zram0
swap partition and clean it up in memory, use the commands from the last two lines of Listing 2.
Ubuntu users have a package named zram-config. After installing this with the package manager, set up the zram device on the fly. To do so, use systemctrl
to start the zram-config
service (first line of Listing 3).
The new /dev/zram0
device is then immediately available as an additional swap partition, as shown by the swapon -s
command (Figure 5). The software automatically allocates half of the physical memory to the swap device. Currently, no other value can be set when calling the service or as a configuration argument. If you want to remove the swap device in RAM, the call from the last line of Listing 3 is all it takes.
Listing 3
Installing zram-config
$ sudo systemctrl start zram-config $ sudo systemctrl stop zram-config
Swap Becomes zswap
Whereas you have additional swap space on a zram device to provide more space for outsourcing, zswap takes a different approach: This kernel function creates a compressed cache and is useful for all installations that already have a swap area.
Instead of moving memory pages to a corresponding device, zswap compresses the data first; then, it stores the data in a dynamically allocated memory pool. The advantage of this method is that the system thus postpones or even completely avoids writing back to the swap device. Writing and reading in the cache is far faster than from a swap device on a data carrier.
If you have a recent kernel, this function will be included and can be enabled as a module. Figure 6 shows this for Debian kernel v4.9.x. If necessary, just stipulate zswap.enabled=1
in the GRUB configuration to enable the feature.
Buy this article as PDF
(incl. VAT)
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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.