06-10-2020, 10:56 PM
Compressed swap is certainly awesome, but I would suggest looking very hard at zswap as opposed to zram. You get some of the same benefits, but a far more "swap-aware" compressed swap system than zram, and you avoid some of the problems with zram (at least if you have a traditional swapfile as well).
While zram is just a compressed block device, zswap is a compressed frontend to the disk based swap - it does require a swapfile on the disk, but it largely won't get used. When the kernel wants to evict a page from RAM to swap, it routes it through zswap, which checks for several things. If the page is a same-filled page (typically all 0s, but could be any repeating value), that gets stored and the page is freed without taking any swap space, because it's easy to recreate. The page is then run through a configurable compression algorithm (typically either lz4 or lzo). If it doesn't compress well enough, it just gets tossed out to the backing swap. After compression, the kernel will store either two (zbud) or three (z3fold) compressed pages in a single page of RAM. It's slightly wasteful in that a single page is rarely totally full of compressed data, but with the same filled page compression, I typically see 2.7x compression in zswap under normal running.
Where zswap is definitely superior, though, is that it has a LRU concept - least recently used. Old data can be pushed out of RAM to the backing file. If it's not been used in a long time, it's unlikely to be used again, so it can get evicted from RAM and free up that space. zram does have the writeback feature, but it's not page-aware - it can only write out whole pages of compressed data. And I'm not regularly seeing that feature enabled in the "swap on zram" configs. This, then, leads to a bit of a priority inversion. The oldest, longest-swapped out pages are likely to be stuck in zram, always taking memory, and reducing the amount of RAM available for active tasks. It's better than nothing, but under higher memory pressure, it ends up not working nearly as well.
So if you like zram, try zswap. It's built into most modern kernels, just enable it over in /sys/module/zswap/parameters/enabled, and tweak settings as you wish. You might try lzo as the compressor and z3fold as the zpool, if you're above about kernel 5.0 - there are bugs in z3fold before around 4.20 that cause weird lockups under high memory stress.
While zram is just a compressed block device, zswap is a compressed frontend to the disk based swap - it does require a swapfile on the disk, but it largely won't get used. When the kernel wants to evict a page from RAM to swap, it routes it through zswap, which checks for several things. If the page is a same-filled page (typically all 0s, but could be any repeating value), that gets stored and the page is freed without taking any swap space, because it's easy to recreate. The page is then run through a configurable compression algorithm (typically either lz4 or lzo). If it doesn't compress well enough, it just gets tossed out to the backing swap. After compression, the kernel will store either two (zbud) or three (z3fold) compressed pages in a single page of RAM. It's slightly wasteful in that a single page is rarely totally full of compressed data, but with the same filled page compression, I typically see 2.7x compression in zswap under normal running.
Where zswap is definitely superior, though, is that it has a LRU concept - least recently used. Old data can be pushed out of RAM to the backing file. If it's not been used in a long time, it's unlikely to be used again, so it can get evicted from RAM and free up that space. zram does have the writeback feature, but it's not page-aware - it can only write out whole pages of compressed data. And I'm not regularly seeing that feature enabled in the "swap on zram" configs. This, then, leads to a bit of a priority inversion. The oldest, longest-swapped out pages are likely to be stuck in zram, always taking memory, and reducing the amount of RAM available for active tasks. It's better than nothing, but under higher memory pressure, it ends up not working nearly as well.
So if you like zram, try zswap. It's built into most modern kernels, just enable it over in /sys/module/zswap/parameters/enabled, and tweak settings as you wish. You might try lzo as the compressor and z3fold as the zpool, if you're above about kernel 5.0 - there are bugs in z3fold before around 4.20 that cause weird lockups under high memory stress.