Swap: Difference between revisions
Tag: Undo |
Axodentally (talk | contribs) Clarify swap on zvol situation. Add notes to zram (recommendation to use userspace oom-killer, zswap instead of zram with writeback). Expand zswap section. |
||
| Line 47: | Line 47: | ||
It is an alternative or complementary approach to swap disks, suitable for systems with enough RAM. In the event the system needs to swap it will move uncompressed RAM contents into the compressed area, saving RAM space while effectively increasing the available RAM at the cost of computational power for compression and decompression. | It is an alternative or complementary approach to swap disks, suitable for systems with enough RAM. In the event the system needs to swap it will move uncompressed RAM contents into the compressed area, saving RAM space while effectively increasing the available RAM at the cost of computational power for compression and decompression. | ||
{{Note|When using zram for swap, it is highly recommended to enable a userspace OOM killer such as systemd-oomd (via {{nixos:option|systemd.oomd.enable}}). Because zram acts as a block device with a hard capacity limit in RAM, the kernel's native OOM killer can sometimes fail to trigger in time under heavy memory pressure, leading to severe system lockups.<ref name="ChrisDown-zswapVsZram">https://chrisdown.name/2026/03/24/zswap-vs-zram-when-to-use-what.html</ref>}} | |||
See [https://search.nixos.org/options?query=zramSwap zramSwap] for a full list of available options and their descriptions. | See [https://search.nixos.org/options?query=zramSwap zramSwap] for a full list of available options and their descriptions. | ||
| Line 53: | Line 55: | ||
Zram supports writeback functionality, allowing idle or incompressible pages to be moved to a backing storage device rather than keeping it in memory. Currently, writeback can only use block storage devices (such as partitions) and does not support swap files. The backing partition must be manually created first, but does not require formatting. | Zram supports writeback functionality, allowing idle or incompressible pages to be moved to a backing storage device rather than keeping it in memory. Currently, writeback can only use block storage devices (such as partitions) and does not support swap files. The backing partition must be manually created first, but does not require formatting. | ||
{{Note|While zram writeback allows moving incompressible or idle pages to disk, it is not fully integrated into the kernel's memory management subsystem. If your goal is to have compressed RAM that automatically spills over to a physical disk when full, [[#Zswap swap cache|zswap]] is generally the better and more robust tool for the job <ref name="ChrisDown-zswapVsZram"/>.}} | |||
An example configuration: | An example configuration: | ||
| Line 82: | Line 86: | ||
== Zswap swap cache == | == Zswap swap cache == | ||
[https://docs.kernel.org/admin-guide/mm/zswap.html Zswap] is a compressed RAM cache for swap pages. It acts as a middle layer between system memory and a traditional disk-based swap device, storing compressed pages in RAM before optionally writing them out to disk-based swap if necessary. | [https://docs.kernel.org/admin-guide/mm/zswap.html Zswap] is a compressed RAM cache for swap pages. It acts as a middle layer between system memory and a traditional disk-based swap device, storing compressed pages in RAM before optionally writing them out to disk-based swap if necessary. Because zswap integrates directly into the kernel's memory management subsystem, it automatically and dynamically evicts the coldest pages to your backing disk when memory pressure rises. This graceful degradation provides a significant architectural advantage over zram for systems that have physical disk swap. | ||
Unlike zram, zswap requires a disk-based swap device or file to back it. | Unlike zram, zswap requires a disk-based swap device or file to back it. | ||
| Line 102: | Line 106: | ||
You can verify zswap's runtime status via <code>cat /sys/module/zswap/parameters/enabled</code> and inspect usage statistics with <code># grep -r . /sys/kernel/debug/zswap/</code> | You can verify zswap's runtime status via <code>cat /sys/module/zswap/parameters/enabled</code> and inspect usage statistics with <code># grep -r . /sys/kernel/debug/zswap/</code> | ||
A proper zswap configuration module is [https://github.com/NixOS/nixpkgs/pull/470366 currently under review]. | |||
== Disable swap == | == Disable swap == | ||
| Line 206: | Line 212: | ||
== Adjusting swap usage behaviour == | == Adjusting swap usage behaviour == | ||
[https://docs.kernel.org/admin-guide/sysctl/vm.html#swappiness Swappiness] controls how aggressively swap space is used, specifically how to free up memory when needed. By default, Linux uses a swappiness value of 60. Higher values will make the kernel prefer swapping out idle processes over dropping caches. Conversely lower values will try to avoid swapping as much as possible, keeping processes in RAM unless absolutely necessary. An optimal value is workload dependent and | [https://docs.kernel.org/admin-guide/sysctl/vm.html#swappiness Swappiness] controls how aggressively swap space is used, specifically how to free up memory when needed. By default, Linux uses a swappiness value of 60. Higher values will make the kernel prefer swapping out idle processes over dropping caches. Conversely lower values will try to avoid swapping as much as possible, keeping processes in RAM unless absolutely necessary. An optimal value is workload dependent and will require experimentation. | ||
{{file|/etc/nixos/configuration.nix|nix| | {{file|/etc/nixos/configuration.nix|nix| | ||
| Line 222: | Line 228: | ||
== ZFS and swap == | == ZFS and swap == | ||
OpenZFS does not support swap on zvols | OpenZFS does not support swap files on a ZFS dataset. Swap on zvols is technically supported, but does not support resume from hibernation and can lead to system lockup in high memory pressure situation and is thus not recommended (see [https://github.com/openzfs/zfs/issues/7734 OpenZFS issue #7734] and the [https://wiki.archlinux.org/title/ZFS#Swap_volume arch wiki on Swap Volumes in ZFS]). | ||
Instead you should set up a swap partition or swap file on a non-ZFS filesystem.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref> | Instead, you should set up a swap partition or swap file on a non-ZFS filesystem.<ref>https://utcc.utoronto.ca/~cks/space/blog/solaris/ZFSForSwapMyViews</ref> | ||
== Using swap files on Btrfs == | == Using swap files on Btrfs == | ||