Shell/Bash: clear swap memory linux Example

By

Shell/Bash Example: This is the "clear swap memory linux" Example. compiled from many sources on the internet by SimpleTutorials.org

clear swap memory linux

sudo swapoff -a; sudo swapon -a

clear ram linux

sudo sync; echo 3 > /proc/sys/vm/drop_caches

linux swap flush

#!/bin/bash

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 "No swap is in use."
elif [[ $used_swap -lt $free_mem ]]; then
    echo "Freeing swap..."
    sudo swapoff -a
    sudo swapon -a
else
    echo "Not enough free memory. Exiting."
    exit 1
fi



* Summary: This "clear swap memory linux" Shell/Bash Example is compiled from the internet. If you have any questions, please leave a comment. Thank you!

Others