- zram creates compressed block devices in RAM that act as swap or ultra-fast disks, reducing disk accesses and improving performance.
- In Debian, Ubuntu and derivatives it is easy to enable zram with zram-tools, adjusting the compression algorithm and size according to the installed RAM.
- Tools like zram-config or zram-generator automate the creation of multiple zram devices and their integration with systemd.
- Choosing between percentage or fixed size, and prioritizing zram over disk swap, allows for a balanced and efficient configuration.

If you use GNU/Linux daily and are concerned about getting the most out of your computer's performanceSooner or later, you're going to encounter the concept of zRAM. Many users with limited RAM experience stuttering, occasional freezes, or slow performance when opening several resource-intensive applications simultaneously. Traditionally, the solution involved using a swap partition on the hard drive, but nowadays there are much faster and more efficient alternatives.
zram is one of those little gems offered by the Linux kernelIt allows you to create compressed block devices directly in RAM for use as an ultra-fast swap area or even as temporary disks. When properly configured, it can make a huge difference on both modest systems (4-8 GB of RAM) and more powerful machines with 16 or 32 GB, reducing disk accesses, preventing crashes, and improving overall system performance.
What is ZRAM and why does it improve performance compared to a classic swap?
zram is a Linux kernel module that creates compressed block devices in RAMIn other words, it creates a kind of "virtual disk" in RAM on which a swap space or even a file system can be created. Data written to this device is compressed on the fly using the CPU and stored in memory, so it takes up less space than it would without compression.
The key idea is to take advantage of the fact that RAM is much faster than any HDD or SSD.When the system starts running out of physical memory, instead of dumping memory pages to a slow disk, it compresses them into zRAM. Although some CPU usage is lost in compression and decompression, the gains in access speed are more than compensated for, and disk wear is reduced, which is especially important for SSDs.
This mechanism was originally known as compcacheOver time, it evolved until it was integrated into the kernel under its current name, zram. Today, it is considered a very serious alternative (and in many distributions, the preferred option) to the traditional swap partition that we have seen in Linux for decades.
One very clear practical advantage is the improvement in performance in "extreme" situations.For example, it has been tested on laptops running Fedora that, by switching from disk swap to swap over zRAM, certain demanding games running with Proton (such as Doom 2016) went from running at a mere 6-7 fps to around 20 fps on the same hardware, simply by changing the swapping strategy. It's not magic; it's simply that the swap is no longer on a relatively slow disk but is instead in compressed RAM.
This approach also has benefits on modern computers with plenty of memory.Although you might think that with 16 or 32 GB of RAM you don't need swap, the truth is that having a compressed buffer in zram can help maintain fluidity when you open browsers with dozens of tabs, virtual machines, heavy editors, or games, preventing the system from starting to kill processes or stutter.
Where is zram used today and which distributions already include it or make it easy to use
Many distributions have made a strong commitment to zram as a replacement or complement to the classic swapOne of the best-known examples is Fedora, which decided to prioritize swap over zRAM by default instead of relying exclusively on a swap partition on disk. This change has significantly improved the experience on laptops and desktops with limited memory.
In the Debian and Ubuntu world, the approach has been changing over the yearsDebian 10 (Buster) and Debian 11 (Bullseye), as well as Ubuntu 20.04 LTS and later, allow you to easily configure zram using specific packages (such as zram-tools or zram-generator). Furthermore, Ubuntu typically uses an on-disk swapfile instead of a dedicated partition, which greatly simplifies disabling that swap space if you decide to switch to zram.
Other distributions have even incorporated zram into their installersArch Linux, for example, offers the option to enable zram directly from its guided installer, archinstall. Simply select the corresponding option to configure the system with swap over zram using fairly reasonable default parameters, something some users even use as a reference for fine-tuning their own systems.
Beyond the desktop, zram is also used in mobile devices and embedded systemsAndroid and Ubuntu Touch take advantage of this to gain some breathing room on smartphones with limited memory: RAM is a very valuable resource, and the ability to compress parts of its contents allows for running more demanding applications than the raw physical memory would otherwise allow. Some lightweight desktop distributions also include it by default.
On systems with ample RAM, such as desktop PCs or mini PCs with 16-32 GBIt's becoming increasingly common not to create a swap partition during installation and to use only zram as the compressed swap mechanism. This keeps the system running smoothly without needing to reserve disk space for swap, which, in many cases, is rarely used.
Basic concepts: swap, block devices, and virtual disks in RAM
To fully understand what zram is doing, it's helpful to review two concepts: swap and block devices.In GNU/Linux, "almost everything is a file," and hardware devices are no exception: they are represented as special files within /dev. A hard drive, an SSD, an SD card, or a USB flash drive appear as block devices that the system accesses with the same interface, even though they use different underlying technologies.
A block device can contain a traditional file system, such as ext4, XFS, btrfs, etc., and be mounted at a mount point for normal user access. However, this abstraction also allows the creation of block devices that are not associated with any physical hardware, such as RAM disks. These are areas of RAM memory that the system exposes as if they were "disks" and that can be formatted and mounted like any other drive.
Swap, on the other hand, is the area used by the system's virtual memory when RAM runs out.Traditionally, swap space resides on a disk (in a partition or a file). When the kernel needs to free up RAM, it moves less active memory pages to this swap area. The problem is that disk access is orders of magnitude slower than RAM access, which is why, when a system starts aggressively paging to swap, a very noticeable slowdown becomes apparent.
The idea behind zram is to combine the best of both worldsThis involves creating a "fake" block device within RAM and compressing the data written to it. This achieves a similar effect to having more available memory (because compressed data takes up less space) at virtually the same speed as RAM, since the bottleneck becomes the CPU instead of the disk. This is especially useful on systems with a decent CPU, where the cost of compression and decompression is very manageable.
Furthermore, another clear advantage is that the RAM does not wear out.Using swap on an SSD involves constant writing to a storage medium with a finite lifespan; with zram, all swapping occurs within memory, without damaging disks or requiring dedicated disk space. This is why more and more users with laptops and SSDs prefer to configure swap on zram and, in many cases, completely disable disk swapping.
Compression algorithms in zram: lz4, zstd, lzo and company
In general, the following performance order is usually accepted.In terms of pure compression and decompression speed, lz4 is usually the fastest, followed by zstd and then lzo. Regarding compression ratio (i.e., how much it reduces data size), zstd usually wins, followed by lzo and then lz4. This means that zstd compresses more but is slightly slower than lz4, while lz4 sacrifices compression ratio for lightning-fast performance.
In many desktop scenarios, zstd offers an excellent balanceIt compresses better than lz4 (thus "stretching" RAM more) and is usually fast enough on modern machines. For this reason, some administrators and advanced users recommend configuring zram to use zstd as the primary algorithm, unless the hardware is very CPU-limited and every cycle counts.
The available and selected algorithm can be viewed within the system itself. When the zram module is loaded, simply checking the contents of /sys/block/zram0/comp_algorithm will show which algorithms the kernel currently supports and which one is active. This information is also commented out in some configuration files for tools like zram-tools.
In any case, the choice of algorithm does not usually require frequent changesThe usual approach is to select one (for example, zstd or lz4), leave it as the default in the settings, and, if you're particularly meticulous, run some performance tests on your specific system. Most users will notice a clear improvement simply by enabling zram, regardless of any subtle differences between algorithms.
Enable zram on Debian, Ubuntu and derivatives with zram-tools
In Debian, Ubuntu, and many APT-based derivatives, the easiest way to enable zram is by using the zram-tools package.This package provides a service and a central configuration file that allow you to easily define how zram will be used as swap. It's a tried and tested, fairly standard method that works well on Debian 10, Debian 11, Ubuntu 20.04, Ubuntu 22.04, and close versions.
The first step is to install the corresponding package from the terminal.using sudo or the root account. The typical command in these distributions would be:
sudo apt install zram-tools
Once zram-tools is installed, the key file is /etc/default/zramswapThis is where parameters such as the compression algorithm, the total size of zram, and its priority over other swaps are defined. If you reboot without changing anything, the system usually creates a small amount of zram by default (for example, 250 MiB), which is often insufficient for realistic desktop use these days.
To adjust the settings to your needs, you need to edit that file with your favorite editor.Those who prefer a graphical environment can use something like Geany with sudo, while those who prefer the console usually use nano or similar. For example:
sudo nano /etc/default/zramswap
Within this file you will find several annotated sections These explain the available options. There's usually a section about the compression algorithm, another about the percentage of RAM to be dedicated to zram, another to set a static size in MiB, and a priority option for swap. The format may vary slightly between versions, but the underlying concept is the same.
Adjust the algorithm and size of the swap space on zram according to the computer's RAM.
One of the most important adjustments is selecting the appropriate compression algorithmIn zram-tools there's usually a line like ALGO= that's commented out or has a default value (often lz4). If you want to make better use of your RAM and your CPU isn't extremely limited, you can change it to use zstd, resulting in something like:
SOMETHING=zstd
The second major parameter is how much space to allocate to zramThere are two common strategies: configuring a percentage of the total RAM or setting a specific amount in MiB. Some examples use variables like PERCENT or PERCENTAGE to define a percentage (for example, 50 for half of the RAM), while other versions of the file expose a variable like SIZE or ALLOCATION to indicate a fixed size.
If you opt for a percentage, a very common configuration in systems with 4-8 GB of RAM is to allocate around 50-70% of the memory a zram. For example:
PERCENT=50
Or in some configuration variations: PERCENTAGE=50. In this way, on a computer with 8 GB of RAM, approximately 4 GB of swap compressed in zram would be created, which is usually sufficient for most desktops and avoids getting too close to the physical limits.
On machines with plenty of RAM, it's usually more sensible to define a static size.Instead of allowing a very high percentage to generate disproportionate zram, values such as SIZE or ALLOCATION in MiB can be used. Some administrators recommend, for example, 4096 MiB (4 GiB) of zram for systems with more than 8 GB of RAM and 8192 MiB (8 GiB) for systems with more than 16 GB, leaving the remaining memory uncompressed.
A real-world example on a computer with 32 GB could be: ALGO=zstd and SIZE=4096, which reserves 4 GB of swap in zram using zstd. Some users use the archinstall installer in Arch Linux for 32 GB machines as a reference, which usually allocates around 4 GB of zram; others prefer to go up to 8 GB if they run many virtual machines or large containers.
This configuration file usually includes fairly detailed comments. Regarding the available options and references to the official kernel documentation, it's a good idea to read those comments carefully to understand what each variable does. If you're unsure what value to set, err on the side of caution: it's better to start with 4 GB and increase later than to go straight to excessively large sizes that could leave you without enough RAM when compression fails to reduce the data significantly.
Management of existing disk swap and swap priorities
When you enable zram, it's important to check what disk swap you have defined.Many Debian and Ubuntu installations create a swap partition during installation, or, in more recent versions of Ubuntu, a swapfile in the root filesystem. If you don't disable these, you'll end up with both compressed swap in RAM and swap on disk, and the kernel will decide which to use based on its priorities.
On computers with a lot of RAM (for example, 16 or 32 GB), many users choose to completely disable the swap partition or disk fileIn these cases, they rely solely on zram for swapping, which avoids slow disk access and reduces wear on SSDs. If your Debian or Ubuntu system didn't create a swap partition on disk (for example, because you omitted it during installation), you don't need to touch anything in /etc/fstab.
If you do have a swap partition, you can disable it by editing /etc/fstab and commenting on the line corresponding to the assembly point of that swap. The typical procedure would be:
sudo nano / etc / fstab
Find the line that refers to the swap partition (usually with swap type) and comment it out by adding a # at the beginning. After saving the changes and restarting, the system will no longer mount that disk swap and will focus on the zram. As a guideline, on systems with only 4 GB of RAM it can be a good idea to keep the disk swap as an additional backup, while from 6-8 GB onwards you can manage quite well with just zram.
In the zram configuration file itself, you can also adjust the device priority.This is done using an option like PRIORITY. swapon allows you to assign numerical priorities to each swap area; the higher the number, the sooner the kernel will use that swap. It makes sense to give zram a higher priority than any swap on the HDD or SSD, ensuring that the fast swap in RAM is exhausted first, and then, only if necessary, the disk swap is used.
If at any time you want to check which swap is active and in what order it is usedYou can use `swapon --show`. This command will display a table showing the swap devices, their size, current usage, and priority. It's a quick way to confirm that your zram configuration is working as expected and that the system is actually taking advantage of it.
Using zram as a compressed RAM disk with systemd zram-generator
Besides zram-tools, on modern systems with systemd there is another very powerful option: zram-generatorThis package allows you to define zram devices using configuration files in /etc/systemd, with enough flexibility to use them as both swap and general-purpose disks formatted with file systems such as ext4.
The main configuration file is usually /etc/systemd/zram-generator.confIn many installations, it doesn't exist by default, so you'll need to create it manually. Alternatively, you can set up a structure in /etc/systemd/zram-generator.conf.d/ with one or more *.conf files that act as configuration "snippets" and overwrite parts of the main file if one exists.
The syntax is based on sections with names like , , etc. The numbering starts at 0, thus defining the first device, the second, and so on. Within each section, parameters similar to those seen in zram-tools can be set, but with some additional options specific to systemd and disk usage.
Among the most interesting options is compression-algorithmThis allows you to choose the compression algorithm (for example, zstd, lz4, lzo-rle, etc.). If not specified, the system will use the kernel default, which in many cases is lzo-rle. There is also an `options` directive for passing mount or swap parameters, such as `discard`, which causes unused compressed pages to be cut off during execution, optimizing memory management.
Another relevant directive is writeback-deviceDesigned for advanced scenarios, this option allows you to designate a device to store pages that cannot be effectively compressed, offloading them from zram. This configuration is less common on desktop computers, but it can be useful in very tight environments where you want to combine compressed memory and backup storage.
If you want to use zram as a compressed RAM disk, fs-type and mount-point come into play.With `fs-type` you define the file system (for example, ext4) and with `mount-point` the directory where the zram device will be mounted. This allows, for example, mounting an ultra-fast, compressed temporary directory in RAM, useful for heavy compilations, processing temporary data, or application caching.
It is possible to mix uses in the same device by creating multiple zram deviceszram0 as swap and zram1 as a disk mounted with ext4. In some practical examples, it is configured, for instance, that zram0 is only created if the system has more than a certain amount of RAM (for example, 9 GB) and that each device can use up to 2 GB of physical memory, thus adjusting the maximum consumption of zram and preventing it from hogging too much RAM if the compression is not as effective as expected.
Practical checks and automatic behavior of zram-config
Some tools like zram-config (used in certain Ubuntu derivatives) automate much of the configurationOnce installed, this package creates and configures zram by default, requiring minimal user intervention. After rebooting, the system starts with the zram devices ready for use as high-priority swaps.
One of the common strategies of these automatic configurations is to create a zram device for each CPU coreThis allows the compression and decompression load to be distributed among multiple threads, making better use of multi-core processors. In a quad-core system, for example, you would see four zram devices (zram0, zram1, zram2, zram3) of similar sizes that the kernel can use in parallel.
The total combined size of zram is usually set at around half of the physical RAMThus, on a machine with 8 GB of RAM, the default configurations could generate around 4 GB of zRAM distributed among the various devices. This amount usually provides a good balance between taking advantage of compression and not leaving the system without sufficient headroom for processes that don't compress well.
The priority assigned to these zram devices is usually higher than that of disk swap.This ensures that the system will first use the compressed RAM swap space and only resort to the disk as a last resort. This preference can be verified with `swapon --show`, where you will see the numerical priorities and can confirm that zram takes precedence over the traditional swapfile or swap partition.
To check the exact configuration and actual usage of zram, there are several useful commands.For example, `cat /proc/swaps` displays all active swap areas, including zram, their size, and how much is being used. You can also inspect `/sys/block/zram*/` for detailed statistics and advanced parameters, such as the current compression algorithm or the amount of physical memory consumed versus logically allocated.
In practice, once zram is properly configured, the user usually doesn't need to intervene.The operating system decides which pages are moved to zRAM, when to compress them, and when to release them. What you will notice, if you use resource-intensive programs or open many applications, is that the machine performs better before it starts to slow down or close processes due to lack of memory.
Configuring swap over zram in Linux allows you to make even better use of available memory.Reduce disk accesses and improve overall system fluidity, whether you have a laptop with 4-8 GB of RAM or a desktop with 32 GB; by understanding the size, algorithm and priority options, you can adjust this functionality to your liking and get the most out of it without complicating your life.
Passionate writer about the world of bytes and technology in general. I love sharing my knowledge through writing, and that's what I'll do on this blog, show you all the most interesting things about gadgets, software, hardware, tech trends, and more. My goal is to help you navigate the digital world in a simple and entertaining way.
