1. Introduction

The use of Linux file system buffer and cache makes I/O operations faster. It should be evident that when we talk about how to clear buffer and cache, we’ll unquestionably examine how the cache works.

So, first, we’ll talk about how the Linux file system cache works. Then, we’ll demonstrate how to monitor the cache usage and, finally, how to empty the buffer and cache. We’ll likewise do some basic exhibition analysis to check that the cache is working true to form and that the cache flush and empty methodology is additionally filling in true to form.

2. How Linux File System Cache Works

The cache in Linux is called Page Cache. It is that certain amount of system memory that the kernel reserves for caching the file system disk accesses. This is to make overall performance faster. During Linux read system calls, the kernel checks if the cache contains the requested blocks of data. If it does, then that would be a successful cache hit. The cache returns this data without doing any I/O to the disk system.

The Linux cache approach is called a write-back cache. This means first, the data is written to cache memory and marked as dirty until synchronized to disk. Then, the kernel maintains the internal data structure to optimize which data to evict from the cache when the cache demands any additional space. For example, when memory usage reaches certain thresholds, background tasks start writing dirty data to disk, thereby emptying the memory cache.

2.1. What’s in the Buffer and Cache

The terms caching and buffering are sometimes used interchangeably. But still, one must know the inherent facts that differentiate the buffer from the cache.

Under a broad classification, a buffer is an area of memory used to temporarily store data while being moved from one place to another. Buffers are the disk block representation of the data that is stored under the page cache. In addition, the buffer contains the metadata of the files or data which resides under the page cache.

On the other hand, a cache is a temporary storage area to store frequently accessed data for rapid access.

2.2. Using the free Command to View Buffer and Cache Usage

Linux free command helps analyze the amount of system memory and the memory allocated to buffer and cache:

# free -m
             total        used        free      shared  buff/cache   available
Mem:          7457         209         6580          0         667        7004
Swap:           0           0           0

What we see from the free command is that there is 7.5 GB of total RAM. Of this, only 209 MB is in use, and 6.5 GB is free. Buffer and cache are using this 667 MB. Now, let’s try to increase that number by running a command to generate and read a file of 1 GB:

# dd if=/dev/random of=/root/data_file count=1400000
# for i in `seq 1 10`; do echo $i; cat data_file >> large_file; done

Now, we’ll make sure to read this 1 GB file and check the usage with the free command again:

# cat large_file > /dev/null; free -m
             total        used        free      shared  buff/cache   available
Mem:          7457         206         5515          0        1735        6992
Swap:           0           0           0

We can see that the buffer and cache usage has gone up from 667 to 1735 MB, a roughly 1 GB increase in its use.

2.3. Experimental Verification of How Cache Works

Let’s do some performance validation to see how the kernel reads a file using the cache? Let’s read a file and write it back to /dev/null to test how long it takes to read the file from the disk. We’ll time it with the time command:

# ls -l                                                                                                                              
total 525448                                                                                                                                           
-rw-r--r-- 1 root root  49032472 Nov 20 09:13 data_file                                                                                                
-rw-r--r-- 1 root root 490324720 Nov 20 09:15 large_file
# time cat large_file >> /dev/null                                                                                                   
                                                                                                                                                       
real    0m4.478s                                                                                                                                       
user    0m0.004s                                                                                                                                       
sys     0m0.388s

It took 4.478 seconds to read the file. Let’s read it again. As the file is in the file system cache, it would be interesting to see how long it takes now:

# time cat large_file >> /dev/null                                                                                                   
                                                                                                                                                   
real    0m0.602s                                                                                                                                       
user    0m0.002s                                                                                                                                       
sys     0m0.393s

It took only 0.602 seconds to read the file when it was cached.

It goes without saying that the use of buffer and cache makes I/O operations faster.

3. How to Empty Buffer, Cache, and Swap

Disk caching makes the system much faster and more responsive! But disk caching is also known to cause certain performance bottlenecks. The real problem comes when Linux aggressively starts caching into its memory. In such situations, it is observed that there is less free memory available for allocation to a running application. This memory pressure causes the system to start swapping instead. When page cache becomes quite large (a lot larger than current swap usage), swap in and swap out happen in turns. This is when a drop cache is required.

3.1. Proc Sys VM Drop Caches Command

If we want to empty buffer and cache, we can use this chain of commands:

free && sync && echo 3 > /proc/sys/vm/drop_caches && free

What we just saw is one way of dropping cache. There are three ways in which the Linux kernel drops cached items. Each one is the way to delete different cached items. And, it is achieved just by changing the numeric argument to the echo command.

We can use echo 1 to free page cache:

echo 1 > /proc/sys/vm/drop_caches

We can use echo 2 to free dentries and inodes:

echo 2 > /proc/sys/vm/drop_caches

And, we can use echo 3 to free page cache, dentries, and inodes:

echo 3 > /proc/sys/vm/drop_caches

3.2. Command to Clear out Swap

Under certain circumstances, we may run over a situation when we’ll have to clear out the swap. Before discussing commands to disable or enable swap, we must know what portion of file system memory is called swap. Swap space temporarily holds data moved from the system RAM.

In Linux, the data becomes part of the swap when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory move to the swap space. Now, let’s take a look at commands to disable or enable swap:

swapoff -a && swapon -a

4. Conclusion

In this article, we discussed the experimental validation of the cache behavior before and after the flushing operation. We also discussed how to empty the buffer and cache by simply sending an echo command to the /proc file system. Now, we know that the disk cache makes applications load faster and run smoother, but it NEVER EVER takes memory away from them! If, however, we find ourselves needing to clear some RAM quickly to work around an issue, we can force Linux to nondestructively drop caches.

Towards the end of the article, we briefly discussed the role swap plays in memory management schemes. Having covered all aspects of it, we can conclude that disk caching only borrows the ram that applications don’t currently want. It will not use swap.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.