AIO Ep21. What Is Linux Swap, and Should You Enable It?简体中文 / [English]<br>Home<br>About<br>Writing<br>Category<br>Friends<br>Search
Previous post Next post Back to top Share post
1. Preface<br>2. What Is Swap<br>3. Swap Issues I Encountered3.1. Windows VM Memory Problems<br>3.2. PVE Host Freezing (zram/zswap Issues)<br>3.3. Swap Thrashing<br>3.4. Swap Encryption
4. Should You Use Swap?
This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to the original Chinese version. I am continuously working to improve the translation.
Preface<br>Due to the sharp rise in memory prices over the past year, I couldn’t afford more RAM after upgrading my platform. My current 16GB of DDR5 is barely enough for daily use, but unexpected OOM kills happen when running heavier workloads.<br>I tried enabling swap to mitigate OOM issues, but ran into various freezes and crashes during configuration. While searching online, I found that people’s understanding of swap is all over the place—full of misconceptions and inaccurate information. Hence, this article.<br>What Is Swap<br>Let’s start with two relatively reliable sources: the archlinux Wiki and a blog post titled In defence of swap: common misconceptions.<br>In short: swap is a temporary backing store for memory in Linux. It allows the kernel to temporarily move cold anonymous pages (pages dynamically allocated during program execution) to swap, freeing up precious physical memory for processes that need it now—thus improving the utilization of physical RAM.<br>A common belief is that swap can extend physical memory. This isn’t quite accurate. When your working set exceeds physical memory, you’ll experience swap thrashing—constant swapping in and out—leading to a severe performance drop. In extreme cases, the system may freeze entirely. This is not what swap is designed for.<br>Swap has an important parameter called swappiness (/proc/sys/vm/swappiness), which ranges from 0 to 200. It controls whether the kernel, under memory pressure, prefers to drop file cache (file pages) or swap out anonymous pages. The ideal setting depends on your hardware. For example, on a Linux system with an SSD as the boot drive, re-reading a file and reading a swapped anonymous page have similar costs, so a value like 100 (indicating equal cost) is reasonable.<br>BTW, the top-voted answer on AskUbuntu about swappiness is outdated and inaccurate<br>Swap Issues I Encountered<br>Unfortunately, things weren’t that simple. After enabling swap, my Proxmox VE setup ran into several issues, and I spent quite some time debugging.<br>Windows VM Memory Problems<br>After enabling swap on the PVE host, for some unknown reason, one of my Windows VMs started exhibiting random errors. Nearly every program—from LogonUI, taskmgr, and explorer to Firefox, QQ, and Navicat—would crash or pop up memory-related error dialogs when memory usage was high. The whole system would frequently freeze, while other Linux VMs and the host remained unaffected.<br>Sample crash dialog<br>I tried disabling memory ballooning, adjusting VM memory up and down, updating guest drivers, upgrading the host kernel, enabling the page file inside Windows, and more—nothing worked.<br>Eventually, I solved all issues by editing the /etc/pve/qemu-server/.conf config file and adding a QEMU memlock option to force the VM’s memory to never be swapped:<br>args: -overcommit mem-lock=on<br>PVE Host Freezing (zram/zswap Issues)<br>Initially, because my SSD uses ZFS—and swap on ZFS has known issues—I opted for zRAM.<br>zRAM acts as a block device that compresses data and stores it back in RAM. A common use case is using zRAM as swap: cold memory data gets compressed and stored in-place in RAM, eliminating the need for disk-based swap. This is similar to the “extended memory” feature often found in Android.<br>At first, I set up a 16GB zRAM device (equal to my physical RAM size) as swap. Initially, everything seemed fine—system ran smoothly.<br>However, after some time, I noticed that under significant memory pressure or spikes, the entire system would become extremely sluggish.<br>So bad, in fact, that PVE’s default watchdog-mux.service would fail to feed the watchdog for 10 seconds straight, causing the entire system to reboot. WTF—I was trying to avoid OOM kills, and now I’m crashing the whole system. Even with the watchdog disabled, the system would freeze completely, with SSH connections timing out for over an hour.<br>10<br>11<br>12<br>13<br>14<br># Reproduction: enable zram first<br>modprobe zram<br>zramctl /dev/zram0 --algorithm zstd --size 16G<br>mkswap /dev/zram0<br>swapon -p 100 /dev/zram0
# Monitor memory pressure (PSI) on host. For example, full 1.54 means processes spent 1.54% of the last 10 seconds waiting for memory<br>watch -n0.5 cat /proc/pressure/memory<br># some avg10=2.78 avg60=2.84 avg300=0.89 total=103627769<br># full avg10=1.54 avg60=1.64 avg300=0.53 total=94869096
# Generate memory load from different VMs<br>stress -c 10 --vm 2 --vm-bytes 4G --timeout 60s<br># The data generated by stress is...