If you’re using Linux, you can multiply your available RAM without having to buy new memory modules. Zram, zswap, and zcache allow you to compress your PC RAM’s contents, practically expanding it. But which one should you use? Which one’s better? Read on to find out.
The Benefits of RAM Compression
If you saw a presentation about the next-gen consoles by Sony and Microsoft, you might have noticed that both companies made a big fuss over how fast their storage is. There’s a reason for all that: 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’ll see, it’s their differentiating factor.
Zram is the most straightforward RAM compression solution in Linux today. Formerly known as compcache, it takes advantage of Linux’s support for multiple swap devices by creating an extra one (or more) in the computer’s RAM.
Zram works by creating a hierarchy of three storage spaces. It creates a new compressed swap volume in the RAM, that is – hierarchically – placed between the RAM and storage devices. Then, when RAM is filled with data, it moves the excess to the compressed swap. When that, too, is filled up, zram starts moving data to the swap partition in the storage drive.
This new swap device is assigned a higher priority than the ones that reside in the PC’s slower actual storage, to be used before them. It also relies on one out of multiple compression algorithms, like LZ4 or LZO, to cram as much data as possible in this swap device.
Modern processors have reached a point where they can compress and decompress data stored in RAM quicker than it could be written to or read from an SSD or HDD drive, with a negligible impact on the computer’s performance.
It’s worth noting that zram works on its own, with no need for other swap devices. So, you can, for example, use it in embedded systems or live CDs, since it doesn’t have to be backed by actual storage.
Zswap is more complicated than zram. It improves the relation between our computer’s RAM and its slower storage devices by adding smart compression where they intersect.
With zswap, the compressed RAM and the swap volumes that already exist in a computer’s storage devices are treated as nodes in an intelligent storage system. Zswap takes up, too, a part of a computer’s RAM but doesn’t blindly move everything that doesn’t fit in the rest of the RAM then try to compress it. Instead, it first compresses everything that doesn’t fit in the actual RAM. If the data is compressed, zswap stores it in RAM. If it isn’t, it stores it to the existing swap.
This way, zswap takes better advantage of the available RAM and swap space since it won’t, for example, hog the RAM it uses with incompressible data.
Since it relies on the existing swap, zswap is better for Linux installations on every PC, especially those with less than 4GBs of RAM.
Zcache works in tandem with the tmem (“Transcendent Memory”) system to offer the Linux kernel a more versatile way of storing data not currently in use.
Zcache adds an extra transparent layer of memory management on the kernel that compresses any data that isn’t currently in use. Unlike having to manage every single byte of data, with zcache, the Linux kernel can store whole pages of data into “pools” through tmem’s interface. The zcache backend then compresses these groups of data.
Like zswap, zcache keeps only compressed data in RAM and pushes uncompressed pages to the actual swap, thus making the best use of both.
Which One Is Better?
In theory, zcache offers the best of the solutions. However, that doesn’t mean it is the way to go as practicality also matters. Despite its advanced features, zcache is widely unsupported. Its development has stagnated, 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 would probably not be much better than if you used zswap.
On the other hand, zram is more restricted because it isn’t much more than a virtual volume stored in RAM and used as swap space. It can help with systems strapped for RAM but works by basically blindly taking things from point A and moving them to point B ad then point C without caring for what they actually are.
This makes zswap the better choice for most Linux users, no matter their distribution. To see how you can take advantage of it, check out our guide on zswap.