Swap: Difference between revisions

Pigs (talk | contribs)
No edit summary
Pigs (talk | contribs)
Add swappiness, zswap, and updated opening statement
Line 1: Line 1:
[[Category:Configuration]]
[[Category:Configuration]]
Swap provides additional virtual memory by extending physical RAM. This can be accomplished by using space on disk, such as [[#Swap file|swap file]] or [[#Swap partition|swap partition]], or through compression based methods like [[#Zram swap|zram]]. Additionally, [[#Zswap swap cache|zswap]] can act as a RAM-based compressed cache sitting in front of a traditional disk-based swap device.


= Configuration =
= Configuration =


Swap on NixOS is set with with one of two options <code>swapDevices</code> for traditional swap files or partitions, or <code>zramSwap.enable</code>  for compressed RAM-based swap. Add either option to your <code>/etc/nixos/configuration.nix</code>.
The following sections describe how to configure and manage swap on NixOS.


To check your current swap setup and usage, you can use the following command: <code>swapon</code>
To check your current swap setup and usage, you can use the following command: <code>swapon --show</code>


{{note| <code>nixos-generate-config</code> does not automatically generate a <code>swapDevices</code> entry if your system uses a swap file or <code>/dev/zram</code> for swap. For details, see the [https://github.com/NixOS/nixpkgs/pull/63083 discussion on GitHub]}}
{{note| <code>nixos-generate-config</code> does not automatically generate a <code>swapDevices</code> entry if your system uses a swap file or <code>/dev/zram</code> for swap. For details, see the [https://github.com/NixOS/nixpkgs/pull/63083 discussion on GitHub]}}
{{note|It's generally recommended to use either zram or zswap, but not both simultaneously, as they serve overlapping roles and may lead to suboptimal memory management behavior.}}


== Swap file ==
== Swap file ==


Add a swap file with the following:
A swap file provides swap space using a regular file on your filesystem, offering greater flexibility compared to a dedicated swap partition.
 
To add a swap file in NixOS, add the following:


{{file|/etc/nixos/configuration.nix|nix|
{{file|/etc/nixos/configuration.nix|nix|
Line 22: Line 28:
}}
}}


Size [https://search.nixos.org/options?channel=24.11&show=swapDevices.*.size is in megabytes]
The <code>size</code> value [https://search.nixos.org/options?channel=24.11&show=swapDevices.*.size is specified in megabytes]


== Swap partition ==
== Swap partition ==
Line 41: Line 47:


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.
== Zswap swap cache ==
[https://docs.kernel.org/admin-guide/mm/zswap.html Zswap] is a compress 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.
Unlike zram, zswap requires a disk-based swap device 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
  ];
</nowiki>
}}
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>


== Disable swap ==
== Disable swap ==
Line 74: Line 100:
}];
}];
</syntaxhighlight>
</syntaxhighlight>
== Adjusting swap usage behaviour ==
[https://docs.kernel.org/admin-guide/sysctl/vm.html#swappiness Swappiness] controls how aggressibely swap space is used. By default, Linux uses a swappiness value of 60. Higher values will make the kernel prefer swapping out idle processes sooner. 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.
{{file|/etc/nixos/configuration.nix|nix|
<nowiki>
boot.kernel.sysctl = {
  "vm.swappiness" = 10;
};
</nowiki>
}}
You can see your current swappiness level by <code>cat /proc/sys/vm/swappiness</code>.


== ZFS and swap ==
== ZFS and swap ==