Memory cache is a vital element in Linux systems, serving to enhance overall system efficiency by temporarily storing frequently used data in a swift and accessible area. However, over time, this cache can become overburdened, leading to reduced system performance. Clearing the memory cache in Linux is a simple process and can be done quite easily.
While clearing the memory cache is generally safe, it is not always advisable to do so regularly. This is because accessing data from the cache is quicker than retrieving it from a persistent disk. Clearing the cache removes cached data from memory, which might require substantial I/O and CPU resources to regenerate the lost data.
This guide provides instructions on how to clear the memory cache on Linux/Unix systems using command-line methods.
Methods to Clear Memory Cache in 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.
- To clear PageCache, dentries, and inodes in the cache memory, effectively cleaning all memory cache:
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches
- To clear only dentries and inodes in cache memory:
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches
- To clear only the page cache in cache memory:
sync; echo 1 | sudo tee /proc/sys/vm/drop_caches
sync command is used to synchronize all cached files in memory with the persistent storage. The next command, following the
;, executes after the completion of the sync command, clearing the specified cache memory.
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:
- Open a terminal and run `crontab -e` to edit the crontab.
- 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:
This command will display the total, used, free, shared, buffer, and cached memory, with the last column indicating the amount of cached memory.
Outputtotal 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.
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