▼
Clear Memory in Linux (specially for squid servers)
☺
If you are hosting SQUID proxy server on your Linux box, you may have noticed that SQUID as a proxy server is very greedy with memory. Over the period of time, it consumes large amount of memory , even in some cases 98-99%.
This script will clear the memory so that it be used by system or SQUID again. Make sure you are logged in with root user in order to continue.
Just create a new script and put it in any folder like /temp
Create File by following command
mkdir /temp cd /temp touch clearmem.sh touch /var/log/cron.log chmod +x clearmem.sh
Now edit the file by
nano /temp/clearmem.sh
.
And Paste the following code.
#!/bin/bash # Clear/Dump Cache Memory, to increase free memory, useful for SQUID proxy OR for general purposes. # Syed Jahanzaib / Email: aacable@hotmail.com Web: http://aacable.wordpress.com # Make sure you are Logged in with user root. # Setting variables for script set $(date) time=`date |awk '{print $4}'` # Action . . . sync; echo 3 > /proc/sys/vm/drop_caches # Add entry in /var/log/cron.log to make record. echo [Jz] Cache Memory Clear, Action Logged at $6-$2-$3 $time > /var/log/cron.log
.
Save, and Exit.
Now change the permission so it can be executed.
chmod 755 /temp/clearmem.sh
.
Now first execute
free -m
and note down the free memory,
Now execute the file by running …
/temp/clearmem.sh
check the log file by
cat /var/log/cron.log
Now again check the free amount from terminal with the command
free -m
.
And you will see the changes before and after running the file clearmem.sh
.
Howto schedule this file to run daily/hourly using ‘crontab’
To run this file on hourly basis, open terminal, (make sure you are logged in with the root user.
type
crontab -e
(if it asks for text editor, select nano ,)
Now paste following code …
@daily /temp/clearmem.sh # Run Daily in night at 00:00 hours
# @hourly /temp/clearmem.sh # Enable this if you want to run the script on hourly basis
Now, based on above selection, cron job will run this command at selected scheduled timings and clear any memory cache
Now some Explanation of what above script do . . .
It will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
* sync only makes dirty cache to clean cache. cache is still preserved.
* drop_caches doesn’t touch dirty caches and only drops clean caches. So to make all memory free, it is necessary to do sync first before drop_caches in case flushing daemons hasn’t written the changes to disk.
This is in generally a non-destructive operation.
.
Regard’s
Syed Jahanzaib
Filed under: Linux Related
