How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

66 Responses

  1. tanasis says:

    Hello, i have centos 7.4 with cpanel 68.0.27 at my server (with HD drive).

    I see at “/var/log/cron” the command “sync; echo 3 > /proc/sys/vm/drop_caches” run every hour.

    How can i change it?

    I check it with “crontab -e” but i didn’t find there…

    • Ravi Saive says:

      @Tanasis,

      Must be cron set for that command, please check all contab entries and files carefully, like cron.hourly file under /etc directory.

      • tanasis says:

        I found it at /etc/cron.d/sync.

        I asked hosting company and suggest me to change the “echo 3 > …
        ” line to “echo 1 > ..” in order to only free the page cache instead of also freeing dentries and inodes.

        Before i removed the “echo 3 > …” i checked my RAM. It had:

        30717040 total, 25244176 free, 2631884 used, 2840980 buff/cache
        

        After 5 hours i have (i had removed “echo 3 > …“) :

        30717040 total, 21581380 free, 2164572 used, 6989240 buff/cache
        

        Am I OK, What do you suggest?

        • Ravi Saive says:

          @Tenasis,

          Yes, it’s now perfect the command now only erasing page cache and I think which is fine for you..

          • tanasis says:

            Ravi, i didn’t change the “echo 3” to “echo 1”.
            I had removed it…
            So,
            – the memory status when i had the “echo 3” was
            Mem30717040 total, 25,244,176 free, 2631884 used, 2840980 buff/cache

            After half day
            – the memory status without any “echo x” was
            Mem : 30717040 total, 244400 free, 2790844 used, 27681796 buff/cache
            The swap was 0

            It is OK?

            Shall i have to set “echo 1” every hour or leave is without “echo xxx” ?

          • Ravi Saive says:

            @Tanasis,

            For now, leave it as it is, and monitor for 2-3 days and see how it works, if it’s clear page cache every hour that’s fine else set echo with option.

          • tanasis says:

            I tried with “echo 1 > ..
            I have high CPU for 2-3 minutes because cron clear the buff/cache.

            I think, I will set “echo 3 > ..” every 4:00am.

            When i didn’t have the “echo” my free RAM was low (244400 free, 2790844 used, 27681796 buff/cache), but buff/cache was high and the swap was zero. Also avail Mem was high too.

          • Ravi Saive says:

            @Tanasis,

            It’s because I think the system in process of deleting Buffer/Cache from the system, during the process may be some RAM is used, once the cache is cleared all comes to normal..

  2. Champu says:

    I think Linux was hungry so that ate the RAM..:)

  3. Ali Yousefi says:

    Thanks for your useful posts.

    Unfortunately your web is not available in my country (Iran), so we have to use proxy for reading your great posts.

    • Ravi Saive says:

      @Ali,

      Could you tell us, what error you getting while accessing our website? it will be more helpful us to find why the site is getting blocked..

  4. Me says:

    @Stephanie

    Yes, very useful.

    You are getting permission denied because you are running sudo sync, semicolon, this means followed by the echo 1. So you’re running echo not as sudo anymore.

    How about a sudo -i followed by the command.

  5. Yuvakumar says:

    I used ‘sync; echo 3 > /proc/sys/vm/drop_caches‘ in my Linux (RHEL5) and taking more time to execute and come out, also i can see in top command bash process utilization is high of cpu.

    What to do next in this case or shall i kill the process ? can some one help me on it.

  6. Amarnath Datta says:

    Hi there, I am working on my site, hence I have been given this code to clear the cache /serverscripts/clear_cache.sh and now the problem is whenever there is a new article published on site the new article doesn’t appear on site unless I go to putty and go with that command /serverscripts/clear_cache.sh

    so please help me in this, as used some cache plugin but didn’t worked well even some plugin loaded my site RAM, so help me!

  7. Stephanie says:

    Not useful.

    “permission denied”

    Even when root or using sudo.

  8. Stephane says:

    On my Ubuntu 16.04 only the root user can write to /proc/sys/vm/drop_caches and a sudoer user cannot.

    On top of that, even the root user cannot grant write permission on this file.

    • Ian Pride says:

      I ran into the same issue, you can sudo a shell command and it should work (does for me Ubuntu 16.04 and Bodhi).
      E.g:

      $ sudo bash -c ‘sync; echo 3 > /proc/sys/vm/drop_caches‘
      or
      $ sudo sh -c ‘sync; echo 3 > /proc/sys/vm/drop_caches‘
      
  9. Abhinav Suryawanshi says:

    When you talk about clearing swap space you say after considering the risk.
    Can you please elaborate more what are the risks in clearing swap space?

  10. Akash Agnihotri says:

    this helped me a lot.. Great work dude !!

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.