So, I just migrated from “another” Arch based Distro to EOS, and changed the underlying Filesystem from ext4 to btrfs. While I was on ext4 I used a swapfile of 16GB as swap, I have 64GB of Memory available. I just did a short gaming session yesterday while I was still doing some background tasks in a VM. My physical Memory was about 60% used, but swap was 100% used - at least that’s what glances was telling me. I was a bit surprised, and that raised some questions:
My swapfile is just 500 MB (that sees to be the default of the installer) - shouldn’t that be way more bigger with so much physical memory? Is there another way to get it bigger than to destroy and recreate it bigger?
Or should I go a completely other way round? What are my options, which also make sense? Deactivate swap? Go for Swap partition? Do X?
Hi @milkytwix
I am on BTRFS here, 20 GB RAM.
According to what I read here and my own experience you already have enough RAM, more than enough that you don’t really need swap.
I am using no swap and everything is running fine.
Though I do not know if gaming requires swap though!
Update:
I might have made a mistake here.
Please see next posts.
To have or not to have a swap space is not really a question of having or not enough RAM.
It seems like a good idea to have at least some swap space no matter the amount of RAM.
Personally I would just remove the existent swapfile and make another bigger one.
You might perhaps want to have a look at zram as well:
I saw somewhere that swap partition is not good for SSD.
That is why my practice is to use swap file with btrfs layout
Never used zram, maybe it is better than swapfile
Thanks for the link @pebcak
I was always under impression that it is needed if RAM needs to be extended but I read there that Swap is not generally about getting emergency memory, it's about making memory reclamation egalitarian and efficient. In fact, using it as "emergency memory" is generally actively harmful.
I apologise for what I mistakenly said above.
But it is not only me. The author says There's also a lot of misunderstanding about the purpose of swap – many people just see it as a kind of "slow extra memory" for use in emergencies
So, I read along a bit and based on what you have written and what I learned I think I want to go ZRAM. So, before I can use that I have to disable the current swap file The way to go should look like this:
- Detect swapfile via swapon --show
- Use swapoff on the outcome, et voila, swap file deactivated
- Delete the @swap btrfs subdirectory via Btrfs Assistant, checking both “Include Timeshift and Snapper Snapshots” as well as “Include Container Subvolumes”, so then there is no swap, no swapfile and no nothing anymore as if I chose “No Swap” during install, right?
From there on onwards I would then just “simply” follow the Arch Wiki:
- Disable zswap via echo 0 > /sys/module/zswap/parameters/enabled
And then? Go the udev way? zram generator? zramswap? zramd? This is the problem that I have with the Arch wiki: So MUCH knowledge, so little guidance on how to use it…
Remove its entry in /etc/fstab as well.
This will only disable zswap at runtime.
For making it permanent, add zswap.enabled=0
to your kernel parameters.
zram-generator is what I use and can confirm that it works as advertised:
Swap space is swap space. It does not matter if that swap is a partition or a file, it’s still on physical storage. If that storage happens to be SSD, then either way (partition or file) is the same. That being said, most Linux system write to disk continually anyhow, so the SSD doesn’t know or care whether the write is to swap, or to a cache folder, or a log file. To answer your question, a swap partition does not make any difference to a SSD.
Tyvm, this saved me from another “Na I won’t boot because you f’ed up my fstab” adventure
Or, from what I have already read befor it was mentioned:
Before starting, permanently disable zswap using the kernel parameter or sysctl knob
what is meant by sysctl knob?
Yeah, I read this before, but it only stated what and how, not when to use the one or the other - hence my questioning here - which is ofc answered in the meantime.
Ty all very much, it’s incredible that this forum - albeit way smaller than for example manjaro or arch forums - is SO much more helpfull… kudos to you!
If I am not mistaken, a “knob” refers to a kernel parameter which you can change using sysctl
.
You could use the pattern: # sysctl -w variable=value
to change a parameter at runtime.
Or drop a .conf file in /etc/sysctl.d
for the change to be applied at each boot.
For more:
https://man.archlinux.org/man/sysctl.8
https://man.archlinux.org/man/sysctl.d.5
I have this on the kernel commandline:
zswap.enabled=0
and my zram-generator.conf
looks like this
[zram0]
compression-algorithm=lz4
zram-size=16384
I have 64 GB RAM and I give 16 GB to swap. I use lz4 compression because it is faster than zstd.
This works perfectly under load. I tested memory consumption multiple times with stress-ng.
stress-ng --vm 3 --vm-bytes 100% -t 10m
My 5 cents:
I would just shrink the btrfs volume, add a dedicated swap partition of desired size (maybe 8 GB in your case) and call it a day.
A dedicated swap parition is much easier to manage than having the swap on the btrfs file system.
If you really want to leverage some RAM there’s always zswap.
That is wrong.
It doesn’t matter how much RAM you have, you always need swap. This is due to opportunistic swapping where the Linux kernel moves rarely used memory pages into swap to maximise cachable space in memory for frequently used pages. The Linux kernel always tries to maximise the use of RAM and it tries to cache as much data as possible to avoid unnecessary disk reads, which are super slow. Any RAM that is not used is RAM that is wasted, so the more RAM you have, the more of it the kernel will try to use. There is no such thing as “more than enough”.
This is especially important for servers, which have very long uptimes, but it’s also noticeable on desktop computers.
In short, always have some swap. You don’t need a lot, but some is certainly necessary. For 32 Gb of RAM or more, it seems to me that a swap file of 6 Gb is quite sufficient (of course, assuming you don’t use hibernate). That’s what I typically use, and it’s mostly based on a gut feeling and casual observation, not on any proper research, so take it with a grain of salt. Your use case might be different.
Actually, the best place to put a swap file (or swap partition, doesn’t matter) is on the fastest SSD you have. That will make a big impact in performance. Putting swap on slow devices makes no sense.
If you are worried about write cycles, you shouldn’t be, because normally, on systems with enough RAM, swap is a rather tiny contributor when it comes to all the data that is written to a drive. This is assuming you are using swap properly, and not as a substitution for the lack of RAM.
I think that is what people get wrong most of the time, they think that swap exists to compensate for not having enough RAM for some task. But swap is a really bad substitute for RAM, it’s terribly slow, even on super fast SSDs. It shouldn’t be used for that. If your system doesn’t have enough RAM to open a million tabs in a browser, you shouldn’t be opening a million tabs in a browser.
zwap saved my raspberry pi 3b, which has only one 1GB of RAM…
Ubuntu for raspberry Pi doesn’t have a swap by default, so I created one and enabled zwap.
So, in this situation, more life to old devices…
This works for raspberry pi 3b too:
https://ubuntu.com/blog/how-low-can-you-go-running-ubuntu-desktop-on-a-2gb-raspberry-pi-4
Zswap is not really swap, it’s a mechanism that reduces the usage of real swap, by compressing rarely used pages and then deciding whether they need to be swapped or whether they can stay in RAM. Compressing a memory page is typically much faster than writing it to disk, but requires more CPU use, so you trade CPU cycles for more efficient memory usage and less swap. But you still need a proper swap file or a swap partition. The only difference is that it will be used less if you have zswap enabled.
On Arch Linux, zswap is enabled by default on all kernels from the repos.
For this situation, it is helping a lot…
I was almost decommissioning it when I decided to try the zswap path, and more than a year now, it remains in use
I’m running:
Apache with php module
Samba as a domain controller in which my NAS is a domain member.
Freeradius for wifi network using samba users database, with post-auth rules based on groups.
Unifi controller with java 17 and mongod 3.6.8, which manages two devices, unifi mini and a unifi nanoHD.
Really a lot for 1GB of ram…
I just knew that. This seems to be a common wrong understanding for many users.
Learning never ends.
Now you got me thinking again… HAving both enough CPU as RAM makes the descision surprsingly more difficult… Zswap or Swapfile…
It’s a really simple decision, actually.
You need a swap file (or a swap partition, they do the same thing). You always need swap, like I said above. And zswap is already enabled out of the box on Arch Linux, at least if you use any of the standard kernels from the repo, so you don’t need to do anything about that.
So the answer is: use both. One is not a replacement for other.
The only real decision you need to make is whether to use a swap partition or a swap file, and how big to make it. Like I said above, I’d go with at least 6 GiB (if not using hibernate), and I prefer a swap file over a swap partition, because it makes it easy to change your mind about the size of it.
As @kresimir was pointing out already: zswap needs swap. This is not an either/or decision.
If you want to get rid of any true swap file or swap partition you can try zram resp. zram-generator.