How Do I Clear the Cache in Linux?
Every Linux system has three methods for clearing the cache without interfering with any programs or services.
- Only clear the PageCache.
sync; echo 1 > /proc/sys/vm/drop caches
- Remove all dentries and inodes.
sync; echo 2 > /proc/sys/vm/drop caches
- Delete the page cache, directories, and inodes.
sync; echo 3 > /proc/sys/vm/drop caches
Explanation of the preceding command.
The file system buffer will be flushed when you run the sync. Commands separated by “;” are executed consecutively. Before running the next command in the sequence, the shell waits for each command to complete. As stated in the kernel documentation, writing to drop cache will remove the cache without terminating any applications or services; command echo will accomplish the work of writing to file.
If you need to empty the disc cache, the first command is the safest incorporate and production environment since “…echo 1 >….” only clears the PageCache. It is not suggested to use the third option above “…echo 3 >” in production unless you are sure what you’re doing since it will delete page cache, dentries, and inodes.
Is it a good idea in Linux to liberate Buffer and Cache that may be used by the Linux Kernel?
Linux’s Free Buffer and Cache
When you are experimenting with different options and want to see if they are truly implemented, especially on the I/O-extensive test, you may need to clean the buffer cache. You may drop cache as described above without resetting the system, requiring no downtime.
Linux is structured in such a way that it searches the disc cache before searching the disc. If it discovers the resource in the cache, the request is not sent to the disc. When we clear the cache, the disc cache becomes less helpful since the OS will hunt for the resource on the drive.
Furthermore, the system will be slowed for a few seconds while the cache is cleared and every resource requested by the OS is reloaded in the disc cache.
Now we’ll write a shell script that will clear RAM cache on a daily basis at 2 a.m. using a cron scheduler process. Add the following lines to the shell script clearcache.sh.
!/bin/bash \s# Note, we are using “echo 3”, but it is not recommended in production instead use “echo 1”
echo “echo 3 > /proc/sys/vm/drop_caches”
Give the clearcache.sh file execution permission.
# chmod 755 clearcache.sh
You may now run the script whenever you need to clean the ram cache.
Set a cron job to delete the RAM cache every day at 2 a.m. Edit the crontab file.
# crontab -e
Add the following line, save, and exit to have it run at 2 a.m. every day.
0 2 * * * /path/to/clearcache.sh
How Do I Clear the RAM Cache on a Linux Production Server?
No, it isn’t. Consider the following scenario: you have set the script to clean the ram cache every day at 2 a.m. Every day at 2 a.m., the script runs and flushes your RAM cache. For whatever reason, more than expected people may be online on your website and requesting resources from your server one day.
At the same moment, the scheduled script executes and clears the cache. All users are now retrieving data from the disc. It will cause the server to crash and damage the database. So clean ram-cache only when necessary, and keep track of your steps, or you’ll be a Cargo Cult System Administrator.
How Do I Free Up Swap Space in Linux?
If you wish to free up Swap space, use the command below.
# swapoff -a && swapon -a
You may also add the above command to a cron job once you’ve considered all of the hazards.
Now we will combine both of the above instructions into a single command to create a good script to clear RAM Cache and Swap Space.
# echo 3 > /proc/sys/vm/drop caches && swapoff -a && swapon -a && printf ‘\n%s\n’ ‘Ram-cache and Swap Cleared’
$ su -c “echo 3 >’/proc/sys/vm/drop caches’ && swapoff -a && swapon -a && printf ‘\n%s\n’ ‘Ram-cache and Swap Cleared'” root
After verifying both of the above commands, we will execute “free -h” before and after executing the script to check the cache.
That’s it for now; if you like the post, please leave us a comment to let us know what you think is a good solution to clean ram cache and buffer in production and enterprise.