Swap: Difference between revisions

Malix (talk | contribs)
Zswap swap cache: deprecate (reason: duplicate)
Clarify swap on zvol situation. Add notes to zram (recommendation to use userspace oom-killer, zswap instead of zram with writeback). Expand zswap section.
 
(One intermediate revision by one other user not shown)
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 79: Line 83:


This is probably because the writeback device has already been mounted elsewhere (e.g. as swap). To avoid this you need to do as the [[#Disable swap]] section says and make sure your writeback device is not being mounted as swap (this can happen due to <code>systemd-gpt-auto-generator(8)</code>). Do note that zram writeback does ''not'' respect the swap on-disk format and will destroy your existing swap header.
This is probably because the writeback device has already been mounted elsewhere (e.g. as swap). To avoid this you need to do as the [[#Disable swap]] section says and make sure your writeback device is not being mounted as swap (this can happen due to <code>systemd-gpt-auto-generator(8)</code>). Do note that zram writeback does ''not'' respect the swap on-disk format and will destroy your existing swap header.
== 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. 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.
Zswap is controlled by kernel parameters and can be enabled in your NixOS configuration by setting appropriate options through <code>boot.kernelParams</code>.
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
  boot.kernelParams = [
    "zswap.enabled=1" # enables zswap
    "zswap.compressor=lz4" # compression algorithm
    "zswap.max_pool_percent=20" # maximum percentage of RAM that zswap is allowed to use
    "zswap.shrinker_enabled=1" # whether to shrink the pool proactively on high memory pressure
  ];
</nowiki>
}}
{{note|If you use the <code>lz4</code> algorithm, you will also need to set {{nixos:option|boot.initrd.systemd.enable}} to true}}
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 183: 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 will will require experimentation.
[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 199: Line 228:
== ZFS and swap ==
== ZFS and swap ==


OpenZFS does not support swap on zvols nor do they support swap files on a ZFS dataset.
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 ==