Zram vs zswap. Why choose one over the other

This is a followup to my recent post as well as other post in the Forum.

I am a noob at this, both zram and zswap. I have very vague ideas on both of them. All I know is that zram has to be installed as package on EOS, whil zswap is practically baked into almost all the Linux Kernels out there. Even those kernels that are not used on EOS. Also both of them work in conjunction with SWAP and RAM.

So here are my queries
What is the difference between these?
What are the pros and cons with each of them vs the other? i.e. How is zram better than zswap? How is zswap better than zram?
When should zram be preferred? When should zswap be preferred?
Can those who have used zram or zswap or both or one of them in the past, share their thoughts and experience?

I am trying to gather info and experience over here. Any comments, hints, pointers, counterpoints, sharing of experiences would be deeply welcome.

1 Like

I’m interested in learning more about this topic. I’m currently using zram because it’s the simplest option for my setup.

1 Like

This article seems to provide a reasonably good explanation of and comparison between zram and zswap:

https://www.baeldung.com/linux/zram-zswap-zcache-comparison

It comes down, as many other occasions, to system configuration and use case.

3 Likes

Can you please share some details on your setup? Like what is the zram-size that has been configured? What is your Physical RAM size? What compression algo are you using, ZSTD or LZO or something else?

What has been your experience in using ZRAM? Have you noticed a degradation in performance?

Any tips?

This is mine. Physical RAM is 32Gb. I also have 8Gb of on disk swap. Performance has been excellent.

[zram0]
zram-size=8G
compression-algorith=zstd
swap-priority=100
Filename                                Type            Size            Used            Priority
/dev/zram0                              partition       8388604         224             100
/dev/sdb1                               partition       8191996         0               -2
2 Likes

Thanks for the article. Will go through it.

About system config and use case, can you please elaborate a bit on it? For example if we have a CPU which is say about 5 years old then using zram would be fine? Or something older than that would negate zram benefits?

I have seen a discussion about a year ago between @mbod and @dalto where they specifically discussed about a use case where a user was CPU constrained and not memory (RAM+SWAP) constrained. Are you referring to a similar use case?

I’ve had my computer for 5 or 6 years.

Operating System: EndeavourOS 
KDE Plasma Version: 6.5.0
KDE Frameworks Version: 6.19.0
Qt Version: 6.10.0
Kernel Version: 6.17.5-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 12 Ă— AMD Ryzen 5 2600X Six-Core Processor
Memory: 32 GiB of RAM (31.3 GiB usable)
Graphics Processor: NVIDIA GeForce GTX 1660

I don’t have any tips, @Archie1 — I’m still learning too :slightly_smiling_face:

I have 64 GB of RAM.
Below is my configuration:

[zram0]
zram‑size = ram / 2 
max-zram-size = 30720 
compression‑algorithm = zstd 
swap‑priority = 100

swapon

NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition  30G   0B  100

Performance-wise, I honestly don’t notice any difference.

After reading the article shared by @cactux, it seems that zswap might be a better fit for my use case.

2 Likes

Thanks. This helps.

Despite having 32GB physical ram, zram still gets used. Do you deal with with very heavy or big data sets or work with big file sizes?

Is your CPU more than 5 years old or less than 5 years old?

I was under the impression that zram-size was expressed as percentage/fraction of actual physical RAM.

With 64 GB of Physical RAM, why would a need for ZRAM arise? Are you running a server on the machine?

Can you please elaborate a bit on this?

This is based on what I understand from the summary table.

Anyway, I don’t plan to change anything on my machine. I’ll keep it as is, using zram without any swap partition or swap file.

From what I’ve gathered from this forum, it’s better to have swap even if you have a large amount of RAM.

No, but I do compile quite large software, but in my experience, Linux likes using some swap.

More than.

If I recall correctly, it can be, but I’m not completely sure about that.

Please note that zswap works in conjunction with a swap devicie, a file or partition.
For zram there is no need for a “regular” swap device unless one needs it for hibernation for example.

4 Likes

Yes, @cactux — thanks for the tips! That’s why I’ll just keep things as they are, using zram without any swap partition or swap file.

2 Likes

Thanks @cactux this article is a gem and very useful. I did not realize that there is a 3rd option zcache that is available too.

@cactux apart from the questions that I had raised earlier. I have a few additional questions. If you can please indulge me. I have 8GB RAM. I have not gone beyond 65% RAM usage, but I can forsee, due to code compiling in python that RAM usage will be breached. Also many applications like LibreOffice, Firefox are not exactly memory lean. From the article it does appear that zswap would be preferred over zram. Though zswap does not seem to have the same level of configuration as zram. Would love to know your thoughts.

You could customize zswap as well.
See this article from ArchWiki: https://wiki.archlinux.org/title/Zswap#

Honestly, not sure which of this alternatives would fit best your use case.
On a laptop with 16GB RAM, I have both zram and swapfile.

Filename				Type		Size		Used		Priority
/dev/zram0                              partition	8388604		0		100
/swap/swapfile                          file		16777212	0		-2

[zram0] 
zram-size = 8192
swap-priority = 100
compression-algorithm = lzo-rle

The swapfile was intended primarily for hibernation which I haven’t been using now for some time.

If I were you, I would try both alternatives and put my system through its paces in your actual use case scenario. Monitor the swap usage. See if you observe any performance difference.

I may be wrong about this but I think, even using zram, you may get some benefits from having some swap file/partition for those occasions when your reaching the size limit of zram. I’ll happily stand corrected if I have got this wrong.

PS. I think I’ll be disabling my zram now to see how zswap + swapfile will handle swapping on my system.

1 Like

Would love to know your experience and comparison on how zswap fared against zram. Please do update this thread after say 3-4 weeks with your observations.

1 Like

Most normal x86 desktop/laptop CPUs from the last 15 years should be fine. Many even much older than that will be fine. Unless you are using a CPU that is ultra low power or some type of low-end embedded CPU, I don’t expect you would have any issues here.

Keep in mind, that was a single person reporting something for a highly specialized use case.

It is always good to have some swap. I also have 64GB of ram and if I don’t have any swap enabled, the kernel will kill processes occasionally.

Assuming a typical zram config where you set it to 100% of RAM, if you are exceeding that and going to swap your system performance would be absolutely terrible at that point. I think I would either change my computing habits or get more RAM if that was happening.

5 Likes

Both work great. If one or the other works noticeably better completely depends on your setup and workload.

For most people it doesn’t matter. Just have any swap at all is fine. If it isn’t fine, you will notice.

2 Likes

would also add to this that whatever/whenever I install I let the installer create a good old fashioned swap partition.

My use case: boring, calm.

@cactux I absolutely trust the Baeldung people and would adjust my consideration about my use case. They rec zswap based on having 12GB of memory alone. Good find. I’m re-reading it.

1 Like