Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@Jekis

This comment has been minimized.

Copy link
Owner Author

Jekis commented Aug 24, 2017

Usage

sudo ./toggle_swap.sh
@Balls0fSteel

This comment has been minimized.

Copy link

Balls0fSteel commented Aug 28, 2017

Thank you! (I have added a "safety net" to this, 250meg in my case. Because if it has only like 50mb and starts to unload swap, it may mean the machine is indeed in a ram heavy situation. I.e.: Unloading the swap could not come at a worse time. So basically it checks if it has the amount AND some more just in case. Percentage could not work too well in this case, so I just stick to a var + megabytes and just input it on my VPS.)

Also, it's really weird how any modern VPS (Vultr, DigitalOcean, Hetzner, OVH and so on) has super fast IO performance, but if you start using swap (let system handle it), it will really slow down. I mean I had dedicated servers with HDDs and default SWAP settings. They ate into swap, but never slow down this much.

@ciniset

This comment has been minimized.

Copy link

ciniset commented Oct 31, 2017

Thank you!
on line 5
free_mem="$(free | grep 'Mem:' | awk '{print $7}')"
should be
free_mem="$(free | grep 'Mem:' | awk '{print $4}')"

@Xerus2000

This comment has been minimized.

Copy link

Xerus2000 commented May 6, 2018

ciniset is right. Also, my free command shows its output in my language instead of english, which breaks the grep. I replaced it with this:
free | head -2 | tail -1 | awk '{print $4}'

I made a fork that works regardless of language and can be used as function from anywhere, see https://gist.github.com/Xerus2000/78f12b64c916bb9e6a82ea622f21d06c

@peschu123

This comment has been minimized.

Copy link

peschu123 commented Aug 27, 2018

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

This comment has been minimized.

Copy link

rmintelligence commented Nov 4, 2018

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

This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.