Skip to content

Instantly share code, notes, and snippets.

@kekneus373
Created January 1, 2026 12:09
Show Gist options
  • Select an option

  • Save kekneus373/be4e080f0b44338e613a1478b36c3ca8 to your computer and use it in GitHub Desktop.

Select an option

Save kekneus373/be4e080f0b44338e613a1478b36c3ca8 to your computer and use it in GitHub Desktop.
[LowRam] Free Up Swap Space When Unneeded
#!/usr/bin/env bash
# Re-run the script as root if not already running as root
if [ "$EUID" -ne 0 ]; then
echo "This script requires root. Re-running with sudo..."
exec sudo "$0" "$@"
fi
free_mem="$(free | grep 'Mem:' | awk '{print $7}')"
used_swap="$(free | grep 'Swap:' | awk '{print $3}')"
echo -e "Free memory:\t$free_mem kB ($((free_mem / 1024)) MiB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MiB)"
if [[ $used_swap -eq 0 ]]; then
echo "Congratulations! No swap is in use."
elif [[ $used_swap -lt $free_mem ]]; then
echo "Freeing swap..."
swapoff -a
swapon -a
else
echo "Not enough free memory. Exiting."
exit 1
fi
# SOURCE: https://askubuntu.com/a/90399
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment