VOGONS


First post, by MagefromAntares

User metadata
Rank Member
Rank
Member

Hi,

I have decided to open this topic based on a brief conversation in the "RAM prices have gone insane" topic, I wished to go into more details but I didn't want to derail the topic with it. If this is the wrong forum to post this then I ask the moderation team to move it into the proper place for this discussion, for me this seemed to be the best place to post it as the generic RAM shortage thread is also there.

In this first post I describe what is my heuristic in using either ZRam or ZSwap to get the ball rolling, however I don't consider myself an expert on the topic, more like someone who is using these regularly, but not knowing the details of either enough well to say I know everything about them, so I will both try to answer questions regarding these and also welcome any suggestions to improve my usage.

First a brief description of what are these:
ZRam: Is a block device in memory that can be used (amongst other purposes) as virtual memory, it is separate from the swap physically present on the HDD/SDD (More detailed description:https://www.kernel.org/doc/html/latest/admin- … ckdev/zram.html)
ZSwap: Stands between the "normal" uncompressed RAM and the already existing swap partition/file, caching and compressing pages and writing and reading pages to the HDD/SDD when needed. (More detailed description:https://docs.kernel.org/admin-guide/mm/zswap.html)

First if the system doesn't have a swap partition/file on any of the mounted disks and you don't wish to create one, then the only option between the two is ZRam, as ZSwap requires an already existing swap to work.

My heuristic of using these:

When to use ZRam:

  • If the size of the swap is less than 75% of physical memory
  • If the storage backing the swap is slow or have long seek times(Most HDDs)

When to use ZSwap:

  • If the size of the swap is equal or larger than 100% of physical memory
  • If the storage backing the swap is quick. (Most SSDs)

You may notice that there are some overlaps and missing areas between those two, these are where I don't yet have a working heuristic, so I have to benchmark the system with both of them before deciding which to use.
Note!: This is a good place to put this: Do not use both of them at the same time!

Some other considerations you may have:

  • ZSwap generally comes in most distros with the default Linux installation, while ZRam needs a package to be installed, it is not a large package, but if you are one of those people that like to keep a Linux system as "pure" as possible this might be a consideration.
  • The worst case scenario of ZRam is worse than that of ZSwap, if ZRam gets full things can escalate to OOM, while ZSwap always have a physical swap to fall back to, I have only experienced this when stress memory loading a system and never during normal usage, so I don't think that this needs to be too high on the list of considerations unless the system has to run unattended for long stretches of time or it is used for some critical application.

There are also suggestions on the net to use ZSwap or ZRam in VMs, however if you run those VMs locally I think it is almost always better to check the memory and swap configuration of the host OS. If you are using "someone else's computer" (AKA the "cloud") to host your VMs, you might still get advantages from using these but I don't really tried them in those environments.

Anyway these are only my way of using these, it is possible that it is not optimal, this is one of the reason why I'm opening this as a topic and not writing an article 😀.

Thanks for anyone reading or contributing to this topic.

"A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it." - Dune

Reply 1 of 2, by st31276a

User metadata
Rank Member
Rank
Member

My take on this is to use your memory requirement as a guide.

If you have enough ram and don’t realy want to use swap, zram allows you to compress anonymous pages not often touched (for example, leaked memory or the dumpster that is a browser’s process) so that you have more free pages for caches.

If your ram is small (as it typically is in a vm) you have to use swap, for which zswap is a slight optimization.

Reply 2 of 2, by wierd_w

User metadata
Rank Oldbie
Rank
Oldbie

Even then, there are things to take into consideration, since zram and zswap use different allocator methodologies.

Iirc, zram uses zmalloc, and zswap uses zbud.

Zmalloc is able to handle 3 pages per physical page, and zbud can handle 2.

Please doublecheck. I'm getting old.

More or less, this means you can get better economy from zram, than from zswap, but you'll need to use a compressor that consistently gets an average of 3:1.

Zstd does that, but is computationally expensive.

If this is a desktop with underused cores, and good cooling, go for it. Set the zram conf file to use zstd. Otherwise, if this is a weak mobile device, you probably just want lzma.