Skip to content

Instantly share code, notes, and snippets.

@DoctorD90
Created August 14, 2015 13:25
Show Gist options
  • Save DoctorD90/4afb55c379174030200e to your computer and use it in GitHub Desktop.
Save DoctorD90/4afb55c379174030200e to your computer and use it in GitHub Desktop.
My cache memory cleaner
#!/bin/bash
function mem_help() {
echo "*Clean up the memory*"
echo "Usage: $0 [Options]"
echo "Options:"
echo " -q, --quiet Run without printing output"
echo " -h, --help Print this help"
}
function mem_free() {
sync
echo 3 > /proc/sys/vm/drop_caches
}
function mem_graph() {
type=(tot used free)
#Set OLD usage
declare -A mem_old=( [cmd]=$(free -t | grep Total:) )
n=2
for t in "${type[@]}"; do
mem_old["$t"]=$(echo "${mem_old[cmd]}" | awk '{ print $'"$n"' }')
#echo "mem_old_"$t" = ${mem_old["$t"]}"
n=$((n + 1))
done
#Empty cache
mem_free
#Set NOW usage
declare -A mem_now=( [cmd]=$(free -t | grep Total:) )
n=2
for t in "${type[@]}"; do
mem_now["$t"]=$(echo "${mem_now[cmd]}" | awk '{ print $'"$n"' }')
#echo "mem_now_"$t" = ${mem_now["$t"]}"
n=$((n + 1))
done
#Prepare MBytes size
for t in "${type[@]}"; do
mem_old["$t"]=$((mem_old["$t"] / 1024))
#echo "mem_old_"$t" = ${mem_old["$t"]}"
mem_now["$t"]=$((mem_now["$t"] / 1024))
#echo "mem_now_"$t" = ${mem_now["$t"]}"
done
echo "MByte OLD GAIN NOW"
echo "Free: ${mem_old[free]} $((mem_now[free] - mem_old[free])) ${mem_now[free]}"
echo "Used: ${mem_old[used]} $((mem_old[used] - mem_now[used])) ${mem_now[used]}"
echo "====== ===== ===== ====="
echo "Total: ${mem_old[tot]} $((mem_old[tot] - mem_now[tot])) ${mem_now[tot]}"
}
if [[ "$#" -eq 0 ]]; then
mem_graph
else
case "$1" in
-q) mem_free ;;
--quiet) mem_free ;;
*) mem_help ;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment