• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to clear the buffer/pagecache (disk cache) under Linux

By admin

Are you facing a performance issue and you suspect it might be related to cache usage? High cache usage should not normally cause performance issues, but it might be the root cause in some rare cases.

What is Memory Cache

In order to speed operations and reduce disk I/O, the kernel usually does as much caching as it has memory By design, pages containing cached data can be repurposed on-demand for other uses (e.g., apps) Repurposing memory for use in this way is no slower than claiming pristine untouched pages.

What is the purpose of /proc/sys/vm/drop_caches

Writing to /proc/sys/vm/drop_caches allows one to request the kernel immediately drop as much clean cached data as possible. This will usually result in some memory becoming more obviously available; however, under normal circumstances, this should not be necessary.

How to clear the Memory Cache using /proc/sys/vm/drop_caches

Writing the appropriate value to the file /proc/sys/vm/drop_caches causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

1. In order to clear PageCache only run:

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

2. In order to clear dentries (Also called as Directory Cache) and inodes run:

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

3. In order to clear PageCache, dentries and inodes run:

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

Running sync writes out dirty pages to disks. Normally dirty pages are the memory in use, so they are not available for freeing. So, running sync can help the ensuing drop operations to free more memory.

Page cache is memory held after reading files. Linux kernel prefers to keep unused page cache assuming files being read once will most likely to be read again in the near future, hence avoiding the performance impact on disk IO.

dentry and inode_cache are memory held after reading directory/file attributes, such as open() and stat(). dentry is common across all file systems, but inode_cache is on a per-file-system basis. Linux kernel prefers to keep this information assuming it will be needed again in the near future, hence avoiding disk IO.

Note: Starting with the sync command as shown in the above 3 commands is optional. The sync command allows the kernel write as many dirty cache pages to disk as it can (to maximize the number of data cache pages that can be dropped)

How to clear the Memory Cache using sysctl

You can also Trigger cache-dropping by using sysctl -w vm.drop_caches=[number] command.

1. To free pagecache, dentries and inodes, use the below command.

sysctl -w vm.drop_caches=3

2. To free dentries and inodes only, use the below command.

sysctl -w vm.drop_caches=2 

3. To free the pagecache only, use the below command.

sysctl -w vm.drop_caches=1
Note: Using vm.drop_caches can cause a deadlock if the system is under heavy memory and I/O load!!!

“Clean” cached data is eligible for dropping. “Dirty” cached data needs to be written somewhere. Using vm.drop_caches will never trigger the kernel to drop dirty cache.

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. How to list or install only security updates with dnf in CentOS/RHEL 8
  2. How to Switch Password Algorithm on CentOS/RHEL
  3. CentOS / RHEL 5,6 : How to reinstall GRUB loader from rescue mode
  4. What is the purpose of .bash_profile file under User Home Directory In Linux
  5. How to Disable or set SELinux to Permissive mode
  6. How to Manage Oracle database Audit File Directory Growth with cron (ASM Instances Only)
  7. Oracle Software Group Accounts OSDBA, OSOPER, Oracle Inventory group
  8. CentOS / RHEL : How to extend Physical Volume in LVM by extending the Disk Partition used
  9. How to set custom device names using udev in CentOS/RHEL 7
  10. CentOS / RHEL 7 : How to rename the volume group for root and swap

You May Also Like

Primary Sidebar

Recent Posts

  • MySQL: how to figure out which session holds which table level or global read locks
  • Recommended Configuration of the MySQL Performance Schema
  • MySQL: Identify what user and thread are holding on to a meta data lock that is preventing other queries from running
  • MySQL: How to kill a Long Running Query using max_execution_time
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary