Clear RAM Memory Cache, Buffer, and Swap Space on Linux | Hostbillo
GNU/Linux operating system has been executing the memory management quite efficiently. One can state that it has implemented memory administration even more than some of the other leading operating systems. The best part of the Linux operating system is that it renders the way to flush or free the ram cache if any process eats away your system’s memory unnecessarily. In this article, we will provide insights into the ways employing which you can free or clear cache Linux, Clear Cache in Linux, buffer, and swap space. Begin diving into the subsequent sections to get the details.
How to Clear Ram Cache in Linux?
With every kind of Linux system, you get three different choices in order to clear cache Linux. With each option, you do not have to intervene in any processes or services. They are explained below along with the command you need to use to implement them –
1. First choice you have is to Clear PageCache only –
2. The second option you have is to Clear dentries and inodes –
3. The third and the last choice you get is to Clear pagecache, dentries, and inodes –
In the above commands, sync functions to clean the file system buffer. Further, the command that is separated by “;” operates sequentially. Until each command is terminated, the shell waits before it implements the subsequent command in the sequence. Moreover, as the kernel documentation states, the drop_cache helps in cleaning the cache without destroying any application or service. And, the command echo does the task of writing to file.
You must note here that for clearing the disk cache, you should make use of the first command as it serves as the safest option in enterprise and production. The “…echo 1 > ….” plays a role in clearing PageCache only. On the other hand, you should not employ the third option “…echo 3 >” in production when you are not completely aware of what you are doing. This is because it flushes out the page cache, dentries, as well as inodes.
Free Buffer and Cache in Linux
There are times when you need to confirm whether the various settings that you apply in your system actually get implemented or not. This becomes more significant when it comes to the I/O-extensive benchmark. For confirming or checking the changes, you are required to clear the buffer cache. You have the option to drop cache in a way that we have discussed in the previous section without rebooting the System. This needs no downtime.
However, Linux operating system is created in such as way that it considers looking into the disk cache before disk. Thus, in case, Linux discovers any resource in the cache, your request can not reach the disk. So, cleaning the buffer and cache will enable your operating system to look for the resource on the disk as the disk cache becomes less beneficial. Further, with this, your system also happens to slow down for a few seconds when the cleaning of the cache is done. And, each resource that your operating system needs get loaded again in the disk cache.
Here we will teach you to create a shell script that can auto free the RAM cache on a daily basis at 2 am through a cron scheduler task.
Also Read: How to Setup Cron Job in Linux | Hostbillo
First of all, you need to create a shell script clearcache.sh and append the following lines.
# Note, we are using “echo 3”, but it is not recommended in production instead use “echo 1”
Next, you need to set execute permission on the clearcache.sh file utilizing the following command –
After entering this command, you can call the script as and when you wish to you free the ram cache. Further, you can set a cron to clear the RAM cache on a daily basis at 2 am. For doing so, you have to open crontab for editing and enter the command as –
Next, you have to append the below line. And then, save and exit to let it run at 2 am every day.
Clear RAM Cache on Linux Production Server?
No, you should not consider clearing the RAM cache on the Linux production server. For instance, if you get to schedule the script in order to clear ram cache on a daily basis at 1 am. Then, your script will execute every day at 1 am and flush your RAM cache. There can be times when someday for any reason more than expected users come online on your website to request resources from your server. And simultaneously, your scheduled script will run and free everything in the cache. So, at this time, when users attempt to fetch data from your disk, your server will crash and can corrupt the entire database. Thus, to avoid being a Cargo Cult System Administrator, you must consider clearing ram-cache only when needed.
How to Clear Swap Space in Linux?
In this section, you will know how to clear swap memory in Linux. In order to clear or flush Swap space, you can simply enter the following command –
Moreover, you can also append this command to a cron script once you get aware of all the related risks.
For combining both the commands into a single one for making an apt script that can clear RAM Cache as well as Swap Space, you need to enter as –
Once you get to test both the above commands, you can execute the command “free -h” before and after running the script and check the cache.
Read More: Check Python Version in Linux, Mac, & Window
While following this guide thoroughly, you must be able to comprehend how to clear cache in Linux, free buffer and cache, clear RAM cache, and flush swap space. If you encounter any issue or difficulty while performing any of these tasks on your system, you can reach out to us. Our Hostbillo technical professionals are available 24/7 to render the immediate response and expert solutions via different communication mediums.