If you are using Linux, you can multiply your available RAM without having to purchase new memory modules. Zram, zswap and zcache allow you to compress the contents of your PC’s RAM, virtually expanding it. But which one should you use? Which is the best? Keep reading to find out.
The advantages of RAM compression
If you’ve seen a presentation on next-gen consoles by Sony and Microsoft, you might have noticed that the two companies argued a lot about the speed of their storage. There’s a reason for all of this: Storage is the most impactful bottleneck, even on the latest high-end PCs.
The ultimate goal of the solutions we see here is to minimize the amount of data exchanged between our computer’s RAM and its slower storage systems. This is important to keep in mind because, as we will see, it is their differentiator.
Zram is the simplest RAM compression solution in Linux today. Formerly known as compcache, it takes advantage of Linux’s support for multiple swap devices by creating one (or more) additional ones in the computer’s RAM.
Zram works by creating a hierarchy of three storage spaces. It creates a new compressed swap volume in RAM, which is – hierarchically – placed between RAM and storage devices. Then when the RAM is full of data, it moves the excess to the compressed swap. When this is also filled, zram starts moving the data to the swap partition in the storage drive.
This new swap device is given a higher priority than those residing in the PC’s slower real storage, to be used before them. It also relies on one of the many compression algorithms, like LZ4 or LZO, to store as much data as possible in this exchange device.
Modern processors have reached a point where they can compress and decompress data stored in RAM faster than it could be written to or read from an SSD or HDD, with negligible impact on the performance of the. computer.
It should be noted that zram works on its own, without the need for other swap devices. So you can, for example, use it in embedded systems or live CDs, as it does not need to be backed up by real storage.
Zswap is more complicated than zram. It improves the relationship between our computer’s RAM and its slower storage devices by adding smart compression where they intersect.
With zswap, compressed RAM and swap volumes that already exist in a computer’s storage devices are treated as nodes in an intelligent storage system. Zswap also takes up some of a computer’s RAM but doesn’t blindly move anything that doesn’t fit into the rest of the RAM and then try to compress it. Instead, it first compresses anything that doesn’t fit into the actual RAM. If the data is compressed, zswap stores it in RAM. If not, it stores it in the existing swap.
This way, zswap makes better use of the available RAM and swap space because it will not monopolize, for example, the RAM it uses with incompressible data.
Since it relies on the existing swap, zswap is better for Linux installations on all PCs, especially those with less than 4 GB of RAM.
Zcache works in tandem with the tmem (“Transcendent Memory”) system to provide the Linux kernel with a more versatile way to store data not currently in use.
Zcache adds an additional transparent memory management layer on the kernel that compresses any data that is not currently in use. Unlike having to manage every byte of data, with zcache the Linux kernel can store entire pages of data in “pools” through the tmem interface. The zcache backend then compresses these groups of data.
Like zswap, zcache only keeps compressed data in RAM and pushes uncompressed pages to the actual swap, thus making the most of both.
What is the best?
In theory, zcache offers the best of solutions. However, that doesn’t mean it’s the way to go, as practicality matters too. Despite its advanced features, zcache is generally not supported. Its development is stagnating and it was removed from the Linux kernel on version 3.11. To use it today you will have to jump through hoops, and the result probably wouldn’t be much better than if you were using zswap.
On the other hand, zram is more restricted because it is not much more than a virtual volume stored in RAM and used as swap space. This can help with systems running out of RAM, but works by blindly taking things from point A and moving them to point B and then point C without worrying about what they really are.
This makes zswap the best choice for most Linux users, regardless of their distribution. To see how you can take advantage of it, check out our guide on zswap.
Is this article useful?