1. Introduction §
The main laptop I use is a Lenovo x390 with only 8 GB of memory, which is not a lot nowadays with a web browser and a few applications opened. The memory cannot be upgraded on this laptop because it is soldered on it, but thanks to memory compression I can easily squeeze a few extra GB out of it.
I wrote about compression with BTRFS in a previous article, it can save a lot of disk space depending on the data. This time, it is about compressing memory with zram or zswap, two features of the Linux kernel.
Sharing some statistics about BTRFS compression
In this blog post, you will learn what zram and zswap are, how they differ, and how to check if they are doing a good job on your system.
I have been experimenting with both in various cases. I found zswap to give better results in most cases, but it requires a real swap to work.
2. zram and zswap §
Both features compress memory pages, but they work in a different way.
2.1. zram §
zram creates a block device stored in memory and its content is compressed. When used as a swap device, the system swaps to a part of the memory, but as the data is compressed, it takes less space than the original pages.
The good point is that zram does not require any disk. The downside is that it increases the pressure on the memory, because everything relies on the memory, and when the zram device is full, there is nothing to go to unless you configured another swap.
Linux kernel documentation: zram
2.2. zswap §
zswap requires a real swap on a storage, like a partition or a swap file. It works as a compressed cache in front of it, when a page is going to be swapped, zswap compresses it and keeps it in memory in a pool. When the pool reaches its maximum size, the oldest pages are written to the swap device.
With a 3 or 4 times compression ratio, this means you can avoid swapping to the disk backed swap and get high throughput, and if non compressible data get swapped, it is sent directly to the disk.
Linux kernel documentation: zswap
2.3. Which one to choose? §
In my opinion, zswap is more efficient, unless you have an old and slow hard drive. The memory is used for what compresses well, and the swap on disk is here as a safety net when the memory is really full. zram is a good choice if you do not have a swap on disk, or if you do not want to write on it.
Whatever you choose, do not use both at the same time. If zswap is enabled while zram is the swap device, zswap compresses the pages before they reach zram which compresses them again, this wastes CPU and memory for nothing. Do not cross the streams, it would be bad.
Some distributions enable zram or zswap by default, so it is a good idea to check what is enabled on your system before adding the other one.
3. Statistics with compswap §
To know if memory compression is useful, you need numbers. The kernel exposes a lot of statistics in /sys, /proc and debugfs, but they are not easy to read, so I made a small tool named compswap to display them.
compswap git repository
compswap is a shell script which detects if zram or zswap is in use and displays information like the compression algorithm, the amount of data stored, the memory really used, the compression ratio and the "effective RAM", which is the physical memory plus what is saved by compression. It also gives hints about common issues, like a double compression with zram and zswap together, a full pool or low compression ratio.
Running compswap will display the statistics once, but you can have a regular refresh using compswap -w 2 to refresh the output every 2 seconds. Some zswap counters are only available to root, run sudo compswap to display them.
4. Configuration §
There are many guides explaining how to configure zram or zswap the best way (compression algorithm, size of the pool, vm.swappiness), so I will not write another one. The Arch Linux wiki is a good start, even if you do not use Arch Linux.
Arch Linux wiki: zram
Arch Linux wiki: zswap
5. Conclusion §
Memory compression is an easy way to squeeze more out of your hardware, it is free, it only costs a bit of CPU and it can give a second life to a computer with a small amount of memory. On my x390 with 8 GB, it makes a difference and I have not been able to notice any lag, more fan spinning or reduced battery life.