Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Tutorials»How to Clear Memory Cache in Linux

    How to Clear Memory Cache in Linux

    By RahulJune 21, 20223 Mins Read

    Memory cache is a critical component of any Linux system, helping to improve performance by storing frequently accessed data in a fast and easily accessible location. However, the cache can also become bloated over time, leading to degraded performance. Fortunately, it is relatively easy to clear the memory cache in Linux.

    Advertisement

    Clearing the memory cache is safe but not recommended every time. It can slow down the system performance as reading files from memory is much faster than persistent disk. Since it discards cached objects from memory, it may cost a significant amount of I/O and CPU to recreate the dropped objects.

    This tutorial will help you to clear the memory cache on Linux/Unix system via the command line.

    How to Clear Memory Cache on Linux

    There are three options available to clear the memory cache in Linux. Choose one of the below options to flush the Linux system cache memory as per your requirements.

    • Clear PageCache, dentries and inodes in cache memory. In short it will clear all the memory cache:
      sync; echo 3 | sudo tee /proc/sys/vm/drop_caches 
      
    • Clear dentries and inodes only in cache memory
      sync; echo 2 | sudo tee /proc/sys/vm/drop_caches 
      
    • Clear page cache only in cache memory
      sync; echo 1 | sudo tee /proc/sys/vm/drop_caches 
      

    Here the first command sync is used to synchronize all the in-memory cache files to the persistent storage. The next command is separated with a “;”. Once the first command is completed, the next command will be triggered to clear cache memory.

    Scheduleng the Clear Memory Cache usiing Crontab

    You can also schedule a corn job to clear the cache on a regular basis. Schedule the following in system crontab to automatically flush cache memory at a regular interval.

    Open a terminal and execute ‘crontab -e’ command to edit crontab:

    crontab -e 
    

    Append below entry to the file:

    0 10 * *  * sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
    

    The above cron will execute on every hour and flushes the memory cache on your system.

    On the production servers, it is not recommended to schedule a clear cache command. It can lead to data corruption or data loss. So beware before running the above command in a production environment.

    How to find Cached Memory in Linux

    Use free command to find out cache memory uses by Linux system. The output of the free command is like below

    free -m 
    
    Output
    total used free shared buffers cached Mem: 16050 15908 142 0 120 12953 -/+ buffers/cache: 834 15216 Swap: 0 0 0

    Here the last column is showing cached memory (12953 MB) on Linux system. The -m option is used to show output MB’s.

    clear cache memory in linux clear linux memory cache dentries flush linux memory flush linux memory cache inodes cache memory cache pagecache
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    An Introduction to the “./configure” Command: Compiling Source Code in Linux

    Getting Started with Linux Command line: The Beginning

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    View 31 Comments

    31 Comments

    1. Lester on November 25, 2022 9:48 am

      Hi, Is your schedule correct. You says it’s every hour but looks like it’s every day at 10am? I’m very new to Linux so I def could be wrong.

      Reply
    2. lbi on August 4, 2021 6:22 am

      Hello Rahul,
      Thanks for the info. It was useful to me, but you have an error on the third command :
      sync; echo 31 | sudo tee /proc/sys/vm/drop_caches
      It should be:
      sync; echo 1 | sudo tee /proc/sys/vm/drop_caches

      See https://www.kernel.org/doc/Documentation/sysctl/vm.txt as specified by Andreas.

      Reply
      • Rahul on August 4, 2021 12:31 pm

        Thanks, I have updated the article.

        Reply
        • Henry Omoroje on October 27, 2021 1:24 pm

          Hello Rahul.. Please I have a cache hitting almost the total RAM I have available. Is it safe to clear the cache? Considering this is a production environment?

          Reply
    3. Sandesh Mendon on June 27, 2020 4:35 pm

      Very good information, thank you

      Reply
    4. Balvinder Singh on March 5, 2020 6:38 pm

      Thanks for the tip, it was helpful.

      Reply
    5. me on November 7, 2019 8:35 am

      for who is having problems about permissions, you should do/try

      sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

      the article would be more complete if integrated with some indications on how to check the processes that “contributed” to produced that cached dimension

      Reply
    6. Hemant Nagpal on October 5, 2019 4:57 pm

      It gives me
      -bash: /proc/sys/vm/drop_caches: Permission denied

      Which permission i need to provide, i am running it with root user.

      Reply
    7. Bengt on January 16, 2019 1:56 pm

      It is a _bad_ idea to flush caches.

      Reply
    8. Francis Rodrigues on May 31, 2018 5:02 pm

      I think this “crontab -e” need to be in “root” permission, right? 0.o

      Reply
    9. Amiya kumar sahoo on March 7, 2018 11:10 am

      This command i have followed but ram cache is not clear. please help you

      Reply
    10. Vigneshram on January 18, 2018 9:32 am

      Hi Team,

      I have scheduled the below job everyday night 23:30 in Production.
      Cache is getting reduced and immediately after 30mts, again its coming to 31 GB where my overall
      RAM memory is 36 GB which is divided into 31 GB + 5 GB of swap.

      30 23 * * * sync; echo 3 > /proc/sys/vm/drop_caches

      Is there any way, we can able to identify this abnormality of memory growth as its a production server.

      Reply
      • arnold on April 11, 2018 12:36 pm

        Have you tried checking for inodes consumption by certain processes?? , are you running a webserver?? give me more details

        Reply
        • arnold on April 11, 2018 12:42 pm

          And remember clearing out your buffer and cache comes with consequences, especially if you’re running a webserver. It’s not a suggested action if you want to free up memory on production servers. I’ll suggest you look into migrating or cleaning out some data from the /var and /tmp directories rather than playing around with Cache and Buffer. As well try reducing the rotational frequency of logs being archived or stored into the /VAR directory, you can do so by editing some of the CRON config files.

          Reply
        • ABHAY KUMAR YADAV on May 16, 2018 1:14 pm

          Yes, tomcat server is running on that machine. And most of the memory is consumed by the buffer/cache. What could be the reason and how do we handle this situation in order to come out of this kind of problem.

          Reply
    11. mugesh on December 13, 2017 8:06 am

      nice

      Reply
    12. proxtik on December 11, 2017 9:19 pm

      Thanks! Clear and precise.

      Reply
    13. there on November 1, 2017 10:09 am

      hi

      Reply
    14. anil on September 23, 2017 2:27 am

      wow such a wonderful article so thanks for sharing

      Reply
    15. UAN Login on July 17, 2017 4:36 am

      very nice article. thanks for share it with us

      Reply
    16. Jimmy on July 14, 2017 10:56 pm

      Stated clearly, very helpful.

      Reply
    17. raju ginne on December 22, 2016 11:49 am

      How to flush DNS Cache,

      getting 2 errors 403 permission denied LAMP stack
      SFTP SSH for wordpress incorrect keys for the user

      your help appreaciated

      Reply
    18. Juan Pham on June 7, 2016 8:29 am

      I am getting the error that why my DNS server consume a lot of memory (run by CentOS 6.6). When this issue happen, the PCs which are assigned DNS IP of this server DNS, they can not browse internet? How can I fix this?

      Reply
    19. Wood on February 2, 2016 10:06 am

      My memory usage is getting iuncreaseds on Application server as well as DB server can I setup cron to clear the cache every 5 hours?

      WIl it increase performance?

      Reply
      • Andreas on February 2, 2016 12:57 pm

        Yes, it will DECREASE performance. Please refer to the kernel documentation at
        https://www.kernel.org/doc/Documentation/sysctl/vm.txt (search for drop_cache)

        Reply
    20. siva on January 29, 2016 10:01 am

      In my Centos server the cache memory keeps on increasing. If i run cronjob everyday, is it make any problem to the server RAM.

      It will make any performance issues.

      Reply
    21. Andreas on November 26, 2015 3:09 pm

      The advice of flushing cache is totally non-sense and only for debugging-purpose. Please refer to the Kernel documentation in

      https://www.kernel.org/doc/Documentation/sysctl/vm.txt

      Reply
    22. Jayakumar on July 8, 2015 4:35 am

      If i run this command also. still it holds cache memory. Do u have any solution or idea to clear the cache memory.?
      [[email protected] ~]# free -g && sync; echo 3 > /proc/sys/vm/drop_caches && free -g
      total used free shared buffers cached
      Mem: 141 123 18 17 0 17
      -/+ buffers/cache: 105 35
      Swap: 19 0 19

      total used free shared buffers cached
      Mem: 141 123 18 17 0 17
      -/+ buffers/cache: 105 35
      Swap: 19 0 19
      [[email protected] ~]#

      Reply
    23. T[m] on October 18, 2014 10:33 pm

      The system will not force active programs to disk in preference of keeping non-essential stuff in cache. It will drop the least recently used items from cache to make room for new programs rather than more your program to swap. It *will* move the inactive data for programs that are currently ‘running’ (i.e. in the wait queue) to swap if it has to. If your system is doing this it’s b/c whatever processes you are currently running are chewing up lots of RAM. Manually flushing your cache won’t do a thing. And this is LINUX not windows, Linux will intelligently use your RAM to improve your performance rather than leaving it idle. Forcing flushes is a silly thing to do unless you are running benchmarking programs multiple times.

      Reply
    24. Ted G on August 14, 2014 2:16 am

      Thanks! Clear and precise.

      Reply
    25. Sergio on July 10, 2014 8:14 pm

      muchas gracias!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Install PHP 8.2-7.4 on RHEL & CentOS Stream 9
    • How to Install MySQL 8.0 on RHEL & CentOS Stream 9
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.