Centos Clear Cache Memory



Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

Here I would like to share a script which you can use to clear your cache memory. IMPORTANT NOTICE: This is something one should not do as cache is used to speed up your work activity building a cache memory but still if you feel to clear your cache memory size then you can follow the following steps. The cache memory may be required to cleaned up at regular intervals and free up the disk space. The same can be achieved by creating a cronjob to automatically clear the cache memory using crontab. Step 1: Access the desired server through shell as a root or sudo user. First - the echo command you gave should be clearing the page cache. This is the same command given here. The page cache is managed by the kernel. It is not used (at least directly) by any process. The memory used by the page cache is available to be given to any process if the kernel decides to do that. The memory has not been leaked. /+ buffers/cache: 771 7056 Swap: 49. You will find difference between used and cache memory before and after clearing cache. In case of heavy memory consumption, you will set cron job to clear memory in hourly basis OR as per your convenience. Dentries or inodes cache which helps the administrator to throw away that script that allocated a ton of memory just to get rid of the cache. Below is the steps we need to follow to clear the pagecache / and memory cache. 0 - Will give the Kernel full control to the cache memory 1 - Will free the page cache 2 - Will free dentries and inodes.

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

2. Clear dentries and inodes.

3. Clear PageCache, dentries and inodes.

Explanation of above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

Clear Dns Cache Centos

If you have to clear the disk cache, the first command is safest in enterprise and production as “...echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above “...echo 3 >” in production until you know what you are doing, as it will clear PageCache, dentries and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk-cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

Set execute permission on the clearcache.sh file.

Now you may call the script whenever you required to clear ram cache.

Now set a cron to clear RAM cache everyday at 2am. Open crontab for editing.

Append the below line, save and exit to run it at 2am daily.

For more details on how to cron a job you may like to check our article on 11 Cron Scheduling Jobs.

Clear Cache Centos

Is it good idea to auto clear RAM cache on production server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2am. Everyday at 2am the script is executed and it flushes your RAM cache. One day for whatsoever reason, may be more than expected users are online on your website and seeking resource from your server.

At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Also you may add above command to a cron script above, after understanding all the associated risk.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

After testing both above command, we will run command “free -h” before and after running the script and will check cache.

Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

2. Clear dentries and inodes.

3. Clear PageCache, dentries and inodes.

Explanation of above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

If you have to clear the disk cache, the first command is safest in enterprise and production as “...echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above “...echo 3 >” in production until you know what you are doing, as it will clear PageCache, dentries and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk-cache.

Cache

Now we will be creating a shell script to auto clear RAM cache daily at 2am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

Set execute permission on the clearcache.sh file.

Now you may call the script whenever you required to clear ram cache.

Now set a cron to clear RAM cache everyday at 2am. Open crontab for editing.

Append the below line, save and exit to run it at 2am daily.

Clear yum cache centos

For more details on how to cron a job you may like to check our article on 11 Cron Scheduling Jobs.

Is it good idea to auto clear RAM cache on production server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2am. Everyday at 2am the script is executed and it flushes your RAM cache. One day for whatsoever reason, may be more than expected users are online on your website and seeking resource from your server.

At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Centos Clear Cache Memory Download

Also you may add above command to a cron script above, after understanding all the associated risk.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

After testing both above command, we will run command “free -h” before and after running the script and will check cache.





Comments are closed.