That’s interesting but like… why? Wouldn’t swapping just cause slowness but not full scale instability/crashing?
Memory intensive applications and applications with memory leaks are most likely to have issues when the system is under stress, ie swapping. When resources are low and the system is swapping corner case bugs may be triggered. Also, ZFS is pretty memory intensive and swapping is bound to affect it
I use swap space and swappines but not as part of a vDev, once the system was online I have added a Intel Optane DC 4800X 375GB PCIe NVME 2.5 SSD, on version Truenas Scale 24.04.1 and create the swap and swappiness so 64 Gigs of Physical RAM + 375 Gbs with the optane, allows me to run the apps and if for example I have a VM that is highly intensive consumer of memory the app or machine goes to the optane, and uses the NVME for memory interchange transactions leaving the physical intact.
I’m running TrueNAS SCALE on a VM which only has 2 GiB and sadly cannot get more RAM. I ran into OOM issues when trying to upgrade the OS version so I had to enable swap on this remote backup system. First I create a swap block dataset on my boot-pool since it’s on an SSD:
sudo zfs create -V 8G -b "$(getconf PAGESIZE)" \
-o logbias=throughput \
-o sync=always \
-o primarycache=metadata \
-o secondarycache=none \
-o compression=zle \
boot-pool/swap
Then format it and swapon:
sudo mkswap /dev/zvol/boot-pool/swap
sudo /sbin/swapon /dev/zvol/boot-pool/swap
I used a pre init script to swapon on boot. It’s good enough for my use case. I was finally able to upgrade the OS. Seems to survive reboots and upgrades just fine.
I found the above command here: Reddit - The heart of the internet
Swapping to a zvol is the one thing that is dangerous. Because if ZFS is the cause of the memory pressure it may lead to a lock up.
If you cannot add more memory but can e.g. add another virtual disk, better use a proper Linux swap partition.
Ah, makes sense.
You have to set up a swapfile or zvol that gets re-added to your fstab on each boot. Despite what a lot of BSD people think, Linux works best with at least some swap enabled, and my solution is to have a pre-init script:
- Run mkswap on a dedicated zvol unless swaplabel already reports it as a swap volume. (Don’t use compression or hashing; the swap volume doesn’t benefit from that.)
- Configure zswap settings as you need them, but I recommend zstd as the zswap compressor, and the pagesize and block size of the zvol should match.
- Write the zswap location to /etc/fstab.
- Run `swapon -a` to activate swap.
The TrueNAS pre-init is a fine place to do this, and you can also set systctl to set vm.swappiness to 133-180, and vm.vfs_cache_pressure to around 300. I find it works well for me, but really only kicks in when I have a lot of apps or a VM running. If you notice thrashing, then RAM is a better choice, but there’s no real reason to be afraid of swap on a zvol if you don’t want the oomkiller to slap you every time some idle app wakes up to poll before going back to sleep.
Swap doesn’t replace RAM, but it does give Linux options for offloading unused pages at good compression rates until needed. The advice to avoid swap is, IMHO, outdated and based on OS biases that aren’t Linux-centric. Over-reliance on swap can cause problems, but a Linux system without swap is asking for trouble.I’ve been running mine this way for 2 years with zero problems, but YMMV.