Syed Jahanzaib Personal Blog to Share Knowledge !

January 10, 2014

Clear Memory in Linux Box

Filed under: Linux Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 11:23 AM


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 /var/log/cron.log
chmod +x

Now edit the file by

nano /temp/


And Paste the following code.

# Clear/Dump Cache Memory, to increase free memory, useful for SQUID proxy OR for general purposes.
# Syed Jahanzaib / Email:  Web:
# Make sure you are Logged in with user root.

# Setting variables for script
set $(date)
time=`date |awk '{print $4}'`

# Action . . .
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/


Now first execute

free -m

and note down the free memory,

Now execute the file by running …


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


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.

crontab -e

(if it asks for text editor, select nano ,)

Now paste following code …

@daily  /temp/       # Run Daily in night at 00:00 hours
@hourly  /temp/   # 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.

Syed Jahanzaib


  1. Sir………this configuration required new squid installation or running bases

    Comment by Salman — January 10, 2014 @ 3:16 PM

  2. I really liked the picture !!!!😀 .. nice work

    Comment by ammar — January 11, 2014 @ 11:11 AM

  3. thank you bro , it`s really help with squid cache server coz I used 16G of ram after 24 hrs no more free memory

    Comment by alfanet1 — January 11, 2014 @ 1:44 PM

  4. Sir……… to run this file every 12:00 hours after ?
    ALLAH aap ko jaza e khair deain……!

    Comment by Salman — January 13, 2014 @ 2:17 PM

  5. Sir……… to run this file every 12:00 hours after ?
    At 12:00 am & again 12:00 pm
    ALLAH aap ko jaza e khair deain……!

    Comment by Salman — January 14, 2014 @ 2:26 PM

  6. hello bro , how to run this script every 2 hrs ?
    coz every 24 too much and my rams almost used by squid

    Comment by alfanet1 — January 16, 2014 @ 10:15 PM

  7. Sir…………is this correct commad for crontab every 12 hours……?
    * */12 * * * /temp/

    Comment by Salman — January 19, 2014 @ 3:19 AM

  8. Salam Alikum

    thank you bro

    Comment by alfanet1 — January 19, 2014 @ 2:17 PM

  9. Sir………………its good working for crontab every 12 hours
    * */12 * * * /temp/
    Thanks a lot of you Jahanzaib Bhai……………!

    Comment by Salman — January 20, 2014 @ 4:47 PM

  10. Dear Sir,

    I need ur help, can we install mikrotik on IBM x3550 m4 server ?

    Comment by Fawad Ahmad — January 23, 2014 @ 1:49 PM

    • Yes you can. But As far as I know, Mikrotik dont support SCSI disks.
      You can also use virtualization to host mikrotik along with other OS in single box. its more convenient and good for management and backup purposes.

      Comment by Syed Jahanzaib / Pinochio~:) — January 27, 2014 @ 8:48 AM

  11. Reblogged this on Henri Sekeladi – Sebuah Catatan Perjalanan and commented:
    Add your thoughts here… (optional)

    Comment by rihend — January 29, 2014 @ 5:55 PM

  12. i use it really WOW thanks ………

    Comment by Mostafa Mohamed — January 30, 2014 @ 2:20 AM

  13. thanx
    really working and helpful

    Comment by ali sameer — March 19, 2014 @ 1:24 AM

  14. Sir……..How to Auto Clear Cache by schedule of Squid Proxy Server ?
    kindly..! Sir..write on article with script…….!
    ALLAH aap ko jaza e khair dey……..

    Comment by Salman — January 22, 2015 @ 1:31 AM

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: