Deflated

Deflated

Article from Issue 215/2018
Author(s): , Author(s):

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.

Figure 1: Hardware Lister provides information about a PC's components, including the specifics of the main memory.

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.

Figure 2: The free command-line tool helps you determine how much RAM a system has and how much of it is used by running processes.

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 3: With just a few commands, you create a zram device, which you then format and mount in any directory.

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.

Figure 4: The swapon -s command shows you which swap partitions are mounted on the system.

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
Figure 5: Under Ubuntu, the zram-config package automatically provides additional swap space.

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.

Figure 6: You can use zswap to compress data in the kernel first, instead of storing the data directly on a swap device.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

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

  • Swapping with zRAM

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

  • 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

  • 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
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.

Learn More

News