← Wed, Sep 23 digest

Swap, ZRAM, Zswap and Hibernate on NixOS

read article ↗ hn thread ↗

summary

Using a disk-backed swap partition or file is essential for system hibernation on NixOS, and this setup should be paired with zswap for optimal memory management. While zram is a popular alternative, it is incompatible with hibernation because it exists only in RAM. A high swappiness value of 100 is recommended for SSD-based workstations to balance filesystem paging and swapping efficiently.

key points

  1. Hibernation requires a physical swap space on disk, meaning swap files or partitions are necessary to save the system state before power-off. While swap files are more flexible, they can complicate the resume process due to the need for specific offsets in the initramfs.
  2. Zswap acts as a compressed cache for pages being moved to disk, reducing disk I/O and improving performance compared to using a disk-backed swap alone. In contrast, zram creates a separate compressed RAM drive, which cannot be used as a backup for hibernation data.
  3. The choice of compression algorithm for zswap, such as zstd, provides a better compression ratio than lz4, allowing more pages to fit into memory and reducing the reliance on slow disk access.

who should read it

NixOS users who want to enable hibernation and are looking for the best configuration for memory management using zswap and zram.

discussion positive

consensus

Users with large amounts of RAM prefer disabling swap or setting swappiness to 0 to avoid micro-stutters and unnecessary disk I/O. While some acknowledge the theoretical benefit of swapping stale memory for disk cache, the practical experience of latency outweighs these gains for high-memory systems. There is also a specific technical warning against using swap files on COW filesystems like btrfs due to performance and maintenance trade-offs.

top insight

Swap files on btrfs filesystems are problematic because they can cause the filesystem scrub process to skip block groups containing the swapfile, potentially leaving other files in those groups unchecked for errors. This makes a dedicated swap partition a significantly safer and more performant choice for btrfs users compared to ext4 or XFS. This detail adds a critical maintenance perspective beyond basic performance discussions.

Generated by an LLM from the article and thread. Check the sources before relying on it.