Clear Cache Memory In Linux

Posted on  by admin

Small memories on or close to the CPU can operate faster than the much larger main memory.Most CPUs since the 1980s have used one or more caches, sometimes in cascaded levels; modern high-end embedded, desktop and server microprocessors may have as many as six types of cache (between levels and functions). I am running a c tool on Linux machine. The tool crashes because of memory issues. I have checked the RAM details of my machine. Total used free shared buffers cached Mem: 7 50 0 15 12758 -/+ buffers/cache: 2972 12825 Swap: 8001 345 7655. Writing the appropriate value to the file /proc/sys/vm/dropcaches causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free. In order to clear PageCache only run: # sync; echo 1 /proc/sys/vm/dropcaches. As you know, every operating system uses cache, buffer, etc. To run its processes as well as increase the operating speed of the operating system. Accordingly, to manage and clear cache, buffer, and Swap memory in Linux, it provides a way for your users to delete them if necessary.

  1. How To Clear Cache Memory In Linux Server

What's going on?

Linux is borrowing unused memory for disk caching. This makes it look like you are low on memory, but you are not! Everything is fine!

Unable to clear cache memory in linux

Why is it doing this?

Disk caching makes the system much faster and more responsive! There are no downsides, except for confusing newbies. It does not take memory away from applications in any way, ever!

What if I want to run more applications?

If your applications want more memory, they just take back a chunk that the disk cache borrowed. Disk cache can always be given back to applications immediately! You are not low on ram!

Do I need more swap?


No, disk caching only borrows the ram that applications don't currently want. It will not use swap. If applications want more memory, they just take it back from the disk cache. They will not start swapping.

How do I stop Linux from doing this?

You can't disable disk caching. The only reason anyone ever wants to disable disk caching is because they think it takes memory away from their applications, which it doesn't! Disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! Therefore, there's absolutely no reason to disable it!

If, however, you find yourself needing to clear some RAM quickly to workaround another issue, like a VM misbehaving, you can force linux to nondestructively drop caches using echo 3 sudo tee /proc/sys/vm/drop_caches.

Clear Cache Memory In Linux

Why does top and free say all my ram is used if it isn't?

This is just a difference in terminology. Both you and Linux agree that memory taken by applications is 'used', while memory that isn't used for anything is 'free'.

But how do you count memory that is currently used for something, but can still be made available to applications?

You might count that memory as 'free' and/or 'available'. Linux instead counts it as 'used', but also 'available':

Memory that isYou'd call itLinux calls it
used by applicationsUsedUsed
used, but can be made availableFree (or Available)Used (and Available)
not used for anythingFreeFree

This 'something' is (roughly) what top and free calls 'buffers' and 'cached'. Since your and Linux's terminology differs, you might think you are low on ram when you're not.

How do I see how much free ram I really have?

To see how much ram your applications could use without swapping, run free -m and look at the 'available' column:

(On installations from before 2016, look at 'free' column in the '-/+ buffers/cache' row instead.)

This is your answer in MiB. If you just naively look at 'used' and 'free', you'll think your ram is 99% full when it's really just 47%!

For a more detailed and technical description of what Linux counts as 'available', see the commit that added the field.

When should I start to worry?

Clear Cache Memory In Linux

A healthy Linux system with more than enough memory will, after running for a while, show the following expected and harmless behavior:

  • free memory is close to 0
  • used memory is close to total
  • available memory (or 'free + buffers/cache') has enough room (let's say, 20%+ of total)
  • swap used does not change

Warning signs of a genuine low memory situation that you may want to look into:

  • available memory (or 'free + buffers/cache') is close to zero
  • swap used increases or fluctuates
  • dmesg grep oom-killer shows the OutOfMemory-killer at work

How can I verify these things?

See this page for more details and how you can experiment with disk cache to show the effects described here. Few things make you appreciate disk caching more than measuring an order-of-magnitude speedup on your own hardware! was presented by This site is available on GitHub for comments and PRs.

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.

Shell Script for Linux clear cache:

Setting up the Linux clear cache script:

• Login as root user to application server VM.
• Copy the 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:


Redhat : Memory Usage and Page cache

How To Clear Cache Memory In Linux Server

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