Zram vs swap partition vs swapfile

To end the discussion in the “show your desktop” about swap partition yes/no. I want to hear your thoughts about swap and why you use it or not.

Personally i have a system up and running with btrfs and 32GB of ram WITHOUT swap enabled. System runs very smooth compared to my Pop! OS install on the same notebook. I do notice improvements with Endeavour OS (like moving a window). So i really wonder what benefits a swap partition would have.

Pop OS uses zram + a swap partition. From my reading online this is not a great combo.

I did a test on a 13 years old notbook, enabled zram according to the arch wiki. I don’t see any benefits from it, only when you compile stuff it will use the swap.

I also would like to know how many amount of swap is needed for sample when you have 64gb of ram installed, how many do you add as swap or zram ? I don’t get a clear answer to that.

1 Like

I truly don’t do things that need it with 32 GB, but I have zram enabled, and sometimes I see a few K in there ;0
Swap is mostly an idea from when machines didn’t have enough memory for their tasks. Someone with high utilization (like video processing multiples, compiling, etc) might need swap, but average desktop users of 16 GB or more probably don’t.

You will need a swapfile or partition for hibernation, if you choose to use it..the size of your memory.

1 Like

Nowadays I only use swapfile (or partition) if I want to activate hibernation. I always enable zram on all my systems, regardless the amount of RAM.

2 Likes

It is a good idea to have some amount of swap of some kind. I have 64GB of RAM and without some swap I find that the oom killer will start killing applications at times.

This is not an answerable question. There is no set size. It all depends on your use.

4 Likes

That’s a question I asked myself for some times …

Fedora only offers ZRAM. I disabled that and created a swap file on BTRFS some month ago. Arch Linux installation via archinstall also uses ZRAM when enabling it. I don’t know what is best for me. I prefer a separate partition like I get while installation of EndeavourOS.

I’m using 32 GB RAM and an 8 GB swap partition. On my laptop, I have 16 GB RAM installed and want to use hibernation mode. So I need a swap partition of minimum 16 GB.

1 Like

I do understand that swap file and swap partition writes on disk while zram uses only memory.

I don’t want unnecessary writes on my Samsung 990 pro’s 4TB’s so i would use zram or nothing at all. But i don’t get a clear answer why swap is needed with that amount of ram (64GB).

Much of the technicalities of swapspace and how kernel handles memory management is above my head, but I gather that some amount of swapspace of some sort helps the kernel to free up some cache space for more frequently used pages by pushing those more rarely used pages into swap resulting in a better management of memory and better performance.

I am not sure if I have got it right but that’s about what I think I know which I also admit that it can be totally erroneous :sweat_smile:

2 Likes

Ultimately, memory utilization is something that varies a lot by use case.

If having no swap is working for you, I wouldn’t worry about it overly unless you start having issues.

1 Like

For me I personally use ZRAM.

I have 64GB or RAM in my system and I have 8GB of ZRAM set up, usually it is within 24-500KB, sometimes a few Megabytes-24MB, but there have been a few times where it reached 3.4GB usage.

I read in an old thread that some swap is always required regardless of RAM amount so I simply followed that advice. Do I need 8GB for ZRAM, I don’t really know, I originally set it to see what the largest amount I would use would be so I guess I could reduce it to 4GB. It could be a lot but I have been fine so far and my RAM usage is always around 60% every day, but the ZRAM usage varies depending on what I am doing.

1 Like

As @dalto pointed out below, this depends heavily on your use case, and there are no hard and fast rules. As a rough guide, however, here’s the list I found online:

  physical RAM      no hibernate     with hibernate    Maximum

       5GB              2GB                 7GB          10GB
       6GB              2GB                 8GB          12GB
       8GB              3GB                11GB          16GB
      12GB              3GB                15GB          24GB
      16GB              4GB                20GB          32GB
      24GB              5GB                29GB          48GB
      32GB              6GB                38GB          64GB
      64GB              8GB                72GB         128GB
     128GB             11GB               139GB         256GB
     256GB             16GB               272GB         512GB
     512GB             23GB               535GB           1TB
