#!/usr/bin/env bash # # Fix high swap usage on Proxmox without buying RAM # https://www.zadran.net/guides/fix-high-swap-usage-on-proxmox # Generated from the guide, last updated 2026-09-16. # # NO WARRANTY. This script is provided as is. You are responsible for # understanding what it does before running it, and for the consequences. # Take a backup first. See https://www.zadran.net/disclaimer # # Destructive steps are left commented out on purpose -- uncomment them # only once you have understood what they do. set -euo pipefail # ---- Set these for your environment ---- # The VM or container you are working on, as shown by qm list or pct list. VMID='100' # What you are resizing that guest to. 6144 is 6 GiB. NEW_MEM='6144' free -h swapon --show { for id in $(qm list | awk "NR>1 {print \$1}"); do qm config $id; done; \ for id in $(pct list | awk "NR>1 {print \$1}"); do pct config $id; done; } \ | awk "/^memory:/ {sum += \$2} END {printf \"assigned to guests: %.1f GiB\n\", sum/1024}" awk "/MemTotal/ {printf \"installed: %.1f GiB\n\", \$2/1048576}" /proc/meminfo qm agent ${VMID} ping && qm config ${VMID} | grep -E "^(memory|balloon|agent):" Get-Counter "\Memory\Committed Bytes" | Select-Object -ExpandProperty CounterSamples free -h qm set ${VMID} --memory ${NEW_MEM} qm set ${VMID} --balloon 0 pct set ${VMID} --memory ${NEW_MEM} qm set ${VMID} --onboot 0 qm shutdown ${VMID} systemctl status ksmtuned systemctl enable --now ksmtuned cat /sys/kernel/mm/ksm/pages_sharing # !! DESTRUCTIVE -- review, then uncomment to run: # swapoff -a && swapon -a free -h cat /sys/module/zfs/parameters/zfs_arc_max free -h { for id in $(qm list | awk "NR>1 {print \$1}"); do qm config $id; done; \ for id in $(pct list | awk "NR>1 {print \$1}"); do pct config $id; done; } \ | awk "/^memory:/ {sum += \$2} END {printf \"assigned: %.1f GiB\n\", sum/1024}"