In the world of Linux, efficient system performance is a key objective for administrators and users alike. A critical aspect of maintaining this performance is managing the system’s cached memory and buffer cache. These components are designed to store frequently accessed data and instructions, reducing the time it takes to access them from disk. However, over time, the cache can accumulate data that is no longer needed, potentially leading to reduced system efficiency and performance issues.

Advertisement

This guide provides instructions on how to clear the RAM memory cache on Linux/Unix systems using command-line methods.

How to Clear RAM Cache in Linux

Linux offers three methods for purging memory cache, allowing you to optimize system performance according to your specific needs.

  • For a comprehensive clearance of PageCache, dentries, and inodes from the cache memory, essentially purging all cached data, use the following command:
    
    sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
    
    
  • If your goal is to remove only dentries and inodes from the cache memory, execute this command:
    
    sync; echo 2 | sudo tee /proc/sys/vm/drop_caches
    
    
  • And, to specifically target and clear the page cache within the cache memory, apply this command:
    
    sync; echo 1 | sudo tee /proc/sys/vm/drop_caches
    
    

The `sync` command precedes each of these operations to ensure that all files currently held in the cache are first synchronized with the storage disk. The following command, indicated by the “;`, is then executed to clear the designated areas of cache memory as requested.

Automating Cache Clearance with Crontab

For periodic cache clearance, you can schedule a cron job. Use the following steps to set up an automatic cache flushing schedule:

  1. Open a terminal and run `crontab -e` to edit the crontab.
  2. Add the following entry to schedule the cache clearance:
    
    10 * * * * sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
    
    

    This cron job runs hourly, clearing the memory cache on your system.

However, on production servers, scheduling a cache clearance is generally not recommended as it may lead to data corruption or loss. Exercise caution before running such commands in a production environment.

Checking Cached Memory in Linux

To determine the amount of cache memory used by a Linux system, use the free command. The output, displayed in megabytes (MB), shows the cached memory usage. For example:

free -m 

This command will display the total, used, free, shared, buffer, and cached memory, with the last column indicating the amount of cached memory.


             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.

Conclusion

In conclusion, managing the memory cache in Linux is an essential task for optimizing system performance. While the cache plays a crucial role in speeding up data access, an overloaded cache can slow down the system. The methods outlined in this article provide straightforward ways to clear different types of cache, including PageCache, dentries, and inodes, ensuring that your Linux system runs efficiently. Additionally, the option to automate this process through cron jobs offers convenience, but it should be approached with caution, especially on production servers, to avoid potential data loss or corruption.

Finally, understanding how to monitor cache usage with the free command is a valuable skill for any Linux administrator or user, helping in making informed decisions about memory management. By regularly monitoring and managing the memory cache, you can maintain the optimal performance of your Linux

Share.

31 Comments

  1. 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.

  2. 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

  3. Hemant Nagpal on

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

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

  4. 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.

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

      • 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.

      • ABHAY KUMAR YADAV on

        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.

  5. 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

  6. 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?

  7. 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?

  8. 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.

  9. If i run this command also. still it holds cache memory. Do u have any solution or idea to clear the cache memory.?
    [root@jai ~]# 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
    [root@jai ~]#

  10. 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.

Exit mobile version