1 Like

Swap is not needed for sleepmode, it’s needed for hibernate i have read. My notebooks without swap are waking just fine and i don’t hibernate.

My gaming notebook is only used for gaming and surfing the net.

OK, translation fail from DeepL …

1 Like

So the people who uses zram, did you also use the setting to write back at disk ? If yes/no … why ?

I have seen this option in zram-generator.conf but I have never looked into it. It seems to be a measure for Zram to write the incompressible pages into a storage device to free up space in memory.

writeback-device=

Write incompressible pages, for which no gain was achieved, to the specified device under memory pressure. This corresponds to the /sys/block/zramX/backing_dev parameter.
Takes a path to a block device, like /dev/disk/by-partuuid/2d54ffa0-01 or /dev/zvol/tarta-zoot/swap-writeback.
If unset, none is used, and incompressible pages are kept in RAM.

I guess if someone wants to use zram, it won’t hurt to add a little chunk of disk space for this purpose.

I just switched to zram last week (32GB RAM, 16GB zram). I tend to have at least virtual machine running (16GB used with Windows 11, usually) or a game, so my zram tends to get used quite a bit. I have not setup a writeback device yet, kinda waiting to see how this goes over a month or 2. So far the performance is good.

When I get my extra 32GB RAM next month, I don’t think the writeback device will be necessary.

1 Like

re: zram v swap —I am assuming you can’t tell any performance issues?

edit/

Same question I guess. When you had a conventional swap partition..do you notice any differences to now?

that sounds reasonable

1 Like

The differences sound minute. Like variations on a theme–just different ways to do the same thing.
–Do I have this right?

OR?

Are all these different memory swaps for different applications? I.E. gamers, laptop owners, people who adore suspend and hibernate, people who are app-heavy to begin with, etc?

every word from your first post I coulda asked. thanks for asking

1 Like

I’ve noticed faster load times of heavy-resource apps, like games and VMs. For example, my system right now has the following memory load:

$ free --mega
               total        used        free      shared  buff/cache   available
Mem:           33445       11751       13120         508       11100       21694
Swap:          16722        4533       12188

If I start my Windows 11 VM, my system barely blinks. This was not the case before, even though I set the swap priority very low (vm.swappiness = 10), whereas my zram settings are much higher:

vm.swappiness = 180
vm.watermark_boost_factor = 0
vm.watermark_scale_factor = 125
vm.page-cluster = 0
2 Likes

Seems like I’m kind of an outsider here by using a swapfile with zswap on all my systems (baremetal and vms).
On the more memory constrained ones I see regular use of zswap. System performance seems mostly untangered in this state, although I did no actual performance tests .

The system will use (z)swap to remove unused data from memory to free it up for caches/buffers. So i guess it’s good to have atleast some swap, but i don’t know much about the actual memory management of the linux kernel, so this is just a wild guess.

“Server” system with 4 GB RAM:

root@sys:~# free
               total        used        free      shared  buff/cache   available
Mem:         3908600     1950180      435880        4128     1710580     1958420
Swap:        1048572       37844     1010728

root@sys:~# grep -r . /sys/kernel/debug/zswap/
/sys/kernel/debug/zswap/stored_pages:8093
/sys/kernel/debug/zswap/pool_total_size:12886016
/sys/kernel/debug/zswap/written_back_pages:0
/sys/kernel/debug/zswap/reject_compress_poor:0
/sys/kernel/debug/zswap/reject_compress_fail:11
/sys/kernel/debug/zswap/reject_kmemcache_fail:0
/sys/kernel/debug/zswap/reject_alloc_fail:0
/sys/kernel/debug/zswap/reject_reclaim_fail:0
/sys/kernel/debug/zswap/pool_limit_hit:0
1 Like