Sunday, 28 February 2016

Clean Cached Memory in Linux Server


1. Check Memory Usages:

Displays a line containing the totals memory in MB:
$ free -t -m

If you want to see the memory from the perspective of application you should look at +- buffers/cache line, the free column of this line shows you the memory that can be used for applications.


#################################################################
# Developed by Ashok Kumar
# 1:50 PM Monday, February 04, 2008
#################################################################
TOT=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USED=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
FREE=$[$TOT - $USED ]
LOG=/tmp/mem_monitor.log
echo > $LOG
SEND=0
if [ "$USED" -gt "0" ]; then
   USEDPERC=$[$USED * 100 / $TOT]
   echo "Used Percentage      : $USEDPERC %"
   TOTMB=$[$TOT / 1024 ]
   USEDMB=$[$USED / 1024 ]
   FREEMB=$[$TOTMB - $USEDMB ]
   #echo "Used Percentage : $USEDPERC"
   if [ "$USEDPERC" -gt "80" ]; then
     SEND=1
     STATUS="Warning"
     echo "------------------------------------------------------------------" >> $LOG
     echo `hostname`   >> $LOG
     echo "------------------------------------------------------------------" >> $LOG
     echo "Total Memory (MB)    : $TOTMB" >> $LOG
     echo "Used Memory (MB)     : $USEDMB" >> $LOG
     echo "Free Memory (MB)     : $FREEMB" >> $LOG
     echo "Used Percentage      : $USEDPERC %"  >> $LOG
     echo "------------------------------------------------------------------" >> $LOG
     if [ "$USEDPERC" -gt "95" ]; then
        STATUS="Critical"
     fi
   fi
fi
if [ "$FREEMB" -eq "0" ]; then
   SEND=1
   STATUS="Fatal"
   echo "------------------------------------------------------------------" >> $LOG
   echo "    No free memory available in " `hostname`    >>$LOG
   echo "------------------------------------------------------------------" >> $LOG
fi


$ top

can display the current CPU and memory usages of all processes.
To exit from top command type q key.

2. Clean Cache && Swap Memory

To free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_caches
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
3. Check memory usages in real-time

$ watch -n 1 cat /proc/meminfo
4. Free up memory either used or cached
$ sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

Reference:

http://www.cyberciti.biz/faq/linux-check-memory-usage/

http://www.upubuntu.com/2013/01/how-to-free-up-unused-memory-in.html

No comments:

Post a Comment