Understanding zswap

I still struggle to understand zswap. My assumption was that before anything goes to the swapfile it is stored in RAM which is 16 GB.

My zswap configuration is:


Then I see


and swapon shows

/swap/swapfile file  18G 3.8M   -2

Can anybody explain why there are 3.8M in the swapfile?

zswap still uses swap, it just provides a compressed RAM cache to facilitate making the swap more efficient and less write intensive–sort of like a buffer, to make writing to swap work better.

You may be thinking of zram, which is a swap device unto itself; it essentially replaces swap (except for hibernation).

This is a short explanation from the ArchWiki:

Zswap is a kernel feature that provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to disk are instead compressed and stored into a memory pool in RAM. Once the pool is full or the RAM is exhausted, the least recently used (LRU) page is decompressed and written to disk, as if it had not been intercepted. After the page has been decompressed into the swap cache, the compressed version in the pool can be freed.

The difference compared to zram is that zswap works in conjunction with a swap device while zram is a swap device in RAM that does not require a backing swap device.

1 Like

That’s exactly what baffles me. As in my example above the cache (pool) isn’t full. Why do I see anything in the swapfile?

This means your pool size is 25% of your memory, so in your case the zswap pool is about 4GB. After your system has about 12GB of RAM in use it should start passing pages to swap.

All 4GB don’t need to be full before the swapfile is used:

This means when the pool is 90% full, it will start ignoring new pages (they get passed directly to swap) until there is enough free space to take more pages. So when you have about 3.6GB in zswap, it will start using the swap a little bit.

Certain types of suspend will also use swap on the disk instead of zswap or RAM.

I don’t think that is quite right.

That should mean that once the compressed pool becomes completely full it will write directly to swap until it drops to 90% full. Once it reaches 90%, it will re-engage the compressed pool again.

1 Like

Gotcha, yes that makes more sense.

Somehow I got a little mixed up reading through this explanation, with the hysteresis and all:

To prevent zswap from shrinking pool when zswap is full and there’s a high pressure on swap (this will result in flipping pages in and out zswap pool without any real benefit but with a performance drop for the system), a special parameter has been introduced to implement a sort of hysteresis to refuse taking pages into zswap pool until it has sufficient space if the limit has been hit.

Now that you explain it, it makes a lot more sense. :bulb: