Basics of Design CC Blog Research & Design Hub

zram in Embedded Linux

Written by Pedro Bertoleti

How to Maximize RAM in a Linux Environment

In this piece, I discuss how to use zram for memory swap, in order to create a drive/disk that is mounted in RAM memory itself, using on-the-fly data compression and decompression. This expands the real storage capacity in RAM and significantly increases write and read speed in swap operations. As a practical example, I’ll also show how to use zram in a Raspberry Pi single-board computer (SBC).

  • How can I maximize RAM in a Linux environment?
  • What does zram do?
  • How does zram work?
  • What are the advantages of zram?
  • How can I use zram in a Raspberry Pi?
  • Raspberry Pi 

Embedded Linux is popular for its flexibility, wide array of supported hardware, and robustness. But, as happens for applications for personal computers, the memory footprints of applications supported by embedded Linux tend to grow over time—and as a consequence they demand more RAM memory to work properly. This leads to more memory swap operations, a must-have solution to keep memory management healthy in embedded Linux.

However, memory swap has a huge cost for embedded Linux: it decreases the overall performance of the operating system (OS). Traditional memory swap operations use a certain portion of a mass storage memory (flash memory, SSD, HD, and so forth) to store the less frequently used data. Considering mass storage memory speed (read and write speed) is much slower than RAM memory data access speed, every single memory swap operation suffers from the slowness in writing and accessing data from mass storage memory, which results in overall embedded Linux latency.

This article covers how to use zram for memory swap, so as to create a drive/disk mounted in RAM memory itself, using on-the-fly data compression and decompression. This expands the real storage capacity in RAM and significantly increases write and read speed in swap operations. As a practical example, I’ll show how to use zram in a Raspberry Pi single-board computer (SBC).


In brief, zram is a Linux OS feature with which you can create a block device mounted into RAM memory, with on-the-fly compression (so data can be written into zram in compressed form) and decompression (so data is automatically decompressed in zram read operation). Data is automatically compressed before being stored in zram, and data from zram is automatically decompressed before being read, too. This compression-decompression feature gives zram the ability to more data than its real capacity, as all data is stored in a compressed form.

Since zram is like a disk or storage drive mounted into RAM memory, it takes advantage of high-speed read and write operations in RAM. Thus, zram also offers a significantly higher read and write speed than traditional mass storage memory (especially old hard drives).

See Figure 1 for an interesting comparison, the source for which can be found on Circuit Cellar’s Article Materials and Resources webpage [1]. As you can see, a RAM disk (or a multipurpose disk mounted into RAM, such as zram) can be more than 10 times faster than a solid-state drive (SSD), and more than 47 times faster than a hard-disk drive (HDD).

A speed comparison between a hard-disk drive, solid-state drive, and RAM disk drive.
A speed comparison between a hard-disk drive, solid-state drive, and RAM disk drive.

zram is, definitionally, a resource that allows one to create a compressed device block into RAM memory. But its enhanced data capacity and data read/write speed make it the perfect choice for memory in swap operations. In fact, using it as such has many advantages. Let’s examine some of these now.

Faster read/write of data: Even with the time spent doing on-the-fly data compression and decompression, zram is still a significantly faster alternative to mass memory storage. Using zram as memory for swap significantly decreases the general latency seen in embedded Linux when it’s in memory swap operation, since the I/O wait—the time the CPU spends waiting for a response from an I/O device, such as a block device—is much shorter in zram thanks to the RAM memory speed in data access. Hence, embedded Linux performance tends to be significantly enhanced, providing a smoother experience to the user. In extreme cases, zram can even prevent the memory thrashing effect, wherein excessive memory swapping drastically reduces system performance.

Avoids wearing out flash memory: As discussed, traditional memory swap uses mass storage memory. Nowadays, embedded Linux systems usually have flash memory for mass storage, and this can quickly wear out with excessive memory swap operations. Flash memory becomes unusable, and embedded Linux won’t work. With zram for memory swapping, the flash memory isn’t used and is thus preserved—at least, it won’t wear out as quickly.

More flexibility: The way zram compresses and decompresses data is governed by a “compression algorithm.” There are multiple types of compression algorithms available, enabling developers to test several and choose the best fit for a particular solution. In many cases, the right compression algorithm can mean better overall embedded Linux performance.

Increased total swap memory capacity: Some compression algorithms offer compression rates greater than three—meaning zram can actually store more than three times its real data capacity. This feature is especially useful in solutions that use a lot of RAM memory, like rich user interfaces with animations and high-resolution images.


To use zram in embedded Linux, some prerequisites must be met. These relate to Linux kernel configurations that enable zram usage, and that indicate which compression algorithms must be integrated into the kernel in its compilation. Then, enabling zram and its compression algorithms involve Linux kernel compilation.

