Centos Clear Cache Memory



In this video you will find how to Clear RAM Memory, Cache, Buffer and Swap memory in Linux 7Find some more videos on Linux:Squid Proxy with user authenticat. /+ 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. Try sync; echo 1 /proc/sys/vm/dropcaches. Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free. As this is a non-destructive operation and dirty objects are not freeable, the user should run 'sync' first.

Linux borrows unused RAM memory for disk caching due to this it looks like the system is low on memory. Usually you don’t need to do anything to clear this memory as Linux automatically manages the RAM and will allocate the cached memory when ever a application asks for the same.

However one of our customers had issue with free command output showing low on RAM and due to this there application health monitoring software was always on RED status and was sending notifications. So to avoid this scenario we created a shell script which checks for the free RAM percentage and clears the cache incase the free RAM hits the percentage mentioned in script.(default trigger: Free RAM =<15%ge of total memory)

Example from Redhat website:

In this example the total amount of available memory is 4040360 KB. 264224 KB are used by processes and 3776136 KB are free for other applications. Do not get confused by the first line which shows that 28160KB are free! If you look at the usage figures you can see that most of the memory use is for buffers and cache. Linux always tries to use RAM to speed up disk operations by using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices). This helps the system to run faster because disk information is already in memory which saves I/O operations. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. If your system runs for a while you will usually see a small number under the field “free” on the first line.

Centos Clear Cache Memory

Shell Script for Linux clear cache:

clear_RAM.sh:

Setting up the Linux clear cache script:

• Login as root user to application server VM.
• Copy the clear_RAM.sh file to any folder. (We have considered as /opt/scripts)
• Provide execute privilege by using below command

• Add the schedule to crontab following below commands

• Use contab -l command to check the cronjob is scheduled or not.

Sample Execution Output:

References:

Redhat : Memory Usage and Page cache

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

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.

# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, dentries and inodes.

Clear

Centos Clear Cache Memory Mac

# sync; echo 3 > /proc/sys/vm/drop_caches

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.

Centos Clear Cache Memory

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 after Every Hour via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

#!/bin/bash
# Note, we are using 'echo 3', but it is not recommended in production instead use 'echo 1'
sync; echo 3 > /proc/sys/vm/drop_caches

Set execute permission on the clearcache.sh file.

# chmod 755 clearcache.sh

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

Now set a cron to clear RAM cache after Every Hour. Open crontab for editing.

# crontab -e

Append the below line, save and exit to run it at Every Hour.

0 * * * * /root/clearcache.sh

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 every hour. After every hour 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.

# swapoff -a && swapon -a

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

Clear Cache Centos

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

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf 'n%sn' 'Ram-cache and Swap Cleared'

Centos

OR

$ su -c “echo 3 >’/proc/sys/vm/drop_caches’ && swapoff -a && swapon -a && printf ‘n%sn’ ‘Ram-cache and Swap Cleared'” root

Centos Clear Cache Memory Download

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

Centos Clear Cache Memory Windows 10

That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know, what you think is it a good idea to clear ram cache and buffer in production and Enterprise? Drivers sg310 usb devices.