Skip to content

Instantly share code, notes, and snippets.

@Jekis
Last active August 17, 2024 01:06
Show Gist options
  • Save Jekis/6c8fe9dfb999fa76479058e2d769ee5c to your computer and use it in GitHub Desktop.
Save Jekis/6c8fe9dfb999fa76479058e2d769ee5c to your computer and use it in GitHub Desktop.
Empty swap. Clear swap. Move swap to RAM. Ubuntu.
#!/bin/bash
function echo_mem_stat () {
mem_total="$(free | grep 'Mem:' | awk '{print $2}')"
free_mem="$(free | grep 'Mem:' | awk '{print $7}')"
mem_percentage=$(($free_mem * 100 / $mem_total))
swap_total="$(free | grep 'Swap:' | awk '{print $2}')"
used_swap="$(free | grep 'Swap:' | awk '{print $3}')"
swap_percentage=$(($used_swap * 100 / $swap_total))
echo -e "Free memory:\t$((free_mem / 1024))/$((mem_total / 1024)) MB\t($mem_percentage%)"
echo -e "Used swap:\t$((used_swap / 1024))/$((swap_total / 1024)) MB\t($swap_percentage%)"
}
echo "Testing..."
echo_mem_stat
if [[ $used_swap -eq 0 ]]; then
echo "No swap is in use."
elif [[ $used_swap -lt $free_mem ]]; then
echo "Freeing swap..."
swapoff -a
swapon -a
echo_mem_stat
else
echo "Not enough free memory. Exiting."
exit 1
fi
@peschu123
Copy link

Hi what about this:

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

Found here & there are multiple options:

https://www.thegeekdiary.com/how-to-clear-the-buffer-pagecache-disk-cache-under-linux/

@rmintelligence
Copy link

Hi
This script is amazingly useful. I was just wondering how it could be modified to only run if the swap was used at more than some value, say 5%, Thanks in advance.

Tom

@movalex
Copy link

movalex commented Sep 25, 2019

I was just wondering how it could be modified to only run if the swap was used at more than some value, say 5%, Thanks in advance.

You can add additional if statement. Check it out:
https://gist.github.com/movalex/ba4e94eb0e03e4cff151162bc3e657aa

@themoonisacheese
Copy link

sorry for digging this up, but leaving a comment for posterity because i struggled for a while:
if the locale happens to be set to something other than en_US, free localizes the "Mem:" and "Swap:" line headings, meaning you have to edit the grep statements to match whatever is being returned by free, and if those line headings happen to have spaces in them, you also have to bump the awk argument number.
and for example, in fr_FR, swap is "Partition d'échange", so edit lines 7 and 8 to:

    swap_total="$(free | grep 'Partition d..change:' | awk '{print $3}')"
    used_swap="$(free | grep 'Partition d..change:' | awk '{print $4}')"

"Mem:" is unchanged in french so nothing changes ealier but other languages might run into trouble and the error message bash throws isn't explicit.
there's probably an easy way of doing this involving not grepping at all and simply parsing the appropriate line numbers (because free always returns the same things) but eh.

@xeruf
Copy link

xeruf commented Sep 11, 2022

@Longlv197
Copy link

Longlv197 commented Apr 23, 2023

hello, i want to run the script whenever an amount memory of swap correspond a specified amount. how can i solve this my case? can you help me? Assume that my memory swap's amount is 1900m, i want run the script automatically

@xeruf
Copy link

xeruf commented Apr 23, 2023

create a cronjob or use a watchdog like https://mmonit.com/monit/
or just shrink your swap lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment