Purge Disk I/O Caches on Linux



Most modern operating systems rely on disk I/O caching to improve system performance by storing frequently accessed data in memory. However, there are times when you might want to clear these caches, such as when benchmarking disk performance, troubleshooting, or ensuring that data is read from the disk rather than memory.

This tutorial will guide you through the process of purging disk I/O caches on a Linux system but first let's dive into brief detail on what disk I/O caches are.

Understanding Disk I/O Caches

Linux uses a portion of system RAM to cache disk reads and writes, which speeds up data access by reducing the need to read from or write to the disk (which is much slower). This caching mechanism is managed by the kernel and is beneficial for system performance.

Linux maintains three main types of caches ?

  • Page Cache ? Caches file contents from disk
  • Dentries ? Caches directory entries
  • Inodes ? Caches filesystem metadata

Why Purge Disk I/O Caches?

There are several scenarios where you might want to clear these caches including ?

  • Benchmarking ? To get accurate disk performance metrics, you need to ensure that data is read from the disk, not from the cache. As such clearing disk cache is essential before starting such benchmarking.
  • Troubleshooting ? When diagnosing disk-related issues, clearing caches can help determine if the problem is related to cached data.
  • Data Consistency ? In some cases, especially in development or testing environments, you might want to ensure that the latest data is being read from the disk. Clearing disk cache can be helpful in this scenario.

How to Purge Disk I/O Caches?

In Linux, you can clear disk I/O caches using the sync command and then writing specific value to the /proc/sys/vm/drop_caches file.

You can do this directly using sysctl command as shown below ?

$ sudo sync
$ sudo sysctl -w vm.drop_caches=3


Or using another way, you can also do it by manually writing to this file using the echo command ?

Sync the Filesystem

Before clearing the caches, it's a good practice to flush the filesystem buffers to disk. This ensures that all pending write operations are completed. Use the following command to do so ?

$ sudo sync

The sync command forces the system to write all buffered data to disk, ensuring data integrity.

Clear the Caches

To clear the caches, you need to write to the /proc/sys/vm/drop_caches file. This file allows you to control the cache clearing process. You can choose to clear either page cache, dentries, and inodes by writing different values to this file.

  • Clear Page Cache ? Write 1 to drop page cache.
  • Clear Dentries and Inodes ? Write 2 to drop dentries and inodes.
  • Clear All Caches ? Write 3 to drop page cache, dentries, and inodes.

Use one of the following commands to clear the respective caches ?

$ sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'


Monitoring Cache Usage

You can monitor cache usage in Linux using these commands ?

$ free -h              # Shows memory usage including cache
$ cat /proc/meminfo    # Detailed memory information
$ vmstat               # Virtual memory statistics

Conclusion

Purging disk I/O caches on Linux is a simple process that can be useful in various scenarios, from performance testing to troubleshooting. This article covers the step-by-step process on how you can do that in any Linux machine.

However, always consider the potential impact of clearing such caches on system performance which is normally taken care of by the Linux kernel. As best practice always run sync before clearing caches and use it with caution on production systems considering its impact on running applications.

Updated on: 2025-01-28T17:55:19+05:30

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements