#!/bin/bash

_FREE=`free | grep 'Mem:' | awk '{print $4}'`
_ALL=` free | grep 'Mem:' | awk '{print $2}'`
_BORDER=`expr $_ALL / 6`

_ZRAMUSE=`cat /proc/swaps | grep ^/dev/zram | awk '{total = total + $4} END{print total}'`

_BEFORE=`free | grep Mem | awk '{print $4}'`

#if [ $_FREE -lt $_BORDER ] ; then
	if [ "$_ZRAMUSE" == "" ] ; then
		_ZRAMUSE=0
	fi
	if [ $_ZRAMUSE -gt 0 ] ; then
		sudo service zram-config stop
		/bin/sync && /bin/sync && sudo /sbin/sysctl -w vm.drop_caches=3
		sudo service zram-config start
	else
		/bin/sync && /bin/sync && sudo /sbin/sysctl -w vm.drop_caches=3
	fi
#fi

_AFTER=`free | grep Mem | awk '{print $4}'`

_RESULT=`expr $_AFTER - $_BEFORE`
_RESULT=`expr $_RESULT + $_ZRAMUSE`

yad --title "メモリの解放" --on-top --center --window-icon=computerjanitor --image=computerjanitor \
--text="${_RESULT}キロバイトを解放しました" 

exit 0
