Which one should I use: zswap or zram?

I mostly use CPU-bound applications that don’t require much RAM, therefore I was wondering which one to use. I also use an HDD (yes, I know). I’ve been using zswap all this time and I wondered if I wrong all along. It works fine with zswap, but I don’t use much RAM, so I don’t know if it benefits my use case or not.

I Googled it, and everyone claims somewhat conflicting things. Some argue that zram consumes more CPU resources because it compresses everything, while others say that zswap requires more CPU resources without explanation. The only thing I’ve seen to be constant is that zram is faster than zswap because it uses ram which is faster than a HDD/SSD and because of that zram performs far less reads and writes to the disk, which could improve performance compared to zswap.

I have enough RAM for everything I use (I rarely use more than 40% of it), so I was wondering which one to use. Maybe even no zswap/zram?

Neither. You should use traditional swap.

Both zswap and zram incur CPU overhead due to compression. If you are already CPU bound, I would stay away from these options.

2 Likes

Neither. You should use traditional swap.

You are right. I’ll do that. Thanks!

The only time I would avoid zram is if your CPU is not fast enough to handle the compression. In those cases the compression overhead may cause performance to decrease.

This comment of yours really cleared things up for me. Thank you again!

1 Like

If you don’t intend to hibernate your computer, make a ~ 6 GB swap file on the fastest drive you have. That’s all you’ll likely need (assuming you don’t have any special requirements). If you have plenty of RAM, you’ll almost never use that swap file, but it will ensure that your RAM usage is efficient (what constitutes “plenty” depends on what you do, of course, but say 16 or 32 GB is what I would consider a lot of RAM in normal circumstances).

Especially on ext4, swap files are usually better than swap partitions because they can be deleted and recreated without repartitioning the drive. Swap partitions are not very flexible.

Unless you regularly experience OoM situations, you shouldn’t worry about swap affecting your SSD lifespan – on systems that have plenty of RAM, the impact of swap is negligible. You want to use the fastest SSD you have for swap, so that you never notice it being used.

And if you don’t have enough RAM, swap won’t really help you in that regard, it’s a very poor substitute for RAM. On systems with insufficient RAM, the best swap will do is prevent the computer from freezing in an OoM situation, giving you some time to gracefully reboot it.

Of course, this is all just my preference, your use case might be different than mine. But I think it’s a sensible starting point if you are unsure what to do.

2 Likes

Which CPU you have?

Zswap is a kernel feature and not a swap device but a compressed RAM cache for swap pages.

It is enabled by default. There is no setting up there.

For it to work, it needs the backing of a “proper” swap device, that is, a swap partition or a swap file.

If you don’t want to use zswap, you would need to explicitly disable the kernel feature.

I am using zram since years. I have 64 GB of RAM and 16 GB swap with zram (=25% of RAM). And I have tested it extensively with stress-ng.

Example:

stress-ng --vm 4 --vm-bytes 100% --vm-method all --verify -t 5m -v

This command consumes all RAM and swap. Depending on how many apps are running during the test, I can even go up to 120% memory consumption, which basically consumes all swap.

I am not experiencing any issue with that. In fact, I tested this with regular swap on a fast nvme device. The difference is dramatic. With swap on nvme the PC is slowing down dramatically when several GB of swap are being used. Not so with zram. With zram I can still work normally and the PC is still in good response even when I use 10+ GB of swap.

I would always go for zram if you are not hibernating.

I also use zram extensively. However, we are talking about a highly specialized use case here. A case in which we are highly CPU bound and using very little memory. That is not at all typical for a desktop.

If your CPU is already consumed at max and your workload doesn’t benefit from faster swap you shouldn’t use zram because it will add CPU overhead.

Well, lets think about this a minute.

User says:

and

That basically tells me that only very little swap is being used and only at rare occasions. Why would that require traditional swap on disc? From my point of view it is perfectly fine to use zram in that scenario and save some disc read/write cycles. It is not only faster with zram but it also extends the life time of the disc.

I am using zram in conjunction with gimp and very big pictures. Sometimes the gimp operations eat up all RAM and swap while it is applying CPU intense filters to the image. With zram this is not a problem at all. With traditional swap on a fast nvme it is not usable. Therefore I do not believe that your CPU load argument is valid.

1 Like

That is a different use case than the OP. A use case in which you are consuming lots of RAM and lots of CPU.

Obviously, the best solution would be for the OP to actually test both with their specific workload. However, I have seen plenty of reports of zram decreasing performance when the CPU does not have available cycles.

Every workload is different so in the absence of being able to test both it is all speculation.

Yes, but what I am saying is, that even with fully using 16 GB of swap and the PC 100% under load, the zram performance is excellent with minimal delays.

Now assume you only have a few MB to swap out. With zram you will not notice that process. It is undetectable. You will not see any delays in your apps. But you will save read/write cycles to your disc.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.