If you’re not familiar with the Linux kernel compilation process, Raspberry Pi’s website has excellent documentation detailing the full process for Pi boards [2]. Assuming you are familiar, the following Linux kernel configuration settings enable zram to work in an embedded Linux OS:

  • CONFIG_zram: Must be set to y.
  • CONFIG_zram_LZ4_COMPRESS: For a compression algorithm to be available in embedded Linux, kernel configs must inform it which compression algorithm to integrate into the kernel when compiling. For example, to make the LZ4 compression algorithm available, CONFIG_zram_LZ4_COMPRESS must be set to y. Consult Linux kernel zram-related documentation to see other available compression algorithm options (and their corresponding kernel configs).

As with everything, there isn’t a definitive, costless, and miraculous solution for an embedded system problem. There’s always a trade-off. A solution that improves some aspects of a system can simultaneously worsen others. And of course, zram is no different.

Specifically, zram demands more processing from CPUs. Its on-the-fly compression and decompression tasks require extra processing compared to traditional swap processes (those using mass storage memory for swap). It’s a good practice to check CPU usage when using zram, to prevent undesired side effects. I strongly recommend you:

  • Check the overall impact on CPU usage: Can the CPU still smoothly run everything? Is overall performance significantly worse?
  • Check the CPU’s temperature: More CPU usage means a higher CPU temperature. You’ll need efficient heat dissipation when using zram. CPU overheating is a big problem, and, needless to say, should be avoided in all real-world implementations of your solution.

So how do you use zram in embedded Linux? Due to its popularity and ease-of-use, I’ll use a Raspberry Pi SBC in my example of using, testing, and validating zram. The processes described here will work on any Raspberry Pi except the Pico, as it doesn’t support embedded Linux. You’ll need:

  • One Raspberry Pi board, other than the Pico
  • One micro-SD card to flash the embedded Linux image
  • One power source to supply the Raspberry Pi (a 5V/2A micro-USB power source, for example)

In addition:

  • The micro-SD card must have the most recent Raspberry Pi OS flashed. I recommend the Raspberry Pi imager software, available on their site [3]. It’s intuitive and perfect for this job.
  • You need access to a terminal in Raspberry Pi. This can be done either via a local network using SSH, or using a physical keyboard, mouse, and monitor connected to the Pi.
  • Your Raspberry Pi needs Internet access to be able to update its packages and to download the required software for zram.

Assuming you’ve already compiled the Linux kernel with the discussed configurations (the Raspberry Pi OS kernel is already okay), the only thing left is to set up zram for memory swapping.

There’s an interesting open-source project available on GitHub that helps in this regard [4]. The appropriately named “zram-swap” contains useful scripts to prepare a Pi board to use zram as memory for swaps without requiring the user to be familiar with memory swap setup commands in Linux. I find it’s a practical solution for those who want to quickly verify zram is working for swaps on their Pi. To use zram-swap:

  • Upgrade all currently installed packages by executing the following commands:

sudo apt-get update

sudo apt-get upgrade -y

This can take several minutes to complete, depending on your Internet connection speed and how many packages will need to be updated.

  • Install git by using the command: sudo apt-get install git
  • Reboot Raspberry Pi using the sudo reboot command.
  • After Raspberry Pi reboots, go to the home directory and clone the project source-code by using the following commands:

cd ~

git clone

  • Enter the project’s directory and execute the install script by using the following commands:

cd zram-swap

sudo ./

After finishing these steps, zram should be working on your Raspberry Pi board as memory for memory swap operations.


By default, the zram-swap project from GitHub configures zram to use 50% of the total Raspberry Pi’s RAM size, and configures LZ4 as the compression algorithm. If you want to change these configurations, you can do so by accessing the /etc/default/zram-swap file. This contains all configurations for this project, should you ever need to tweak any of them.

It’s good practice to test some different zram sizes and compression algorithms when developing, to fine-tune the performance of an embedded Linux distro. And you’ll end up with a clearer picture of how a given zram size and compression algorithm influences the performance of a particular embedded Linux solution.


One useful way to check if zram is working as memory for swap operations in your Pi is to examine the content of the /proc/swaps file by using the cat /proc/swaps command. This file contains information on swaps on Linux, including type, priorities, and sizes.

You can also use the free tool to verify the memory swap usage on the system. Figure 2 shows an example of the output of the free tool. Note that you can check several memory stats on embedded Linux, including the swap usage.

Free tool output
Free tool output

Hopefully now you know a little more about the main advantages of zram over traditional mass storage memory. It can be significantly more efficient, as well as much faster since it uses RAM memory. Further, I hope you have sense of how and why to use zram for swapping in embedded Linux. I think it’s an excellent way to the overall performance of embedded Linux and prevent flash memory wear-out. zram is great for embedded Linux solutions that require a lot of RAM. Consider it for your next application—I’d love to hear how it turns out. 

[1] Gecko & Fly—12 Free RAMDisk vs SSD:
[2] Linux kernel compilation for Raspberry Pi:
[3] Raspberry Pi imager software:
[4] Open-source zram Raspberry Pi project on Github:



Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.

Note: We’ve made the Dec 2022 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article

Supporting Companies

Upcoming Events

Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2024 KCK Media Corp.

zram in Embedded Linux

by Pedro Bertoleti time to read: 8 min